1.GitHub存储库链接:https://github.com/1078862512/Software-Engineering
2.开发程序前,模块开发预计时间:
PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
Planning | 计划 | ||
· Estimate | ·估计这个任务需要多长时间 | 30 | |
Development | 开发 | ||
· Analysis | ·需求分析(包括学习新技术) | 180 | |
· Design Spec | ·生成设计文档 | 20 | |
· Design Review | ·设计复审(和同事审核设计文档) | ||
· Coding Standard | ·代码规范(为目前的开发制定合适的规范) | ||
· Design | ·具体设计 | 40 | |
· Coding | ·具体代码 | 300 | |
· Code Review | ·代码复审 | 60 | |
· Test | ·测试(自我测试,修改代码,提交修改) | 40 | |
Reporting | 报告 | ||
· Test Report | ·测试报告 | 20 | |
· Size Measurement | ·计算工作量 | 10 | |
· Postmortem & Process Improvement Plan | ·事后总结,并提出过程改进计划 | 10 | |
合计 | 710 |
3.解题思路
编程语言:C
需要用到的知识:C语言对文件流的控制、字符串、字符数组、图形化界面等
结构分析:分为c、w、l、x、a四种模式,故可分为一个主函数调用四个函数实现
4.设计实现过程
根据用户输入的“order”的不同,主函数会调用不同的函数来实现相应功能:-c是调用字符数,-w是词的数,-l是行数,-s可调用递归,-a可现实复杂统计,-x参数可以启动图形化操作。
5.代码说明主程序
(1).字符统计
int charcount(char file[])//字符统计函数 { FILE *p = NULL; int charcount = 0; char c; fopen_s(&p,file, "r"); if (p==NULL) { printf("文件打开失败 "); return -1; } printf("文件打开成功 "); c=fgetc(p); while (c != EOF) { c = fgetc(p); charcount++; } fclose(p); return charcount; }
(2)行数统计
int linecount(char file[])//行数统计函数 { FILE *p = NULL; char c; int linecount = 0; fopen_s(&p,file, "r"); if (p == NULL) { printf("文件打开失败 "); return -1; } c = fgetc(p); while (c != EOF) { if (c == ' ') { linecount++; } c = fgetc(p); } linecount++; fclose(p); return linecount; }
(3)单词统计
int wordcount(char file[])//单词数统计函数 { FILE *p = NULL; char c; int wordcount = 0; fopen_s(&p, file, "r"); if (p == NULL) { printf("文件打开失败 "); return -1; } c = fgetc(p); while (c != EOF) { if (c >= 'a'&&c <= 'z' || c >= 'A'&&c <= 'Z' || c >= '0'&&c <= '9') { while (c >= 'a'&&c <= 'z' || c >= 'A'&&c <= 'Z' || c >= '0'&&c <= '9' || c == '_') { c = fgetc(p); } wordcount++; c = fgetc(p); } c = fgetc(p); } fclose(p); return wordcount; }
(5)空行统计:
int spacecount(char file[])//a模式下的空行统计函数 { FILE *p = NULL; char c; int spacecount = 0; int codecount = 0; int annocount = 0; int count = 0; fopen_s(&p, file, "r"); if (p == NULL) { printf("文件打开失败 "); return -1; } c = fgetc(p); while (c != EOF) { if (c != ' ') { count++; } if (count<2&&c == ' ') { spacecount++; } if (c == ' ') { count = 0; } c = fgetc(p); } fclose(p); return spacecount; }
(6)代码行统计
int codecount(char file[])//a模式下的代码行统计函数 { FILE *p = NULL; char c; int codecount = 0; int count = 0; int flag = 0; fopen_s(&p, file, "r"); if (p == NULL) { printf("文件打开失败 "); return -1; } c = fgetc(p); while (c != EOF) { if (c == '/') { c = fgetc(p); if (c == '/') { flag=1; } } if (c != ' ') { count++; } if (count>=2 && c == ' '&&flag==0) { codecount++; } if (c == ' ') { count = 0; flag = 0; } c = fgetc(p); } fclose(p); return codecount; }
(7)注释行统计
int annocount(char file[])//a模式下的注释行统计函数 { FILE *p = NULL; char c; int annocount = 0; int count = 0; fopen_s(&p, file, "r"); if (p == NULL) { printf("文件打开失败 "); return -1; } c = fgetc(p); while (c != EOF) { if (c == '/') { c = fgetc(p); if (c == '/') { annocount++; } } c = fgetc(p); } fclose(p); return annocount; }
(8)主程序
int main() { int final; char order; char file[50]; while (1) { printf("*********************************** "); printf("欢迎使用,字符统计工具 "); printf("请选择要实现的功能: c:字符数统计 l:行数统计 w:单词数统计 a:进行文件中空行、代码行与注释行统计 "); printf("*********************************** "); printf("要结束,请输入q "); scanf_s("%c", &order,1); switch(order-'a') { case 2: { printf("请输入文件名称: "); scanf_s("%s", file, 50); final = charcount(file); if (final == -1) { printf("文件名出错 "); break; } printf("文件字符总数为:%d ", final); break; } case 11: { printf("请输入文件名称: "); scanf_s("%s", file, 50); final = linecount(file); if (final == -1) { printf("文件名出错 "); break; } printf("文件行总数为:%d ", final); break; } case 22: { printf("请输入文件名称: "); scanf_s("%s", file, 50); final = wordcount(file); if (final == -1) { printf("文件名出错 "); break; } printf("文件单词总数为:%d ", final); break; } case 0: { printf("请输入文件名称: "); scanf_s("%s", file, 50); final = spacecount(file); if (final == -1) { printf("文件名出错 "); break; } printf("文件空行总数为:%d ", final); final = codecount(file); if (final == -1) { printf("文件名出错 "); break; } printf("文件代码行总数为:%d ", final); final = annocount(file); if (final == -1) { printf("文件名出错 "); break; } printf("文件注释行总数为:%d ", final); break; } case 16: { printf("本次运行结束,感谢使用 "); return 0; } } } }
6.运行测试
7.实际花费时间
PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
Planning | 计划 | ||
· Estimate | ·估计这个任务需要多长时间 | 34 | |
Development | 开发 | ||
· Analysis | ·需求分析(包括学习新技术) | 200 | |
· Design Spec | ·生成设计文档 | 32 | |
· Design Review | ·设计复审(和同事审核设计文档) | ||
· Coding Standard | ·代码规范(为目前的开发制定合适的规范) | ||
· Design | ·具体设计 | 20 | |
· Coding | ·具体代码 | 342 | |
· Code Review | ·代码复审 | 50 | |
· Test | ·测试(自我测试,修改代码,提交修改) | 35 | |
Reporting | 报告 | ||
· Test Report | ·测试报告 | 10 | |
· Size Measurement | ·计算工作量 | 10 | |
· Postmortem & Process Improvement Plan | ·事后总结,并提出过程改进计划 | 10 | |
合计 | 743 |
8.项目小结
我承认这是个不完整的项目。由于个人技术原因,递归以及图形化界面还在制作中。我在这之前是主要在钻研前端开发的一些东西,我个人是比较喜欢那些能看见的东西的,所以就没太注意后台的编程技术这方面。接到这个项目之后,首先是惊诧——我根本没这些技术,就要我自己去做一个这样的程序了?后来还是硬着头皮一点点学文档函数,并且在网上寻找C语言图形化选择文件的办法。
在这个项目做到差不多现在这个样子的时候,我觉得自己的技术确实跟别人差了太多,我们学习的大部分是后台的东西,前端的东西好像在课程上暂时用不到。所以就制定了这学期学习新技术的计划。这一次的时间耗费上与预期相差不大,主要花在学新技术上。我希望通过自己的学习以及一次次的项目作业,自己在这个学期能有很大进步。我也将在之后的时间里完善这个不怎么美好的开始。