JAVA实现WC
github地址:https://github.com/shoulder01/wc.git
一、项目相关要求
wc.exe 是一个常见的工具,它能统计文本文件的字符数、单词数和行数。这个项目要求写一个命令行程序,模仿已有wc.exe 的功能,并加以扩充,给出某程序设计语言源文件的字符数、单词数和行数。实现一个统计程序,它能正确统计程序文件中的字符数、单词数、行数,以及还具备其他扩展功能,并能够快速地处理多个文件。
二、功能实现
基本功能
计算文件的字符数 (实现)
计算文件的词的数目 (实现)
计算文件 的行数 (实现)
扩展功能:
统计文件代码行 (实现)
统计文件 空行 (实现)
统计文件注释行 (实现)
未实现的要求:分别用参数命令返回各结果
三、psp表格
PSP2.1 |
Personal Software Process Stages |
预估耗时(分钟) |
实际耗时(分钟) |
Planning |
计划 |
30 |
40 |
· Estimate |
· 估计这个任务需要多少时间 |
60 |
50 |
Development |
开发 |
150 |
180 |
· Analysis |
· 需求分析 (包括学习新技术) |
100 |
120 |
· Design Spec |
· 生成设计文档 |
40 |
50 |
· Design Review |
· 设计复审 (和同事审核设计文档) |
30 |
30 |
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
40 |
40 |
· Design |
· 具体设计 |
60 |
60 |
· Coding |
· 具体编码 |
250 |
300 |
· Code Review |
· 代码复审 |
50 |
90 |
· Test |
· 测试(自我测试,修改代码,提交修改) |
80 |
120 |
Reporting |
报告 |
150 |
150 |
· Test Report |
· 测试报告 |
30 |
30 |
· Size Measurement |
· 计算工作量 |
40 |
50 |
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
150 |
190 |
合计 |
|
1260 |
1500 |
四、代码说明
1、计算文件字符数,行数,单词数
private static void listNext(File file) throws IOException { InputStreamReader isr=new InputStreamReader(new FileInputStream(file)); BufferedReader br=new BufferedReader(isr); /** * 计算文件字符数,行数,单词数 */ while((br.read())!=-1) { String s=br.readLine(); charNum+=s.length(); wordsNum+=s.split(" ").length; lineNum++; } isr.close(); }
2、统计文件代码行,注释行,空行
1 private static void fileLine(File f) throws IOException{ 2 String strLine = ""; 3 String str = fromFile(f); 4 if (str.length() > 0) { 5 while (str.indexOf('\n') != -1) { 6 7 strLine = str.substring(0, str.indexOf('\n')).trim(); 8 if (strLine.charAt(0) == '/' || strLine.charAt(0) == '*') { 9 commentsNum++; 10 }else{ 11 sourceNum++; 12 String regEx = ".*\\/\\/.*"; 13 if(regEx(strLine,regEx)){ 14 commentsNum++; 15 } 16 else if (strLine.length() == 0 ) { 17 blankNum++; 18 } 19 } 20 str = str.substring(str.indexOf('\n') + 1, str.length()); 21 22 } 23 } 24 }
3、主函数
1 public static void main(String[] args) throws IOException { 2 File directory = new File(""); 3 Scanner input=new Scanner(System.in); 4 System.out.println("请输入文件路径:"); 5 String path=input.next(); 6 listNext(new File(path)); 7 fileLine(new File(path)); 8 fromFile(new File(path)); 9 System.out.println("总行数:"+lineNum); 10 System.out.println("字符数:"+charNum); 11 System.out.println("单词数:"+wordsNum); 12 System.out.println("代码行数:"+sourceNum); 13 System.out.println("空白行数:"+blankNum); 14 System.out.println("注释行数:"+commentsNum); 15 }
五、测试结果
六、总结
接到这个作业的第一反应就是“好难,不会”,可能这和自己对java和c语言不是很熟悉,因为慢慢对打代码失去了兴趣。不过作业还是要完成,只能硬着头皮上。其间查找了很多的例子去看,也找了书本温习之前学过的知识。过程在也得到了同学的帮助,才勉强完成了不分要求。虽然没能把这次作业完成得很好,但自己起码去做了,多少是有收获的。这次作业也让自己知道自己真的要把java和c语言捡起来,逼自己学,能学多少是多少。往后也要多向同学们学习。