2006年9月全國等級考試三級c語言上機(jī)題庫(四十三)

字號:

☆題目43(無憂id 82 字符排序題)
    無憂id 82題(只是將結(jié)果按“從大到小”排序)
    函數(shù)ReadDat()實(shí)現(xiàn)從文件in.dat中讀取20行數(shù)據(jù)存放到字符串?dāng)?shù)組xx中(每行字符串長度均小于80)。請編制函數(shù)jsSort(),其函數(shù)的功能是:以行為單位對字符串變量的下標(biāo)為奇數(shù)的字符按其ASCII值從小到大的順序進(jìn)行排序,排序后的結(jié)果仍按行重新存入字符串?dāng)?shù)組xx中,最后調(diào)用函數(shù)WriteDat()把結(jié)果xx輸出到文件out.dat中。
    例如:位置   0 1 2 3 4 5 6 7 
    源字符串 a b c d e f g h
    則處理后字符串 a h c f e d g b
    部分源程序存在文件prog1.c中。
    請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
    #include
    #include
    #include
    char xx[20][80];
    void jsSort()
    {int i,j,k,strl;
    char ch;
    for(i=0;i<20;i++)
    { strl=strlen(xx[i]);
    for(j=1;j    for(k=j+2;k    if(xx[i][j]>xx[i][k]) { ch=xx[i][j];xx[i][j]=xx[i][k];xx[i][k]=ch;}
    }
    }
    void main()
    {
    readDat();
    jsSort();
    writeDat();
    }
    readDat()
    {
    FILE *in;
    int i=0;
    char *p;
    in=fopen("in.dat","r");
    while(i<20&&fgets(xx[i],80,in)!=NULL){
    p=strchr(xx[i],'\n');
    if(p)*p=0;
    i++;
    }
    fclose(in);
    }
    writeDat()
    {
    FILE *out;
    int i;
    out=fopen("out.dat","w");
    clrscr();
    for(i=0;i<20;i++){
    printf("%s\n",xx[i]);
    fprintf(out,"%s\n",xx[i]);
    }
    fclose(out);
    }