第五周作业 末蓝、 2022-01-07 09:53 310阅读 0赞 <table> <thead> <tr> <th>这个课程属于哪个课程</th> <th>C语言程序设计II</th> </tr> </thead> <tbody> <tr> <td>这个作业要求在哪里</td> <td>2019春季学期第五周作业</td> </tr> <tr> <td>我的课程目标</td> <td>学会并运用各种排序法</td> </tr> <tr> <td>这个作业在哪个具体方面帮助我实现目标</td> <td>写排序法</td> </tr> <tr> <td>参考文献</td> <td>《C语言程序设计(第3版》</td> </tr> </tbody> </table> 7-1 英文单词排序 (25 分) 本题要求编写程序,输入若干英文单词,对这些单词按长度从小到大排序后输出。如果长度相同,按照输入的顺序不变。 输入格式: 输入为若干英文单词,每行一个,以\#作为输入结束标志。其中英文单词总数不超过20个,英文单词为长度小于10的仅由小写英文字母组成的字符串。 输出格式: 输出为排序后的结果,每个单词后面都额外输出一个空格。 输入样例: blue red yellow green purple # # 输出样例: red blue green yellow purple PTA实验代码 # include<stdio.h> # # include<string.h> # int main(void) \{ char input\[21\]\[11\] = \{ '\\0' \}; char snap\[11\] = \{ '\\0' \}; int i = 0; while (1) \{ scanf("%s", input\[i\]); if (input\[i\]\[0\] == '\#') break; i++; \} input\[i\]\[0\] = '\\0'; int len = i; int j = 0; for (i = 0; i < len; i++) \{ for (j = 1; j < len - i; j++) \{ if (strlen(input\[j - 1\]) > strlen(input\[j\])) \{ strcpy(snap, input\[j - 1\]); strcpy(input\[j - 1\], input\[j\]); strcpy(input\[j\], snap); \} \} \} for (i = 0; i < len; i++) printf("%s ", input\[i\]); return 0; \} 博客园实验代码 # include<stdio.h> # # include<stdlib.h> # # include<string.h> # int main(void) \{ FILE \*fp; fp=fopen("c:\\作业1\\lwr.txt","r+"); char input\[21\]\[11\] = \{ '\\0' \}; char snap\[11\] = \{ '\\0' \}; int i = 0; while (1) \{ scanf("%s", input\[i\]); if (input\[i\]\[0\] == '\#') break; i++; \} input\[i\]\[0\] = '\\0'; int len = i; int j = 0; for (i = 0; i < len; i++) \{ for (j = 1; j < len - i; j++) \{ if (strlen(input\[j - 1\]) > strlen(input\[j\])) \{ strcpy(snap, input\[j - 1\]); strcpy(input\[j - 1\], input\[j\]); strcpy(input\[j\], snap); \} \} \} for (i = 0; i < len; i++) printf("%s ", input\[i\]); fprintf(fp,"\\n"); for(i=0;i<n;i++) \{ fprintf(fp,"%s ",a\[i\]); \} if(fclose(fp)) { printf("Can not close the file!\n"); exit(0); } return 0; \} 流程图如下 ![1580605-20190329213452097-1316221535.png][] 本题调试过程碰到的问题及解决办法 排序法和ASCII字符开始没看懂,后来问度娘搞懂了。 运行结果图 ![1580605-20190329215209604-916387251.png][] 预习作业截图 ![1580605-20190329215504986-1871909671.png][] ![1580605-20190329215624588-1303578789.png][] ![1580605-20190329215708644-1678560670.png][] 学习进度条 <table> <thead> <tr> <th>周/日期</th> <th>这周所花的时间</th> <th>代码行数</th> <th>学到的知识点简介</th> <th>目前比较迷惑的问题</th> </tr> </thead> <tbody> <tr> <td>3/24-3/29</td> <td>2天</td> <td>80行</td> <td>二维数组的定义与使用</td> <td>与字符串有关的函数</td> </tr> </tbody> </table> 转载于:https://www.cnblogs.com/lwr4539/p/10624191.html [1580605-20190329213452097-1316221535.png]: /images/20211223/527bfdcfbffc414fb8c10801d00b2a8d.png [1580605-20190329215209604-916387251.png]: /images/20211223/5530869526c5458c9227731f54cbf5a5.png [1580605-20190329215504986-1871909671.png]: /images/20211223/818a392a677f48778bc2150e71a9ce4f.png [1580605-20190329215624588-1303578789.png]: /images/20211223/4877f695e58849adb8f14a96656c63c7.png [1580605-20190329215708644-1678560670.png]: /images/20211223/ab2893b746c2431c9607c4d48dd737bd.png
还没有评论,来说两句吧...