| 贵州学习网—属于你的学习中心 |
|
(本题用于笔试,需对文件调用代码作部分修改方可执行) 函数readdat()实现从文件in.dat中读取20行数据存入到字符串数组xx中(每行字符串长度均小于80字符);请编制jssort(),其函数的功能是;以行为单位对字符串按给定的条件排序,排序后的结果仍按行重新存入字符串数组xx中。最后main()函数调用writedat()把结果xx输出到文件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 f e g h 4 3 2 1 9 5 6 7 8 程序: #include #include #include char xx[20][80]; int readdat(void); void writedat(void); void jssort(void) { int i,j,k,strl,half; char ch; for (i=0;i<20;i++) { strl=strlen(xx[i]); half=strl/2; for (j=0;j if (strl%2) half++; for (j=half;j {ch=xx[i][j]; xx[i][j]=xx[i][k]; xx[i][k]=ch;} } } void main() { readdat(); jssort() writedat(); system("pause"); } int readdat() { file *in; int i=0; char *p; in=fopen("in.dat","r"); while (i<20 %26amp;%26amp; fgets(xx[i],80,in)!=null) { p=strchr(xx[i],’\n’); if (p) *p=0; i++; } fclose(p); return 0; } void writedat(void) { file *out; int i; out=fopen("out.dat","w"); for (i=0;i<=20;i++) { printf("%s\n",xx[i]); fprintf(out,"%s\n",xx[i]); } fclose(out); return 0; } |
责任编辑:gzu521