wc.exe 是一个常见的工具,它能统计文本文件的字符数、单词数和行数。这个项目要求写一个命令行程序,模仿已有wc.exe 的功能,并加以扩充,给出某程序设计语言源文件的字符数、单词数和行数。
实现一个统计程序,它能正确统计程序文件中的字符数、单词数、行数,以及还具备其他扩展功能,并能够快速地处理多个文件。
具体功能要求:
程序处理用户需求的模式为:
wc.exe [parameter] [file_name]
基本功能列表:
wc.exe -c file.c //返回文件 file.c 的字符数
wc.exe -w file.c //返回文件 file.c 的词的数目
wc.exe -l file.c //返回文件 file.c 的行数
扩展功能:
高级功能:
-x 参数。这个参数单独使用。如果命令行有这个参数,则程序会显示图形界面,用户可以通过界面选取单个文件,程序就会显示文件的字符数、行数等全部统计信息。
需求举例:
wc.exe -s -a *.c
返回当前目录及子目录中所有*.c 文件的代码行数、空行数、注释行数。
用谷歌引擎查阅相关的资料,查看一些别人分享的博客。
解决了,最后确定用C语言,再一步步查阅资料,慢慢debug,做出了基本功能和拓展功能。
第一次一个人从头开始做出一个小型程序,也是第一次用C语言做命令行程序,以前不太喜欢C语言,感觉面向过程的语言太麻烦了,相比较之下还是喜欢面向对象语言,不过慢慢地也体会到了C语言的乐趣,感觉还是在于解决问题的思路,语言是一种辅助的工具,选择合适的语言有时候会方便很多。
//遍历文件的每一行,统计行数,字符数和词数
fgets(st,bufsize,fp);
res.line++;
int len=strlen(st);
for (int i=0;i<len;i++)
{
if (is_character(st[i]))
{
res.character++;
if ( i==0 || !is_character(st[i-1]) )
res.word++;
}
}
2.代码行与空行的判断
// 代码行与空行的判断
int show_char=0;
for (int i=0;i<len;i++)
{
if (st[i]!=‘ ‘ && st[i]!=‘\n‘ && st[i]!=‘\t‘)
show_char++;
}
if (show_char>1) res.code_line++;
else res.null_line++;
3.注释行的判断
// 注释行的判断
bool is_annotation_line=false;
show_char=0;
for (int i=0;i<len;i++)
{
if ((st[i]!=‘ ‘) && st[i]!=‘\n‘ && st[i]!=‘\t‘)
{
show_char++;
if (st[i]==‘/‘)
{
if (i+1<len && st[i+1]==‘/‘)
is_annotation_line=true;
break;
}
if (show_char>1) break;
}
}
if (is_annotation_line==true) res.annotation_line++;
4.递归处理目录下符合条件的文件
//递归处理目录下符合条件的文件
void listFiles(string dir,Topt nowopt)
{
intptr_t handle;
_finddata_t findData;
handle = _findfirst((dir+"\\*.*").c_str(), &findData); // 查找目录中的第一个文件
if (handle == -1)
{
cout << "Failed to find first file!\n";
return;
}
do
{
string nextdir=dir+‘/‘+string(findData.name);
if (strcmp(findData.name, ".") == 0) continue;
if (strcmp(findData.name, "..") == 0) continue;
if (findData.attrib & _A_SUBDIR )
{
listFiles(nextdir,nowopt);
}
else
{
printf("the file is %s\n",(nextdir).c_str());
int len=nextdir.size();
char filename[len+1];
nextdir.copy(filename, len, 0);//这里5代表复制几个字符,0代表复制的位置,
filename[len]=‘\0‘;
work(filename,nowopt);
printf("-------------\n");
}
} while (_findnext(handle, &findData) == 0); // 查找目录中的下一个文件
_findclose(handle); // 关闭搜索句柄
}
PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 430 | 680 |
Estimate | 估计这个任务需要多少时间 | 30 | 30 |
Development | 开发 | 400 | 650 |
Analysis | 需求分析 (包括学习新技术) | 150 | 250 |
Design Spec | 生成设计文档 | 20 | 40 |
Design Review | 设计复审 (和同事审核设计文档) | 20 | 30 |
Coding Standard | 代码规范 (为目前的开发制定合适的规范)划 | 10 | 30 |
Design | 具体设计 | 30 | 40 |
Code Review | 代码复审 | 30 | 60 |
Test | 测试(自我测试,修改代码,提交修改) | 30 | 50 |
PReporting | 报告 | 110 | 150 |
Test Report | 测试报告 | 30 | 60 |
Size Measurement | 计算工作量 | 20 | 20 |
Postmortem & Process Improvement Plan | 事后总结, 并提出过程改进计划 | 60 | 70 |
合计 | 计划 | 430 | 680 |
原文:https://www.cnblogs.com/Yanzery/p/9648231.html