2019春第五周作业

2019春第五周作业

| 这个作业属于那个课程 | C语言程序设计II |
| -------- | -----: | :----: |
| 这个作业要求在哪里 | https://edu.cnblogs.com/campus/zswxy/software-engineering-class1-2018/homework/2825|
| 我在这个课程的目标是 | 掌握字符串的基本概念及使用方法 |
| 这个作业在那个具体方面帮助我实现目标 | 理解字符串与一维字符数组的区别、字符串的存储以及字符串的操作方法 |
|参考文献|无|

基础作业

本题要求编写程序,输入若干英文单词,对这些单词按长度从小到大排序后输出。如果长度相同,按照输入的顺序不变。

输入格式:

输入为若干英文单词,每行一个,以#作为输入结束标志。其中英文单词总数不超过20个,英文单词为长度小于10的仅由小写英文字母组成的字符串。

输出格式:

输出为排序后的结果,每个单词后面都额外输出一个空格。

输入样例:

blue
red
yellow
green
purple

输出样例:

red blue green yellow purple

1)PTA代码

#include <stdio.h>
#include <string.h>
main()
{
char str[20][10],t[20],str1[10];
int i,j,n=0;
while(1)
{
	scanf("%s",str1);
	if(str1[0]=='#')
    {
		break;
	}
    else
	{
    strcpy(str[n],str1);
    n++;
    }
}
for(i=0;i<n-1;i++)
	for(j=0;j<n-i-1;j++)
    {
        if(strlen(str[j])>strlen(str[j+1]))
	    {
           strcpy(t,str[j]);
           strcpy(str[j],str[j+1]);
           strcpy(str[j+1],t);
        }
    }
for(i=0;i<n;i++)
{
    printf("%s ",str[i]);
}

}

2)博客园改进代码

#include <stdio.h> 
#include <string.h>
#include <stdlib.h>
int main(void) {
FILE * fp; 
char input[21][11] =  {'\0'}; 
char snap[11] =  {'\0'}; 
int i = 0; 
if ((fp=fopen("F:\\txt\\klaus杨梓欣.txt","a+")) == NULL)
{
    printf ("File open error!\n");
    exit (0);
}


while (1) {
    
    fscanf(fp,"%s", input[i]);
    printf("%s\n", 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); 
        }
    }
}
fprintf(fp,"\n");
for (i = 0; i < len; i++){
    fprintf(fp,"%s\n", input[i]);
    printf("%s\n", input[i]);
}   
    if(fclose(fp) )
{
    printf("Can not close the file!\n");
    exit(0);
}

return 0;

}

3)运行结果截图



4)流程图

4)时间表

| 周/日期 | 这周所花时间 | 代码行| 学到的知识点 |目前比较困惑的问题
| -------- | -----: | :----: |
| 3/9-3/15 | 30min | 35 |文件的建立|
| 3/15-3/22 | 40min | 46 |一维二维数组|二维数组的调用
| 3/22-3/29 | 1h| 69 |如何输出一行的连续字符|指针的使用

posted @ 2019-03-29 09:07  修电脑的阿欣  阅读(164)  评论(1编辑  收藏  举报