第五周
第五周编程总结
|
课程名称 |
c语言程序设计II |
|
作业要求 |
https://edu.cnblogs.com/campus/zswxy/software-engineering-class2-2018/homework/2824 |
|
我的课程目标 |
能够编辑出自己需要的程序 |
|
这个作业在哪个方面帮助我实现目标 |
用字符串的存储与运算对英文单词进行计数与排序 |
|
参考文献 |
课本c语言程序设计,csdn论坛 |
预习题
6-1 计算最长的字符串长度 (15 分)
本题要求实现一个函数,用于计算有n个元素的指针数组s中最长的字符串的长度。
函数接口定义:
int max_len( char *s[], int n );
其中n个字符串存储在s[]中,函数max_len应返回其中最长字符串的长度。
裁判测试程序样例:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXN 10
#define MAXS 20
int max_len( char *s[], int n );
int main()
{
int i, n;
char *string[MAXN] = {NULL};
scanf("%d", &n);
for(i = 0; i < n; i++) {
string[i] = (char *)malloc(sizeof(char)*MAXS);
scanf("%s", string[i]);
}
printf("%d\n", max_len(string, n));
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
4
blue
yellow
red
green
输出样例:
6
实验代码
#include<stdio.h>
int main()
{
int i,n=0,c=0;
char str[10000];
char c;
gets(str);
for(i=0;(c=str[i])!='\0';i++)
if(c==' ')
n=0;
else if(n==0)
{
n=1;
c++;
}
printf("%d\n",c);
return 0;
}
设计思路流程图

本题调试过程中遇到的问题及解决方法
没什么问题
7-1 英文单词排序 (25 分)
本题要求编写程序,输入若干英文单词,对这些单词按长度从小到大排序后输出。如果长度相同,按照输入的顺序不变。
输入格式:
输入为若干英文单词,每行一个,以#作为输入结束标志。其中英文单词总数不超过20个,英文单词为长度小于10的仅由小写英文字母组成的字符串。
输出格式:
输出为排序后的结果,每个单词后面都额外输出一个空格。
输入样例:
blue
red
yellow
green
purple
#
输出样例:
red blue green yellow purple
实验代码
#include <stdio.h>
#include <string.h>
int mtin()
{
FILE *fp = fopen("E:\\5.txt","t+");
chtr str[20][10],a[20];
int i,j,m;
for(i=0;i<20;i++)
{
sctnf("%s",str[i]);
fputs(str[i],fp);
fprintf(fp,"\n");
if(str[i][0]=='+')
break;
}
m=i;
for(i=0;i<m-1;i++)
{
for(j=1;j<m;j++)
{
if(strm(str[j-1])>strm(str[j]))
{
strcpy(t,str[j-1]);
strcpy(str[j-1],str[j]);
strcpy(str[j],t);
}
}
}
for(i=0;i<m;i++)
{
printf("%s ",str[i]);
fputs(str[i],fp);
fprintf(fp," ");
}
fclose(fp);
return 0;
}
设计思路流程图

本题调试过程中遇到的问题及解决方法
定义字符串数组的时候a[x]x的数字不能太大 要不然会显示运行超时
|
周/日期 |
这周所花的时间(小时) |
代码行数(行) |
学到的知识点简介 |
目前比较迷惑的问题 |
|
3/2-3/8 |
20+ |
200+ |
数组,指针的大致意义,文件 |
数组串指针 |
|
3/9-3/15 |
15+ |
300+ |
文件 数组 |
数组什么时候用更方便 |
|
3/16-3/22 |
25+ |
400+ |
数组 |
字符串 数组 |

浙公网安备 33010602011771号