课程 | |
---|---|
作业要求 | |
github地址 | https://github.com/xxxy7280/WordCount |
伙伴博客地址 |
PSP表格
PSP2.1 | Personal Software Process Stages | 预估耗时(分钟 | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 30 | 30 |
Estimate | 估计这个任务需要多长时间 | 3天 | 5天 |
Development | 开发 | 60 | 80 |
Analysis | 需求分析(包括学习新技术) | 30 | 45 |
Design Spec | 生成设计文档 | 60 | 80 |
Design Review | 设计复审(和同事审核设计文档) | 30 | 35 |
Coding Standard | 代码规范(为目前的开发制定合适的规范) | 10 | 15 |
Design | 具体设计 | 30 | 30 |
Coding | 具体编码 | 600 | 720 |
Code Review | 代码复审 | 60 | 80 |
Test | 测试(自我测试、修改代码、提交修改) | 120 | 130 |
Reporting | 报告 | 30 | 40 |
Test Report | 测试报告 | 30 | 50 |
Size Measurement | 计算工作量 | 20 | 30 |
Postmortem & Process Improvement Plan | 事后总结,并提出过程改进计划 | 60 | 55 |
合计 | 1170 | 1420 |
计算模块接口的设计与实现过程
计算设计
思维导图
除主函数外,此程序设计了六个基本模块,
Characres():实现字符统计
Word_s():统计单词
Line():统计行数
All():实现所有输出功能
Output(string path):输出结果到指定文件,参数为指定文件名
void Cal::Output(string path)//输出到文件 { ofstream fout; string path_1="D:\\wordCount\\201831061307\\Count\\"+path; fout.open(path_1); //fout.open("D:\\wordCount\\201831061307\\Count\\output.txt"); fout << "characters: " << Characters() << endl; fout << "words: " <<Word_s() << endl; fout << "lines: " << Lines() << endl; for (int i = 0; i < numofwords - 1; i++) { fout << Words[i].str<< "\t"; fout << Words[i].many << endl; } fout.close(); cout << "成功写入文件"<<endl; }
cmd输入D:\WordCount\201831061307\Count\Debug\Count.exe -o output.txt
运行结果
对关键函数-统计词频函数按如下思维
封装与设计接口
函数封装
接口为
#include"Count.h"
运行结果
代码复审过程
代码规范:
1、在进行变量名和函数名的定义的时候加上必要的注释,注明其作用与功能
2、在函数代码中,在必要的地方添加说明,便于理解查错
3、全局变量必须定义在开头,使用时便于知道其意义
4、缩进时使用tab键(有的时候需要自己缩进)
5、括号的使用很有必要,标注优先级
6、再给变量赋值时,一个变量一行
7、再有意义相同变量时,在其结尾添加数字用于区分
参考网址:C语言代码规范(编程规范) (http://c.biancheng.net/view/158.html)
两人互审中发现的问题:
同学A:
1.定义变量时有时候不解释其意义
2.关键功能处不加以说明,同伴阅读困难
3.有的时候忘了缩进,代码不雅观
同学B:
1.多定义了一些变量,未使用也未删除
2.编写函数时不解释其功能
计算模块接口部分的性能改进
效能分析代码-主函数
int main() { while(1) { words(); sort(); shuchu(5); errno_t err; err = fopen_s(&fp, "D:\\WordCount\\201831061307\\Count\\input.txt", "r+"); Characters(); Word_s(); Lines(); All(); Output(); fclose(fp); } return 0; }
初始版本效能分析结果
由图可知,耗时最大的是All()函数
void All()//输出所有功能结果 { cout << "characters: " << Characters() << endl; cout << "words: " << Word_s() << endl; cout << "lines: " << Lines() << endl; for (int i = 0; i < numofwords - 1; i++) { cout << Words[i].str << "\t"; cout << Words[i].many << endl; } }
计算模块部分单元测试展示
测试单词个数
此时向Word_s中添加如下代码
errno_t err; err = fopen_s(&fp, "D:\\WordCount\\201831061307\\Count\\input.txt", "r+");
测试代码:
#include "stdafx.h" #include "CppUnitTest.h" #include"D:\WordCount\201831061307\Count\Count\Count.h" using namespace Microsoft::VisualStudio::CppUnitTestFramework; namespace CountUnitTest1 { TEST_CLASS(UnitTest1) { public: TEST_METHOD(TestMethod1) { Cal x; int count = 107; Assert::AreEqual(x.Word_s(),count);// TODO: 在此输入测试代码 } }; }
测试结果:
测试行数
TEST_METHOD(TestMethod3) { Cal x; int count = 5; Assert::AreEqual(x.Lines(), count);// TODO: 在此输入测试代码 }
如果有空行 忽略
测试字符数
TEST_METHOD(TestMethod2) { Cal x; int count = 505; Assert::AreEqual(x.Characters(), count);// TODO: 在此输入测试代码 }
测试结果
计算模块部分异常处理说明
直接在主函数和cmd窗口中进行异常处理
指定文件打开失败
修改代码中文件名为一个不存在的文件 以只读方式打开 打开失败时 退出程序
else if(...) { errno_t err; err = fopen_s(&fp, "D:\\WordCount\\201831061307\\Count\\eee.txt", "r+"); if (fp == NULL) { cout << "The file can not be opened" << endl; exit(0); } else if... }
运行结果
命令行参数输入错误
修改原有代码
else分支改为
else if(strcmp(argv[1], "-c") == 0 || strcmp(argv[1], "-w") == 0 || strcmp(argv[1], "-l") == 0 \ || strcmp(argv[1], "-a") == 0 || strcmp(argv[1], "-o") == 0)
else { cout << "input error!" << endl; }
测试结果
描述结对的过程
作业感想
结对编程时可以互相发现对方问题,相比单人完成任务效率更高,在以后的项目工作中,可以适时采用结对编程的工作方式,对项目完成效率和质量都有帮助。