个人项目作业
项目地址:https://github.com/sunandsheep/SER
一、项目相关要求
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 返回更复杂的数据(代码行 / 空行 / 注释行)。
空行:本行全部是空格或格式控制字符,如果包括代码,则只有不超过一个可显示的字符,例如“{”。
代码行:本行包括多于一个字符的代码。
注释行:本行不是代码行,并且本行包括注释。一个有趣的例子是有些程序员会在单字符后面加注释:
} //注释
在这种情况下,这一行属于注释行。
[file_name]: 文件或目录名,可以处理一般通配符。
高级功能:
-x 参数。这个参数单独使用。如果命令行有这个参数,则程序会显示图形界面,用户可以通过界面选取单个文件,程序就会显示文件的字符数、行数等全部统计信息。
需求举例:
wc.exe -s -a *.c
返回当前目录及子目录中所有*.c 文件的代码行数、空行数、注释行数。
二、PSP2.1表格
| PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
| Planning | 计划 | 20 | 15 |
| · Estimate | · 估计这个任务需要多少时间 | ||
| Development | 开发 | ||
| · Analysis | · 需求分析 (包括学习新技术) | 50 | 60 |
| · Design Spec | · 生成设计文档 | ||
| · Design Review | · 设计复审 (和同事审核设计文档) | ||
| · Coding Standard | · 代码规范 (为目前的开发制定合适的规范) | ||
| · Design | · 具体设计 | 30 | 30 |
| · Coding | · 具体编码 | 600 | 500 |
| · Code Review | · 代码复审 | 300 | 360 |
| · Test | · 测试(自我测试,修改代码,提交修改) | 180 | 180 |
| Reporting | 报告 | 120 | 180 |
| · Test Report | · 测试报告 | 30 | 20 |
| · Size Measurement | · 计算工作量 | ||
| · Postmortem & Process Improvement Plan | · 事后总结, 并提出过程改进计划 | 30 | 20 |
| 合计 | 1300 | 1365 |
三、解题思路
首先认真看完题目描述、项目要求,了解需求后将各个功能分成几个模块,然后思考各个模块如何实现。考虑到Java还不太熟悉,就选择用较为熟悉的c语言进行开发。思考过程中发现有不会的知识就记下来,去论坛认真学习,掌握新知识再开始敲代码。然后按要求进行测试。如时间充裕则考虑增加功能。
四、设计实现过程
先用三个函数实现基本功能,然后使用main函数进行传参、调用函数,实现功能。后来还有点时间就做了个统计空行的功能。
五、代码说明
1 #include<stdio.h> 2 #include<iostream> 3 using namespace std; 4 5 //字符统计函数 6 void charcount(FILE *file) 7 { 8 int charnum = 0; 9 char c; 10 c = fgetc(file); 11 while (c != EOF) 12 { 13 charnum++; 14 c = fgetc(file); 15 } 16 cout <<"The sum of char is:" << charnum << endl; 17 } 18 19 20 //词数统计函数 21 void wordcount(FILE *file) 22 { 23 int wordnum = 0; 24 char c,prc; 25 c = fgetc(file); 26 while (c != EOF) 27 { 28 prc = c; 29 c = fgetc(file); 30 if ((('a' <= prc && prc <= 'z') || ('A' <= prc && prc <= 'Z')) && (c < 'A' || ('Z' < c && c < 'a') || c > 'z')) 31 wordnum++; 32 } 33 cout << "The sum of word is:" << wordnum++ << endl; 34 35 } 36 37 38 //行数统计函数 39 void linecount(FILE *file) 40 { 41 int linenum = 0; 42 char c,prc; 43 if (file == NULL) 44 printf("reading error"); 45 c = fgetc(file); 46 while (c != EOF) 47 { 48 if (c == '\n') { 49 linenum++; 50 } 51 prc = c; 52 c = fgetc(file); 53 if (c == EOF ) 54 { 55 linenum++; 56 break; 57 } 58 } 59 cout << "The sum of line is:" << linenum << endl; 60 } 61 62 //空行统计函数 63 void emptylinecount(FILE* file) 64 { 65 int count = 0; 66 char c, prc; 67 c = fgetc(file); 68 if (c == '\n') 69 count++; 70 while(c != EOF) 71 { 72 prc = c; 73 c = fgetc(file); 74 if ((c == '\n' || c == EOF) && prc == '\n') 75 count++; 76 } 77 cout << "The sum of empty line if:" << count << endl; 78 } 79 80 int main(int argc,char* argv[]) 81 { 82 FILE *file; 83 char *file_name, order_name[10] = {'c','w','l','a'}; 84 char order; 85 int i = 0, tag = 0; 86 if (argc == 3) 87 { 88 file = fopen(argv[2], "r"); 89 order = *argv[1]; 90 91 while (file == NULL) { //文件名输入错误时可以重新输入 92 cout <<"reading error"<<endl; 93 cout << "Please input file name" << endl; 94 file_name = (char*)malloc(sizeof(char) * 20); 95 scanf("%s",file_name); 96 file = fopen(file_name, "r"); 97 } 98 99 for ( i = 0; order_name[i] != '\0'; i++) //检查输入的命令是否正确 100 { 101 if (order == order_name[i]) 102 { 103 tag = 1; 104 break; 105 } 106 } 107 108 while (tag == 0) //如果输入命令错误则重新输入 109 { 110 cout << "There's no such order,pleas input again" << endl; 111 cin >> order; 112 for ( i = 0; order_name[i] != '\0'; i++) 113 { 114 if (order == order_name[i]) 115 { 116 tag = 1; 117 break; 118 } 119 } 120 } 121 122 switch (order) //调用命令 123 { 124 case 'c': 125 charcount(file); 126 break; 127 case 'w': 128 wordcount(file); 129 break; 130 case 'l': 131 linecount(file); 132 break; 133 case 'a': 134 emptylinecount(file); 135 default: 136 break; 137 } 138 fclose(file); 139 140 } 141 142 else 143 cout << "Please input 3 parameter" << endl; 144 145 system("pause"); 146 return 0; 147 }
六、测试运行
测试用例:实际还做了空文件、一个单词等的测试,为节省篇幅这里只贴上测试典型c文件的图。

测试空行数:

字符数测试结果:

词数测试结果:

行数测试结果:

空行数统计结果:

七、项目小结
以前我基本没有按流程来开发程序,都是边想边做,导致效率较为低下。这次由于按照流程来开发程序,感觉思路清晰了不少,开发效率明显提高了。而且通过这个项目我稍微掌握了对文件夹的操作,算是提高了c语言的水平。由于事先对程序进行了设计,所以测试过程中没有出现多少bug,减轻了我的工作量。还有就是我终于学会了使用github,算是不小的收获了。

浙公网安备 33010602011771号