软工个人项目作业1
个人项目作业
github个人项目地址:https://github.com/DongDGT/WordCount
1.PSP2.1
|
PSP2.1 |
Personal Software Process Stages |
预估耗时(分钟) |
实际耗时(分钟) |
|
Planning |
计划 |
10 |
5 |
|
· Estimate |
· 估计这个任务需要多少时间 |
10 |
5 |
|
Development |
开发 |
430 |
850 |
|
· Analysis |
· 需求分析 (包括学习新技术) |
30 |
180 |
|
· Design Spec |
· 生成设计文档 |
30 |
30 |
|
· Design Review |
· 设计复审 (和同事审核设计文档) |
5 |
5 |
|
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
5 |
5 |
|
· Design |
· 具体设计 |
30 |
30 |
|
· Coding |
· 具体编码 |
300 |
480 |
|
· Code Review |
· 代码复审 |
30 |
30 |
|
· Test |
· 测试(自我测试,修改代码,提交修改) |
300 |
120 |
|
Reporting |
报告 |
90 |
90 |
|
· Test Report |
· 测试报告 |
30 |
30 |
|
· Size Measurement |
· 计算工作量 |
30 |
30 |
|
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
30 |
30 |
|
合计 |
|
530 |
945 |
2.解题思路
这个作业一看过去是要对文件进行操作,于是就先去学了一下C++文件流相关的知识,主要是如何读入文件流。然后去再看题目,要数行数,我是想读文件流中的\n来判断是否到了行尾,
但是ifstream只能一次读一个string,看不出\n,找来找去发现了流迭代器可以完美符合我的需要,能从文件流中一个个字符读入,接下来就好判断多了,只要做好字符的判断就能判断出行数和列数了。由于时间紧急,暂时还没有去做递归查找和用图形界面选文件的操作。
3.设计思路
设计两个类,一个负责处理文件流计算各个数据并传出去,即核心计算类(WordCount类);一个负责处理主函数中的参数生成相应的信息控制计算器,即消息处理类(ControlMessage类);
核心计算类只需要文件流、各种计数结果,以及各类计算函数和对应的控制器。
消息处理类包括操作指令集合、要计算的文件名集合、收集信息函数、分配信息函数和根据信息操作函数
4.核心代码
计算类中大部分的功能实现是差不多的,下面仅列出-a功能的函数,利用了流迭代器来实现逐字符检查。
void WordCount::OthersLinesCount() { istreambuf_iterator<char>bInterator(this->inStream); istreambuf_iterator<char>eof; bool isNote=false; bool isNoting=false; bool isNoteReady=false; bool isNoteEndReady=false; bool isDataLine=false; while(bInterator!=eof){ char nowchar; nowchar=*bInterator; if (((nowchar >= 'A' && nowchar <= 'Z') || (nowchar >= 'a' && nowchar <= 'z')) && !isNote&&!isNoting) { isDataLine=true; isNoteEndReady = false; isNoteReady = false; } else if(nowchar=='/') { if(!isNoteReady) isNoteReady=true; else isNote=true; if (isNoteEndReady) isNoting = false; isNoteEndReady=false; } else if(nowchar=='*') { if(isNoteReady) isNoting=true; else if(isNoting) isNoteEndReady=true; isNoteReady = false; } else if(nowchar=='\n') { if(isDataLine) this->dataLinesCount++; else if(isNote) this->notesLinesCount++; else if (isNoting) this->notesLinesCount++; else this->emptyLinesCount++; isNote=false; isDataLine=false; isNoteEndReady = false; isNoteReady = false; } bInterator++; } inStream.seekg(ios::beg); }
消息处理类中把主函数中的参数分成操作和文件名两类,便于后面处理,下面是分配信息函数
1 void ControlMessage::Distribution() 2 { 3 bool controlError = false; 4 for (int i = 0; i < message.size(); i++) 5 { 6 if ((message[i].data())[0] == '-') 7 { 8 if ((message[i].data())[1] == 'c' || (message[i].data())[1] == 'w' || (message[i].data())[1] == 'l' || (message[i].data())[1] == 'a') 9 { 10 this->control.push_back((message[i].data())[1]); 11 } 12 else 13 controlError = true; 14 } 15 else 16 { 17 fileName.push_back(message[i]); 18 } 19 } 20 if (controlError) 21 { 22 cout << "输入的操作有误,请检查有无输入错误的操作符" << endl; 23 } 24 this->isDistribution = true; 25 this->addAble = false; 26 }
主函数只需要按顺序调用消息处理类中的收集、分配和处理函数就行了
1 #include<string> 2 #include"ControlMessage.h" 3 4 using namespace std; 5 6 int main(int argc, char* argv[]){ 7 8 ControlMessage controlor; 9 10 for (int i = 1; i < argc; i++) 11 { 12 controlor.AddMessage(argv[i]); 13 } /*123*/ 14 int test/*仅测试用*/; 15 controlor.Distribution(); 16 controlor.Control(); 17 return 0; 18 } 19 20
5.测试用例
本程序实现了-c、-l、-w和-a
用main.cpp来测试

测试结果符合要求
6.项目小结
算是一次有规划、提前做好计划的一次项目经历,提前对整个项目做分析,分好类,学习完新的知识点之后再进行编程,速度快了不少,虽然由于特殊原因导致时间紧迫,致使没时间去完成另外两项功能算是个遗憾,慢慢有空再撘起来吧。

浙公网安备 33010602011771号