一.Github链接
https://github.com/sicongHe/WordCount
二.PSP表格
| PSP阶段 | 预估耗时(小时) | 实际耗时(小时) |
| 计划: | ||
| 估计这个任务需要多少时间 | 14 | 24 |
| 开发: | ||
| 需求分析(包括学习新技术) | 2 | 2 |
| 生成设计文档 | 1 | 1 |
| 设计复审(和同事复审设计文档) | 1 | 2 |
| 代码规范(为目前的开发制定合适的规范) | 1 | 2 |
| 具体设计 | 1 | 2 |
| 具体编码 | 2 | 8 |
| 代码复审 | 1 | 1 |
| 测设(自我测试,修改代码,提交修改) | 2 | 3 |
| 报告: | ||
| 测试报告 | 1 | 1 |
| 计算工作量 | 1 | 1 |
| 事后总结,并提出过程改进计划 | 1 | 1 |
| 合计 | 14 | 24 |
三.解题思路
老师给出的需求模糊不清,很多细节的地方只能靠自己脑补,脑补完需求开始写代码。
思路大概是先处理命令行的输入参数,不同的参数会在随后调用不同的函数来生成返回结果,返回结果由所有输入参数对应的函数拼接组成。
思路没什么独特的,代码编写也不难,关键是需求给的太不明确了。
四。程序设计实现
各个参数对应的子函数如下
int character(string name) {
ifstream infile;
infile.open("./" + name);
int i = 0;
char buf;
while (!infile.eof()) {
infile.get(buf);
i++;
}
infile.close();
return i-1;
}
int line(string name) {
ifstream infile;
infile.open("./" + name);
int i = 1;
char buf;
while (!infile.eof()) {
infile.get(buf);
if(buf == '\n')
i++;
}
infile.close();
return i;
}
int word(string name) {
ifstream infile;
infile.open("./" + name);
int i = 0;
bool flag = true;
char buf;
while (!infile.eof()) {
infile.get(buf);
if (buf != ' ' && buf != ',' && buf != '\n') {
if (flag == true) {
i++;
flag = false;
}
}
else {
flag = true;
}
}
infile.close();
return i;
}
int codeLine(string name) {
ifstream infile;
infile.open("./" + name);
int i = 0;
bool flag = true;
string buf;
while (getline(infile,buf)){
if (flag == false) {
int max = buf.size();
if (buf[max - 2] == '*' && buf[max - 1] == '/') {
flag = true;
}
continue;
}
if (buf.size() < 2) {
continue;
}
else {
if (buf[0] == '/' && buf[1] == '/') {
continue;
}
if (buf[0] == '/' && buf[1] == '*') {
flag = false;
continue;
}
i++;
}
}
infile.close();
return i;
}
int noteLine(string name) {
ifstream infile;
infile.open("./" + name);
int i = 0;
bool flag = false;
string buf;
while (getline(infile, buf)) {
if (flag == true && buf.size() != 0) {
i++;
int max = buf.size();
if (buf[max - 2] == '*' && buf[max - 1] == '/') {
flag = false;
}
continue;
}
if (flag == false) {
if (buf[0] == '/' && buf[1] == '*') {
flag = true;
i++;
continue;
}
}
if (buf[0] == '/' && buf[1] == '/') {
i++;
continue;
}
}
infile.close();
return i;
}
五.代码说明
代码的思路很简单
首先在主函数中处理命令行输入的参数 然后根据不同的参数来调用不同的处理函数
将不同的处理函数的返回值拼接成字符串后写入输出文件中
六.测试设计
//测试字符数
void test1(string name){
cout<<name<<"中的字符数为:"<<character(name)<<endl;
}
//测试单词数
void test2(string name){
cout<<name<<"中的单词数为:"<<word(name)<<endl;
}
//测试行数
void test3(string name){
cout<<name<<"中的行数为:"<<line(name)<<endl;
}
//测试代码行数
void test4(string name){
cout<<name<<"中的代码行数为:"<<codeLine(name)<<endl;
}
//测试注释行数
void test5(string name){
cout<<name<<"中的注释行数为:"<<nodeLine(name)<<endl;
}
//测试空行数
void test6(string name){
cout<<name<<"中的空行数为:"<<blankLine(name)<<endl;
}
//测试递归处理文件
void test7(string filePath){
vector<string> files;
getFiles("./", files);
int size = files.size();
for (int i = 0; i < size; i++)
{
int max = files[i].size();
if (files[i][max - 2] == '.' && files[i][max - 1] == 'c') {
sourceName = files[i];
if (o == false) {
outputName = "result.txt";
}
if (c) {
ret += sourceName + ',' + "字符数:" + to_string(character(sourceName)) + '\n';
}
if (w) {
ret += sourceName + ',' + "单词数:" + to_string(word(sourceName)) + '\n';
}
if (l) {
ret += sourceName + ',' + "行数:" + to_string(line(sourceName)) + '\n';
}
if (a) {
ret += sourceName + ',' + "代码行/空行/注释行:" + to_string(codeLine(sourceName)) + '/' + to_string(blankLine(sourceName)) + '/' + to_string(noteLine(sourceName)) + '\n';
}
}
}
cout<<ret<<endl;
}
//测试文件写入
void outputFileTest(string outputName,string sourceName){
ofstream file;
file.open("outputName");
ret += sourceName + ',' + "字符数:" + to_string(character(sourceName)) + '\n';
ret += sourceName + ',' + "单词数:" + to_string(word(sourceName)) + '\n';
ret += sourceName + ',' + "行数:" + to_string(line(sourceName)) + '\n';
ret += sourceName + ',' + "代码行/空行/注释行:" + to_string(codeLine(sourceName)) + '/' + to_string(blankLine(sourceName)) + '/' + to_string(noteLine(sourceName)) + '\n';
outfile.write(ret.c_str(), ret.length());
outfile.close();
}
浙公网安备 33010602011771号