Github地址:

https://github.com/hddddd/Wordcount

1.PSP表格

PSP2.1

PSP阶段

预估耗时(分钟)

实际耗时(分钟)

Planning

计划

30

45

· Estimate

· 估计这个任务需要多少时间

15

20

Development

开发

1000

900

· Analysis

· 需求分析 (包括学习新技术)

150

200

· Design Spec

· 生成设计文档

30

45

· Design Review

· 设计复审 (和同事审核设计文档)

45

60

· Coding Standard

· 代码规范 (为目前的开发制定合适的规范)

10

10

· Design

· 具体设计

70

40

· Coding

· 具体编码

700

900

· Code Review

· 代码复审

150

200

· Test

· 测试(自我测试,修改代码,提交修改)

300

100

Reporting

报告

150

90

· Test Report

· 测试报告

50

40

· Size Measurement

· 计算工作量

10

20

· Postmortem & Process Improvement Plan

· 事后总结, 并提出过程改进计划

30

30

 

合计

2800

 

 

3000

 

 

2、解题思路

  逐行读取文件代码,通过正则表达式逐个处理最后相加。

 

3.程序设计实现过程

本项目采用JAVA语言实现,除主函数外,还有多个自定义函数共同组成。

 

4、关键代码:

此处引用成建伟同学的代码。

字符数、单词数和行数

复制代码
while((line=br.readLine())!=null)
                {    linecount++;
                    sb.append(line);
                    charcount+=line.length();
                     String[] split = line.split("\\s++|\\.|,|\\;|\\(|\\)|\\[|\\]|\\<|\\>|\\=|\\-|\\+|\\*|\\/|\\{|\\}");  
//设置单词划分的要求
                     for (int i = 0; i < split.length; i++) {
//                       获取到每一个单词  
                         Integer integer = map.get(split[i]);  
//                       如果这个单词在map中没有,赋值1  
                         if(null==integer){  
                             map.put(split[i], 1);  
                         }else{ // 如果有,在原来的个数上加上一  
                             map.put(split[i], ++integer);  
                         }  
                     }  
                 }
//               遍历,根据key获取所对应的value  
                 Set<String> keySet = map.keySet();  
                 for (String string : keySet)
                     if(!(string.equals("")))//测试时候发现,去除不了多个空格的要求
                     wordcount+=map.get(string);
复制代码

此处引用成建伟同学的代码。

扩展功能
 

复制代码
 //统计代码行/空行/注释行
while ((line = br.readLine()) != null) {  
              line = line.trim();  
         if (line.matches("^[//s&&[^//n]]*$")||line.equals("{")||line.equals("}")) {  
           /* 空行 :本行全部是空格或格式控制字符,如果包括代码,则只有不超过一个可显示的字符,例如“{”*/
                  whiteLines++;      
              }
             /* 本行不是代码行,并且本行包括注释。一个有趣的例子是有些程序员会在单字符后面加注释:
              *  }//注释
              */
           else if (line.startsWith("/*") && !line.endsWith("*/")||
((line.startsWith("{/*")||line.startsWith("}/*"))&&!line.endsWith("*/"))){
                  // 判断此行为"/*"开头的注释行  
                  commentLines++;  
                  comment = true;  
              } else if (comment == true && !line.endsWith("*/")
&&!line.startsWith("*/")) {  
                  // 为多行注释中的一行(不是开头和结尾)
                  notLine++;//虚假的注释行计数
                  commentLines++;  
              } else if (comment == true && (line.endsWith("*/")||line.startsWith("*/"))) {  
                  // 为多行注释的结束行  
                  commentLines++;  
                  comment = false;  
              } else if (line.startsWith("//")|| line.startsWith("}//")||line.startsWith("{//")||
                          ((line.startsWith("{/*") ||line.startsWith("}/*")||line.startsWith("/*")) && line.endsWith("*/"))) {  
                  // 单行注释行  
                  commentLines++;  
              } else {  // 正常代码行  
                  normalLines++;  
              }  
复制代码

  

5.测试设计过程:

在wc.exe的目录下放置测试文件,分别输入以下命令:

1. wc.exe -w -c -l test.c

2. wc.exe -w -c D:\test\test.c

3,wc.exe -w test.c -e stoplist.txt -o out.txt

4. wc.exe -a -c -l -w test.c -e stoplist.txt

5. wc.exe -a -c test.c -o out.txt

6. wc.exe -s -w test.c -e stoplist.txt

7. wc.exe -w -l D:\test\test.c -o out.txt

8.wc.exe -a -l -c -w test.c

9.wc.exe -l test.c -e stoplist.txt

10. wc.exe -s -w test.c -o out.txt

得到相应的测试结果。

6.心得体会:

本次作业量较大,但对自己的能力也是一种锻炼,学到了许多东西,受益匪浅,希望在以后的学习生活中能够继续努力。

参考文献链接:

http://www.cnblogs.com/jakejian/p/8613002.html

http://www.cnblogs.com/xinz/archive/2011/10/22/2220872.html

 

 

posted on 2018-03-22 19:40  侯岱  阅读(187)  评论(1编辑  收藏  举报