项目要求
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 返回更复杂的数据(代码行 / 空行 / 注释行)。(已完成)
我的github项目地址
由于怎么搞都搞不上github,所以只好将代码放在CSDN
附上链接:https://blog.csdn.net/qq_43584683/article/details/104888382
遇到的困难及其解决方法
遇到的困难:学Python已经很久了,有很多知识已经忘记
解决方法:将之前的书复习了一遍
收获:将Python复习了一遍
关键代码:
获取行数:
1 def get_lines(file_path): 2 count = 0 3 f = open(file_path, 'r', encoding='utf-8') 4 for line in f.readlines(): 5 count = count + 1 6 f.close() 7 return count
获取字符数:
1 def get_chars(file_path): 2 f = open(file_path, 'r',encoding="UTF-8") 3 count = sum([len(word) for line in f for word in line.split()]) 4 f.close() 5 return count
获取词的数目:
1 def get_words(file_path): 2 f = open(file_path,'r', encoding="UTF-8") 3 count = len(re.split(r'[^a-zA-Z]+', f.read())) - 1 4 f.close() 5 return count;
获取更复杂的数据(代码行 / 空行 / 注释行):
1 def get_other(file_path): 2 code_line = 0 3 none_line = 0 4 tip_line = 0 5 f = open(file_path, 'r', encoding='utf-8') 6 for line in f.readlines(): 7 if not line.split(): 8 none_line = none_line + 1 9 continue 10 elif (line[0:2] == '//'): 11 tip_line = tip_line + 1 12 continue 13 code_line = code_line + 1 14 15 f.close() 16 return [code_line, none_line, tip_line] 17 // 返回一个列表
主程序:
1 if __name__ == '__main__': 2 Input = input("请输入命令(命令说明可以查找wc.exe -h):") 3 Ipt = Input.split() 4 if Ipt[0] == 'wc.exe': 5 if Ipt[1] == '-h': 6 get_help() 7 if len(Ipt) == 3: 8 if Ipt[1] == '-c': 9 count = get_chars(Ipt[-1]) 10 print("代码行数为:", count) 11 elif Ipt[1] == '-w': 12 count = get_words(Ipt[-1]) 13 print("代码单词为:", count) 14 elif Ipt[1] == '-l': 15 count = get_lines(Ipt[-1]) 16 print("代码行数为:", count) 17 elif Ipt[1] == '-a': 18 tru = get_other(Ipt[-1]) 19 print("代码行数为:", tru[0]) 20 print("空行数为:", tru[1]) 21 print("注释行数为:", tru[2]) 22 else: 23 print("Error") 24 if len(Ipt) == 4: 25 files = os.listdir(Ipt[-1]) 26 if Ipt[1] == '-s': 27 if Ipt[2] == '-c': 28 char_sums = 0; 29 for file in files: 30 if file[(len(file) - 2):len(file)] == ".c": 31 f = Ipt[-1] + '/' + file 32 char_sum = get_chars(f) 33 char_sums += char_sum 34 print("这个目录下的文件的字符数:",char_sums) 35 elif Ipt[2] == '-w': 36 word_sums = 0; 37 for file in files: 38 if file[(len(file) - 2):len(file)] == ".c": 39 f = Ipt[-1] + '/' + file 40 word_sum = get_words(f) 41 word_sums += word_sum 42 print("这个目录下的文件的单词数:" ,word_sums) 43 elif Ipt[2] == '-l': 44 line_sums = 0; 45 for file in files: 46 if file[(len(file) - 2):len(file)] == ".c": 47 f = Ipt[-1] + '/' + file 48 line_sum = get_lines(f) 49 line_sums += line_sum 50 print("这个目录下的文件的行数:",line_sums) 51 elif Ipt[2] == '-a': 52 other_sums = [0,0,0]; 53 for file in files: 54 if file[(len(file) - 2):len(file)] == ".c": 55 f = Ipt[-1] + '/' + file 56 other_sum = get_other(f) 57 other_sums[0] += other_sum[0] 58 other_sums[1] += other_sum[1] 59 other_sums[2] += other_sum[2] 60 print("代码行数为:", other_sums[0]) 61 print("空行数为:", other_sums[1]) 62 print("注释行数为:", other_sums[2]) 63 else: 64 print("Error") 65 if Ipt[2] == '-s': 66 if Ipt[1] == '-c': 67 char_sums = 0; 68 for file in files: 69 if file[(len(file) - 2):len(file)] == ".c": 70 f = Ipt[-1] + '/' + file 71 char_sum = get_chars(f) 72 char_sums += char_sum 73 print("这个目录下的文件的字符数:",char_sums) 74 elif Ipt[1] == '-w': 75 word_sums = 0; 76 for file in files: 77 if file[(len(file) - 2):len(file)] == ".c": 78 f = Ipt[-1] + '/' + file 79 word_sum = get_words(f) 80 word_sums += word_sum 81 print("这个目录下的文件的单词数:" ,word_sums) 82 elif Ipt[1] == '-l': 83 line_sums = 0; 84 for file in files: 85 if file[(len(file) - 2):len(file)] == ".c": 86 f = Ipt[-1] + '/' + file 87 line_sum = get_lines(f) 88 line_sums += line_sum 89 print("这个目录下的文件的行数:",line_sums) 90 elif Ipt[1] == '-a': 91 other_sums = [0,0,0]; 92 for file in files: 93 if file[(len(file) - 2):len(file)] == ".c": 94 f = Ipt[-1] + '/' + file 95 other_sum = get_other(f) 96 other_sums[0] += other_sum[0] 97 other_sums[1] += other_sum[1] 98 other_sums[2] += other_sum[2] 99 print("代码行数为:", other_sums[0]) 100 print("空行数为:", other_sums[1]) 101 print("注释行数为:", other_sums[2]) 102 else: 103 print("Error") 104 else: 105 print("Error") 106 input("Press any key to exit...")
测试实例
放了两个文件,分别为:
测试
wc.exe -h 帮助
获取文件的行数
获取文件的字符数
获取文件的词数
获取目录中文件的行数
获取目录中文件的词数
获取其他信息
PSP2.1
SP2.1 |
Personal Software Process Stages |
预估耗时(分钟) |
实际耗时(分钟) |
Planning |
计划 |
5 |
30 |
· Estimate |
· 估计这个任务需要多少时间 |
1 |
2 |
Development |
开发 |
300 |
500 |
· Analysis |
· 需求分析 (包括学习新技术) |
50 |
60 |
· Design Spec |
· 生成设计文档 |
20 |
30 |
· Design Review |
· 设计复审 (和同事审核设计文档) |
30 |
10 |
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
10 |
10 |
· Design |
· 具体设计 |
20 |
30 |
· Coding |
· 具体编码 |
20 |
30 |
· Code Review |
· 代码复审 |
20 |
10 |
· Test |
· 测试(自我测试,修改代码,提交修改) |
20 |
50 |
Reporting |
报告 |
20 |
20 |
· Test Report |
· 测试报告 |
30 |
20 |
· Size Measurement |
· 计算工作量 |
10 |
15 |
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
10 |
20 |
合计 |
566 |
855 |
项目小结
这是我第一次做项目有很多东西都不懂,之前的知识也忘了很多,所以这个项目的开展异常的困难。
但是通过做这一次的项目我感觉自己学会了很多的东西,还捡起了以往的知识。
往后有项目要做会越来越好的。