Github:https://github.com/qywxc/wc.exe
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 的行数 (实现)
扩展功能:
-s 递归处理目录下符合条件的文件(未实现)
-a 返回更复杂的数据 (未实现)
| PSP | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
| Planning | 计划 | 30 | 40 |
| Estimate | 估计这个任务需要多少时间 | 15 | 20 |
| Development | 开发 | 400 | 500 |
| Analysis | 需求分析(包括学习新技术) | 150 | 180 |
| Design Spec | 设计生成文档 | 10 | 20 |
| Design Review | 设计复审 (和同事审核设计文档) | 5 | 10 |
| Coding Standard | 代码规范 (为目前的开发制定合适的规范) | 20 | 30 |
| Design | 具体设计 | 70 | 120 |
| Coding | 具体编码 | 300 | 420 |
| Code Review | 代码复审 | 20 | 50 |
| Test | 测试(自我测试,修改代码,提交修改) | 60 | 90 |
| Reporting | 报告 | 40 | 70 |
| Test Report | 测试报告 | 25 | 40 |
| Size Measurement | 计算工作量 | 10 | 15 |
| Postmortem & Process Improvement Plan | 事后总结,并提出新计划 | 20 | 20 |
| Total | 总计 | 1175 | 1625 |
统计字符数函数代码:
int CharCount(char file[]){//字符数统计函数
FILE *pf=NULL;
int ccount=0;
pf=fopen(file,"r");
if(pf==NULL){
printf("寻找文件失败\n");
exit(-1);
}
char mychar;
mychar = fgetc(pf);
while(mychar!=EOF){
mychar = fgetc(pf);
ccount++;
}
fclose(pf);
return ccount;
}
统计行数函数代码:
int LineCount(char file[]){//行数统计函数
FILE *pf=NULL;
int lcount=0;
pf=fopen(file,"r");
if(pf==NULL){
printf("寻找文件失败\n");
exit(-1);
}
char mychar;
mychar = fgetc(pf);
while(mychar!=EOF){
if(mychar==‘\n‘){
lcount++;
mychar = fgetc(pf);
}
else{
mychar = fgetc(pf);
}
}
fclose(pf);
return lcount+1;
}
统计单词数函数代码:
int WordCount(char file[]){//单词数统计函数
FILE *pf=NULL;
int wcount=0;
pf=fopen(file,"r");
if(pf==NULL){
printf("寻找文件失败\n");
exit(-1);
}
char mychar;
mychar = fgetc(pf);
while(mychar!=EOF){
if(mychar>=‘a‘&&mychar<=‘z‘||mychar>=‘A‘&&mychar<=‘Z‘||mychar>=‘0‘&&mychar<=‘9‘){
while(mychar>=‘a‘&&mychar<=‘z‘||mychar>=‘A‘&&mychar<=‘Z‘||mychar>=‘0‘&&mychar<=‘9‘||mychar==‘_‘){
mychar=fgetc(pf);
}
wcount++;
mychar=fgetc(pf);
}
mychar=fgetc(pf);
}
fclose(pf);
return wcount;
}
主函数代码:
int main(){//主函数
char input[10],File[200];
while(1){
printf("请输入用户命令:wc.exe-");
scanf("%s",&input);
if(input[0]==‘c‘){
printf("请输入文件名:");
scanf("%s",&File);
int charcount=0;
charcount=CharCount(File);
printf("文件的字符数为:%d\n",charcount);
continue;
}
if(input[0]==‘w‘){
printf("请输入文件名:");
scanf("%s",&File);
int wordcount=0;
wordcount=WordCount(File);
printf("文件的词数为:%d\n",wordcount);
continue;
}
if(input[0]==‘l‘){
printf("请输入文件名:");
scanf("%s",&File);
int linecount=0;
linecount=LineCount(File);
printf("文件的行数为:%d\n",linecount);
continue;
}
if(input[0]==‘a‘){
printf("请输入文件名:");
scanf("%s",&File);
ComplexCount(File);
continue;
}
}
system("pause");
return 0;
}
测试结果:


1、github使用不熟练。
2、只完成了基本功能,高级功能的注释行和代码行的计数算法尚未解决,空行的计数方法存在缺陷。
3、高级语言学习匮乏。
原文:https://www.cnblogs.com/qywxc/p/12563588.html