个人作业

1.

 

PSP2.1Personal Software Process Stages预估耗时(分钟)实际耗时(分钟)
Planning 计划   500    500
· Estimate · 估计这个任务需要多少时间  480         470
Development 开发    
· Analysis · 需求分析 (包括学习新技术)  35 35
· Design Spec · 生成设计文档  10  10
· Design Review · 设计复审 (和同事审核设计文档)  10  10
· Coding Standard · 代码规范 (为目前的开发制定合适的规范)  10  10
· Design · 具体设计  20  10
· Coding · 具体编码  170 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)//  统计字符数函数 
{char ch;//空
int count=0;//从0开始计数

FILE *f=fopen(File,"r");
while((ch=fgetc(f))!=EOF)
{if(ch!=' '&&ch!='\n'&&ch!='\t')//如果不为空格、换行符、tab
    count++;}
fclose(f);
printf("字符数为%d\n\n",count);

}
复制代码

统计单词数函数

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void countword(char*File)//单词数函数
{int tag=0;
int count=0;
char ch;
FILE *f=fopen(File,"r");
while((ch=fgetc(f))!=EOF)
{
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) tag=1;
        else if(tag==1){  //ch不是字母且标志为1时则说明刚检测完一个单词
            count++;
            tag=0;
}
}
 
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') count++;
    }
count++; //文件结束时当前行加一
fclose(f);
if(f!=NULL) printf("行数为%d\n\n",count);
}
复制代码

6.测试运行:

7.项目小结:现学现用,利用结合之前c语言技术进行思考

posted @ 2020-03-16 01:36  小杨的博客~  阅读(127)  评论(0编辑  收藏  举报