WordCount

github项目地址:https://cannotfindtheid.github.io/WordCount/

 


PSP表格如下

PSP2.1表格

PSP2.1

PSP阶段

预估耗时

(分钟)

实际耗时

(分钟)

Planning

计划

 30

 20

· Estimate

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

 10

 10

Development

开发

 60

 未计

· Analysis

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

 60

 120

· Design Spec

· 生成设计文档

 60

 30

· Design Review

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

 60

 未计

· Coding Standard

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

 30

 10

· Design

· 具体设计

 120

 90

· Coding

· 具体编码

 240

 >240

· Code Review

· 代码复审

 60

 未计

· Test

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

 120

 120

Reporting

报告

 60

 90

· Test Report

· 测试报告

 60

 60

· Size Measurement

· 计算工作量

 20

 20

· Postmortem & Process Improvement Plan

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

 20

 未计

 

合计

 1020

 未计

写在前面:
(2018.3.19)首先还是向老师请求谅解。这周任务有点多,虽然我没有像其他同学那样为了实习准备笔试和面试,但是前几天都在准备周四晚上的补考,没有分出精力,补考结束后又匆忙地赶写另一门课程的结课作业,导致这次软件测试作业并没有能按时按量完成。一方面是历史遗留,另一方面也是我最近的时间安排出了问题,所有事情累在一起,一整个星期都手忙脚乱分身乏术,到最后几乎所有的任务都没有做好。
这次作业虽然交了,但是完成内容并没有达到老师的要求。代码部分只大概写了基本要求(-c,-w,-l)和两个拓展功能(-o,-a)。作业过程中因为慌乱和不熟练,效率也比较低下,没有对测试用例进行细致的思考和选择,作业整体还是有点敷衍了事的感觉。以后如果有机会,会继续完善。

 (2018.3.22)因为考虑到自己的代码功能不够完善,所以测试部分使用的是室友【U201517097-祝雨昕】的代码,Github上提交的包也是由室友提供,我自己的代码另附。室友表示下级文件的遍历功能还有缺陷,我的测试是根据她的需求分析来写的,所以和我自己的设计思路以及代码会有出入。

解题思路

开始做的时候其实已经是周日晚上了。因为时间剩的不多,所以也没有太多深入了解。看完需求之后大致列了一下思路:

字符数    简单定义为字符串长度=字符数
单词数    while p→“ ”num++将所有以空格分隔开的字符串视为单词
行数     ReadLine,num++
停用词
注释行    “//” “/*” “*/” “^s//” “}//”
输出          io.BufferedWritter

关于实现

在实现的过程中,我又对上面的思路进行了不少调整。由于之前java编程经验不足,走了不少弯路,而且直到昨天也还是没有正确实现停用词和下级文件统计,考虑到还有测试和博客的部分,我放弃了这两个功能。

最终测试所用的代码并不是我自己的代码,室友代码较长,只在这里贴一部分。

代码部分

室友代码(main函数)

 

 1 public void parse(String name,ArrayList<String> stopList){
 2         String s;
 3         String wordSplit1[]=null;
 4         String wordSplit2[]=null;
 5         char c1;
 6         char c2 = 0;
 7         FileReader fr=null;
 8         BufferedReader br=null;
 9         try {
10             fr=new FileReader(new File(name));
11             br=new BufferedReader(fr);
12             int flag3=0;//多行注释行
13             
14             //计算单词数
15             while((s=br.readLine())!=null){
16                 if(flag3==2)flag3=0;
17                 
18                 //空格分割的单词组
19                 wordSplit1=s.split(" ");
20                 for(int i=0;i<wordSplit1.length;i++){
21                     //得到多个单词
22                     wordSplit2=wordSplit1[i].split(",");
23                     wordNum+=wordSplit2.length;
24                     
25                     for(int j=0;j<wordSplit2.length;j++){
26                         if(wordSplit2[j].equals(""))wordNum--;
27                         
28                         if(stopList!=null)
29                             for(int k=0;k<stopList.size();k++)
30                                 //如果有禁用列表中的或空字符串,不算单词数
31                                 if(!stopList.get(k).equals("")&&wordSplit2[j].equals(stopList.get(k)))wordNum--;
32                         
33                     }
34                 }
35                 
36                 //计算各行数
37                 lineNum++;
38                 charNum+=s.length();
39                 int flag1=0;//此行可看做代码的字符数
40                 int flag2=0;//判断是否有单行注释
41                 
42                 
43                 //按行解析
44                 for(int i=0;i<s.length();i++){
45                     c1=s.charAt(i);
46                     
47                     if(i<=s.length()-2&&c1!=44){
48                         c2=s.charAt(i+1);
49                         //此字符和后一个字符为“//”,为单行注释
50                         if(c1==47&&c2==47)flag2=1;
51                         //多行注释开始
52                         if(c1==47&&c2==42)flag3=1;
53                         //多行注释结束
54                         if(c1==42&&c2==47)flag3=2;
55                         }
56                     if(flag2==0)flag1++;
57                     }
58                 
59                 //得到此行类型
60                 if(flag3!=0)noteLine++;
61                 else if(flag1==0||flag1==1){
62                     if(flag2==0)blankLine++;
63                     if(flag2==1)noteLine++;
64                 }
65                 else codeLine++;
66             }
67             
68             
69         } catch (Exception e) {
70             // TODO Auto-generated catch block
71             e.printStackTrace();
72         }finally{
73             try {
74                 br.close();
75                 fr.close();
76             } catch (IOException e) {
77                 // TODO Auto-generated catch block
78                 e.printStackTrace();
79             }
80             
81         }
82     }

自写代码

 1         InputStreamReader isr = new InputStreamReader(new FileInputStream(filepath));                   //读取文件数据
 2         BufferedReader br = new BufferedReader(isr);                                                //使用缓冲区的read(),readLine()
 3         while(br.read()!=-1)                                                                        //读取
 4         {
 5            String s = br.readLine();
 6            char[] chars=s.toCharArray();
 7            Ccount += s.length();                                                                      //字符个数即为字符串长度
 8            Wcount += s.split(" ").length;                                                             //把一个字符串以“ ”分割成字符串数组即单词
 9 
10            if(chars.length>1) {                                                                       //判断注释行
11                if(chars[0]=='/'&&chars[1]=='/'){                                                      //"//"
12                    Nline++;
13                }
14                else if(chars[0]=='/'&&chars[1]=='*'){                                                 //"/*"
15                    Nline++;
16                    note=1;
17                }
18                else if(chars.length>2) {
19                    if ((chars[0] == '{' || chars[0] == '}')) {                                        //"{//"
20                        if (chars[1] == '/' && chars[2] == '/'){
21                            Nline++;
22                        }
23                        else if (chars[1] == '/' && chars[2] == '*') {                                 //"{/*
24                            Nline++;
25                            note = 1;
26                        }
27                        else if(note!=1) {
28                            Cline++;
29                        }
30                    }
31                    else if(note!=1) {
32                        Cline++;
33                    }
34                }
35                else if(note!=1) {
36                    Cline++;
37                }
38                else {                                                                                 //"/*"……"*/"之间为注释行
39                    Nline++;
40                    for (i = 0; i < chars.length; i++) {
41                        if (chars[i] == '*') {
42                            if (i + 1 < chars.length)
43                                if (chars[i + 1] == '/')
44                                    note = 0;
45                        }
46                    }
47                }
48            }
49            else {                                                                                    
50                Eline++;                                                                              //没有字符
51            }
52            Lcount++;                                                                                 //总行数
53         }  

测试设计

 测试用例如下

用例号

        待测内容

         命令行输入

  备注

Test0

bh13y1@#'2.3

bduabkwb2 ubj

wc.exe -c test0.c -o result.txt

-c是否有正确输出

 

Test1

void transfer(int n)
 {if (n > 10)transfer(n / 10);
  printf("%c", '0' + n % 10); }

wc.exe –w test1.c -o result.txt

-w是否有正确输出

 

Test2

printf("Please input a number:\n");
scanf("%d", &n);
transfer(n);

 

wc.exe –w test2.c -o result.txt –e stoplist.txt

启用停用词表-w是否正确输出

 

Test3

Nisdjoija

 

Dnksan;i

%#%&%#&&%

wc.exe –l test3.c -o result.txt

-l是否有正确输出

 

Test4

wc.exe –s *.c -o result.txt

-s没有符合条件的文件

 

Test5

wc.exe –s *.c -o result.txt

使用*号获取所有符合条件的文件

 

Test6

wc.exe –s *.c -o result.txt

-s处理所有符合条件的文件并输出结果

 

Test7

void main()
 {
int n;
printf("Please input a number:\n");
     scanf("%d", &n);
     transfer(n);
}//this is a noteLine
 
void show(){
    ; /*noteLine
    
    hello,this is noteLine
    right?
    */
   ;//it is

 

wc.exe –c,-w,-l,-a test7.c -o result.txt

综合命令(-c,-w,-l,-a,-o)

 

Test8

Nisdjoija

 

Dnksan;i

%#%&%#&&%

wc.exe –o result.txt

无待测文件名的错误输入

 

Test9

Nisdjoija

 

Dnksan;i

%#%&%#&&%

wc.exe  test9.c

无命令的错误输入

 

Test10

void main()
 {int i = 1;
/*noteLine
    
   this is noteLine
    right?
    */
 }//this is a noteLine

 

wc.exe –a test10.c -o result.txt

-a是否有正确输出(注释)

 

Test11

codeline

codeline/*noteLine
    
    noteline
    */

codeline//it is

//it is//codeline

wc.exe –a test11.c -o result.txt

-a是否有正确输出(代码)

 

Test12

{

  {

   

   }

 

  {  

}

}

wc.exe –a test12.c -o result.txt

-a是否有正确输出(空行)

 

 

 

 

 

posted @ 2018-03-23 16:45  仪征  阅读(269)  评论(1编辑  收藏  举报