源程序特征统计程序
一、项目相关要求
- 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 的行数
二、关键代码
// 字符数
void getC(FILE *f)
{
int letter = 0, num = 0;
while (!feof(f)) {
letter = fgetc(f);
if (letter == '\n') {
continue;
}
num++;
}
printf("\n【字符数】: %d\n", num - 1);
}
// 词数
void getW(FILE *f)
{
int letter = 0, word = 0;
int mark = 0;
while (!feof(f)) {
letter = fgetc(f);
if ((letter >= 'a'&&letter <= 'z') || (letter >= 'A'&&letter <= 'Z') || letter == '_') {
if (mark == 0)
mark = 1;
} else if (letter == '.') {
continue;
} else {
if (mark == 1) {
word++;
mark = 0;
} else {
continue;
}
}
}
printf("\n【词数】: %d\n", word);
}
// 行数
void getL(FILE *f)
{
int letter = 0, line = 0;
int mark = 0;
while (!feof(f)) {
letter = fgetc(f);
if (letter == '\n') {
line++;
}
}
printf("\n【行数】: %d\n", line);
}
// 主程序
int main(int argc, char *argv[]) {
char com;
FILE * fp = NULL;
char* func = (char*)malloc(sizeof(char) * 50);
if (argv[2] != NULL && argv[1] != NULL) {
fp = fopen(argv[2], "r");
func = argv[1];
com = func[1];
if (fp == NULL)
{
printf("\n【错误】: 文件打开失败\n");
exit(1);
}
} else {
printf("\n【错误】: 参数输入错误\n");
return 0;
}
if (com == 'c')
getC(fp);
else if (com == 'w')
getW(fp);
else if (com == 'l')
getL(fp);
else {
printf("\n【错误】: 错误输入\n");
return 0;
}
rewind(fp);
return 0;
}
三、测试
- 测试文件:
- 空文件: null.cpp
- 只有一个字符的文件: c.cpp
- 只有一个词的文件: w.cpp
- 只有一行的文件: l.cpp
- 一个典型的源文件: file.cpp
- 测试结果
![avatar]()
四、PSP
| PSP |
Personal Software Process Stages |
预估耗时(分钟) |
实际耗时(分钟) |
| Planning |
计划 |
10 |
15 |
| · Estimate |
· 估计这个任务需要多少时间 |
240 |
180 |
| Development |
开发 |
110 |
65 |
| · Analysis |
· 需求分析 (包括学习新技术) |
30 |
10 |
| · Design Spec |
· 生成设计文档 |
10 |
10 |
| · Design Review |
· 设计复审 (和同事审核设计文档) |
5 |
5 |
| · Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
10 |
5 |
| · Design |
· 具体设计 |
5 |
5 |
| · Coding |
· 具体编码 |
20 |
15 |
| · Code Review |
· 代码复审 |
10 |
5 |
| · Test |
· 测试(自我测试,修改代码,提交修改) |
20 |
10 |
| Reporting |
报告 |
30 |
40 |
| · Test Report |
· 测试报告 |
10 |
20 |
| · Size Measurement |
· 计算工作量 |
10 |
10 |
| · Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
10 |
10 |
| 合计 |
|
140 |
120 |