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 的行数(实现)拓展功能:
} //注释wc.exe -s -a *.c
https://github.com/06linxi/wordcount
#判断是否是注释行
def isCmt(line, multiCmtFlagIdx):
singleCmtFlag = ‘//‘ # 单行注释符号
startCmtFlag = "/*"
endCmtFlag = "*/"
isCmtRet = True
# print ‘line: ‘ + line.strip()
if multiCmtFlagIdx == False: # 不在多行注释中
# 单行注释
# re.match(pattern, string, flags=0),匹配开头,\s是任意空白字符
if re.match(r‘(\s)*‘ + singleCmtFlag, line):
return isCmtRet, multiCmtFlagIdx
# 多行注释开始符号
if startCmtFlag in line: # 找到多行注释开始符号
if endCmtFlag in line:
multiCmtFlagIdx = False
return isCmtRet, multiCmtFlagIdx
return isCmtRet, multiCmtFlagIdx
else:
# 没有找到多行注释开始符,继续查找下个类型的符号
multiCmtFlagIdx = False
isCmtRet = False
return isCmtRet, multiCmtFlagIdx
else: # 在多行注释中
# 多行注释开始符
if endCmtFlag in line:
multiCmtFlagIdx = False
# print isCmtRet, multiCmtFlagIdx
return isCmtRet, multiCmtFlagIdx # 返回是否注释行,以及当前是否在多行注释中
#相关计算
def get_count(data):
chars = len(data)
words = len(data.split())
lines = data.count(‘\n‘)
return lines, words, chars
测试运行




| PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
|---|---|---|---|
| Planning | 计划 | 600 | 600 |
| · Estimate | · 估计这个任务需要多少时间 | 600 | 600 |
| Development | 开发 | 540 | 540 |
| · Analysis | · 需求分析 (包括学习新技术) | 60 | 60 |
| · Design Spec | · 生成设计文档 | 30 | 30 |
| · Design Review | · 设计复审 (和同事审核设计文档) | 30 | 30 |
| · Coding Standard | · 代码规范 (为目前的开发制定合适的规范) | 30 | 30 |
| · Design | · 具体设计 | 30 | 30 |
| · Coding | · 具体编码 | 300 | 300 |
| · Code Review | · 代码复审 | 30 | 30 |
| · Test | · 测试(自我测试,修改代码,提交修改) | 30 | 30 |
| Reporting | 报告 | 60 | 60 |
| · Test Report | · 测试报告 | 40 | 40 |
| · Size Measurement | · 计算工作量 | 10 | 10 |
| · Postmortem & Process Improvement Plan | · 事后总结, 并提出过程改进计划 | 10 | 10 |
| 合计 | 600 | 600 |
原文:https://www.cnblogs.com/5185jim/p/9642523.html