Wordcount

Github项目地址: https://github.com/yigerens/wordcount

1、PSP表格

PSP2.1表格

PSP2.1

PSP阶段

预估耗时

(分钟)

实际耗时

(分钟)

Planning

计划

 30  30

· Estimate

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

40  30

Development

开发

600  650

· Analysis

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

 20  10

· Design Spec

· 生成设计文档

 20  10

· Design Review

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

 120  130

· Coding Standard

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

 120  130

· Design

· 具体设计

 30  20

· Coding

· 具体编码

 300  300

· Code Review

· 代码复审

 30  30

· Test

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

 30  30

Reporting

报告

 30  30

· Test Report

· 测试报告

 20  30

· Size Measurement

· 计算工作量

 30  30

· Postmortem & Process Improvement Plan

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

 30  30
 

合计

   

2、解题思路  

拿到题目后看到要求是对程序设计语言源文件统计字符数、单词数、行数,统计结果以指定格式输出到默认文件中,以及其他扩展功能,并能够快速地处理多个文件。想到的就是正则文法来匹配单词,使用的是c来处理这个项目,发现Java生成的不是exe文件,而现在要生成exe文件,还好在老师的文档要求里找到里对应的农科有解决方法。

3、代码说明:

(1)字符数、单词数和行数

复制代码
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);
复制代码

 

单词的划分:由空格或逗号分割开的都视为单词,且不做单词的有效性校验,例如:thi#,that视为用逗号隔开的2个单词。 (2)扩展功能  

复制代码
 //统计代码行/空行/注释行
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++;  
              }  
复制代码

 

   

复制代码
//统计使用stop表后的的单词总数目
//调用停用词表,重写统计单词数
        ArrayList<String> stop=new ArrayList<String>(3);        
//     读入stopfile.txt的单词并放进入到一个动态string数组中保存,以便于后面遍历    
                String line=new String("");
                StringBuffer sb=new StringBuffer();
                 TreeMap<String, Integer> map = new TreeMap<>();
                String[] split =null;
                while((line=br.readLine())!=null){
                     sb.append(line);
                    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);  
                        } } }         
                Set<String> keySet = map.keySet();  
                for (String string : keySet) {  
                    stop.add(string);    
                }
//遍历要统计的文件,进行统计各单词的总数
TreeMap<String, Integer> map = new TreeMap<>();
                while((line=br.readLine())!=null){
                    String[] split = line.split("\\s++|\\.|,|\\;|\\(|\\)|\\[|\\]|\\<|\\>|\\=|\\-|\\+|\\*|\\/|\\{|\\}|\\_");  //去除多个空格\\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) {
                    int i=0;
                    if(!(string.equals(""))){//去掉空格符
                    wordcount+=map.get(string);//统计单词总数
               while(i<stop.size()){//遍历”停用词表”
                       if(string.equalsIgnoreCase(stop.get(i++)))//不区分大小写判断
                   {
                     stopcount+=map.get(string);//统计停用词在该文件中的总数
                       //System.out.println(string+":"+map.get(string));  
                       }} }
复制代码

代码来源:1504成建伟 

4.测试设计过程

采用白盒测试

测试用例:

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

 

posted @ 2018-03-22 20:31  meizhichao  阅读(204)  评论(1)    收藏  举报