一、项目要求:
基本功能列表:
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
二、PSP
PSPPersonal Software Process Stages预估耗时(分钟)实际耗时(分钟)
Planning
计划
70
140
· Estimate
估计这个任务需要多少时间
30
40
Development
开发
150
180
Analysis
需求分析 (包括学习新技术)
50
120
·Design Spec
生成设计文档
20
40
·Design Review
设计复审 (和同事审核设计文档)
0
0
· Coding Standard
代码规范 (为目前的开发制定合适的规范)
20
20
·Design
具体设计
50
90
·Coding
具体编码
100
120
·Code Review
代码复审
50
70
·Test
测试(自我测试,修改代码,提交修改)
20
15
Reporting
报告
20
30
·Test Report
测试报告
20
20
·Size Measurement
计算工作量
40
60
·Postmortem & Process Improvement Plan
事后总结, 并提出过程改进计划
20
40
合计
640
985
三、代码实现:
源代码:
#-*- coding:utf-8 -*-
import os
import re
import sys
import glob
def count_char_number(input): #统计字符个数
content = ""
with open(input,'r') as rea:
c = rea.readlines()
for i in c:
i = i.strip()
if len(i) <= 0:
continue
content += i
return len(content)
def count_word_number(input): #统计单词字数
with open(input,'r') as rea:
content = rea.read()
content = content.strip()
c_r_word_list = [r'\o',r'\n',r'\r',r'\t',r'\v',r'\a',r'\b',r'\f',r'\ddd',r'\xhh']
for w in c_r_word_list:
content = content.replace(w,'')
c_p_word_list = [r'%d',r'%o',r'%x',r'%u',r'%c',r'%s',r'%f',r'%e',r'%g']
for w in c_p_word_list:
content = content.replace(w,'')
#只留非特殊字符
content = re.findall('[a-zA-Z]+', content, re.S)
# for c in content:
# print(33,c)
return len(content)
def count_line_number(input): #统计行数
n = 0
with open(input,'r') as rea:
content = rea.readlines()
return len(content)
return_file_paths = []
def get_files(file_path): #递归获取目录以及子目录
root_path = file_path
files = os.listdir(root_path)
for f in files:
_file_path = os.path.join(root_path,f)
if os.path.isdir(_file_path):
get_files(_file_path)
elif os.path.isfile(_file_path):
appendix = _file_path.split('.')[-1]
if appendix == 'c':
return_file_paths.append(_file_path)
return return_file_paths
def count_complex_number(input): #获取空行,注释,代码行
blank = 0
code = 0
note = 0
with open(input,'r') as rea:
content = rea.readlines()
is_note = False
for line in content:
line = line.replace(' ','').strip()
if line == "":
blank += 1
elif len(line) <= 1 and (line == '{' or line == '}'):
blank += 1
elif '//' in line :
note += 1
elif '/*' in line:
is_note = True
note+=1
elif '*/' in line:
is_note = False
note +=1
elif is_note is True:
note += 1
else:
code += 1
return blank,code,note
def main(): #主函数
_get_char_number = None
_get_word_number = None
_get_line_number = None
_get_complex_number= None
args = sys.argv
reverse = False
if len(args) == 3:
if '-c' in args:
_get_char_number = str(args[-1])
elif '-w' in args:
_get_word_number = str(args[-1])
elif '-l' in args:
_get_line_number = str(args[-1])
elif '-a' in args:
_get_complex_number = str(args[-1])
elif len(args) == 4:
if '-c' in args and '-s' in args:
_get_char_number = str(args[-1])
reverse = True
elif '-w' in args and '-s' in args:
_get_word_number = str(args[-1])
reverse = True
elif '-l' in args and '-s' in args:
_get_line_number = str(args[-1])
reverse = True
elif '-a' in args and '-s' in args:
_get_complex_number = str(args[-1])