作业2
1.项目GitHub地址:https://github.com/mmlnn/work1
2.
| PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
|---|---|---|---|
| Planning | 计划 | 480 | 470 |
| · Estimate | · 估计这个任务需要多少时间 | 480 | 470 |
| Development | 开发 | ||
| · Analysis | · 需求分析 (包括学习新技术) | 30 | 40 |
| · Design Spec | · 生成设计文档 | 10 | 10 |
| · Design Review | · 设计复审 (和同事审核设计文档) | 10 | 10 |
| · Coding Standard | · 代码规范 (为目前的开发制定合适的规范) | 10 | 10 |
| · Design | · 具体设计 | 20 | 10 |
| · Coding | · 具体编码 | 150 | 200 |
| · Code Review | · 代码复审 | 20 | 20 |
| · Test | · 测试(自我测试,修改代码,提交修改) | 60 | 50 |
| Reporting | 报告 | 40 | 40 |
| · Test Report | · 测试报告 | 30 | 40 |
| · Size Measurement | · 计算工作量 | 20 | 20 |
| · Postmortem & Process Improvement Plan | · 事后总结, 并提出过程改进计划 | 30 | 20 |
| 合计 | 430 | 470 |
3.解题思路:分析需求,预估完成时间,分步实现功能。
4.设计实现过程:将代码分为一个主函数和三个子函数,三个子函数对应三种功能。
5.代码说明:
int main() { FILE *f; char function; //控制功能 char File[100]; //存储文件名 int a; do{ a=0; printf("输入需被执行操作的文件地址:\n"); scanf("%s",File); if((f=fopen(File,"r"))==NULL) //若找不到文件 {printf("找不到此文件,请重新输入\n"); a=1;}}while(a==1); while(1) {printf("c: 统计字符数 w:统计单词数 l:统计行数 b:退出") ; printf("\n输入所选择的操作 :"); scanf(" %c",&function); if(function=='b'){break;} switch(function){ case 'c': countcharacter(File);break; case 'w': countword(File);break; case 'l': countline(File);break; case 'b': break; default : printf("输入错误,请重新输入\n"); } } }
统计字符函数
void countcharacter(char*File)//字符数函数 { int count=0;//计数 char ch; FILE *f=fopen(File,"r"); while((ch=fgetc(f))!=EOF) {if(ch!=' '&&ch!='\n'&&ch!='\t') count++;} fclose(f); printf("字符数为%d\n\n",count); }
统计单词数函数
void countword(char*File)//单词数函数 { int count=0; char ch; FILE *f=fopen(File,"r"); while((ch=fgetc(f))!=EOF) { while((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) ch=fgetc(f); count++; } fclose(f); printf("单词数为%d\n\n",count); }
统计行数函数
void countline(char*File) //行数函数 { int count=0; char ch; FILE *f=fopen(File,"r"); while((ch=fgetc(f))!=EOF){ if(ch=='\n') //遇到换行符+1
count++; } count++; fclose(f); printf("行数为%d\n\n",count); }
6.测试运行:


7.项目小结:大一时c语言没教文件操作,所以现学现用,了解了fopen等函数,由于知识掌握的不牢,debug很久。
浙公网安备 33010602011771号