函数readDat()实现从数据文件in.dat中读取20行数据存放到字符串数组XX中(每行字符串长度均小于80)。请编写函数jsSort()。其功能是:以行为单位对字符串按下面给定的条件进行排序,排序后的结果仍按行重新存入字符串数组XX中。最后调用函数w

admin2013-12-10  18

问题 函数readDat()实现从数据文件in.dat中读取20行数据存放到字符串数组XX中(每行字符串长度均小于80)。请编写函数jsSort()。其功能是:以行为单位对字符串按下面给定的条件进行排序,排序后的结果仍按行重新存入字符串数组XX中。最后调用函数writeDat()把结果XX输m到文件out.dat中。条件:从中间位置开始,将字符串分成两部分,左边部分按字符的ASCII码值降序排序,右边部分按字符的ASCII码值升序排序。如果原字符串长度为奇数,则最中间的字符不参加排序,字符仍存放在原位置上。
  例如:位置0 1 2 3 4 5 6 7 8
    原字符串    a b C d h g f e
    1 2 3 4 9 8 7 6 5
    处理后字符串d C b a e f g h
    4 3 2 1 9 5 6 7 8
【试题程序】
    #include
    #include
    char xxE20][80];
    void readDat();
    void writeDat();
    void jsSort()
    {
  jsSort();
    writeDat();
    }
    void readDat()
    {
    FILE*in;
    int i=0;
    char*P;
    in=fopen(“in.dat”,“r”)1
    while(i<20 8L~fgets(xx[-i],80,in)!
NULL){
    p=strehr(xx,\n);
    if(p)*p=0}
    i++;
    }
    fclose(in);
    }
    void writeDat()
    {
    FILE*out}
    int i;
    Outofopen(“out.dat”,“W”);
    for(i=0;i<20;i++){
    printf(“%s\n”,xx);
    fprintf(out,“%s\n”,xx);
    }
    fclose(out);
    }

选项

答案void jsSort() (unsigned int i,j,k; /*定义变量*/ int temp; for(i=0;i<20li++) {for(j=0;jxx[i][k]) {temp=xx[i][i]; xx[i][j]=xxi[i][k]; xx[i][kJ==tempt } } }

解析
转载请注明原文地址:https://kaotiyun.com/show/cElZ777K
0

最新回复(0)