WordCount小程序及測試

Github項目地址:https://github.com/792450735/wchtml

PSP表格:java

PSP2.1表格[1]git

PSP2.1程序員

PSP階段github

預估耗時數組

(分鐘)app

實際耗時函數

(分鐘)學習

Planning測試

計劃

   15

 20

· Estimate

· 估計這個任務須要多少時間

 870

 1090

Development

開發

 800

 1040

· Analysis

· 需求分析 (包括學習新技術)

 90

 90

· Design Spec

· 生成設計文檔

 30

 30

· Design Review

· 設計複審 (和同事審覈設計文檔)

 10

 10

· Coding Standard

· 代碼規範 (爲目前的開發制定合適的規範)

 10

 10

· Design

· 具體設計

 90

 70

· Coding

· 具體編碼

 500

 700

· Code Review

· 代碼複審

 50

 60

· Test

· 測試(自我測試,修改代碼,提交修改)

 50

 60

Reporting

報告

 70

 50

· Test Report

· 測試報告

 20

 20

· Size Measurement

· 計算工做量

 20

 10

· Postmortem & Process Improvement Plan

· 過後總結, 並提出過程改進計劃

 30

 20

 

合計

 885

 1110

解題思路:

拿到題目後,首先看大體的需求:

基本功能

wc.exe -c file.c     //返回文件 file.c 的字符數

wc.exe -w file.c     //返回文件 file.c 的單詞總數

wc.exe -l file.c     //返回文件 file.c 的總行數

wc.exe -o outputFile.txt     //將結果輸出到指定文件outputFile.txt

 

 擴展功能

wc.exe -s            //遞歸處理目錄下符合條件的文件

wc.exe -a file.c     //返回更復雜的數據(代碼行 / 空行 / 註釋行)

wc.exe -e stopList.txt  // 停用詞表,統計文件單詞總數時,不統計該表中的單詞

 

大體明確了要求,須要用java語言編寫,須要學習:java如何對文件進行操做[2];java的基本語法;java對字符、字符串、數組等的操做[3];將程序打包成.exe文件並將配置環境打包進去[4]等。

程序設計實現過程

首先決定把功能分爲三塊:計算字符數、單詞數、總行數和停用詞放在一塊(wc());-a功能另放一塊(hard());-o功能也一塊(oout())。

能夠把程序只用一個主類,上面三塊爲類裏的三個方法,而且因爲方法之間須要數據通訊,因此將記錄的數據設爲public static,而且另寫一個初始化函數init(),當讀多個文件時,每讀一個後,從新初始化數據。

代碼說明

關鍵代碼分爲三個核心方法,一個初始化方法,一個main函數。

主類裏的數據申明:

public static int words=0;//單詞數

    public static int lines=1;//行數

    public static int chars=0,t1=0,t2=0,t3=0;//t1,t2,t3分別爲代碼行,空行,註釋行

    public static ArrayList<String> fname=new ArrayList<String>();//一個或多個要讀取的.c文件組。

    public static String outname="result.txt";//輸出文件的文件名

    public static String stopList;//停用詞表

main函數中:經過設一個flags數組來記錄用戶輸入的命令,而後根據數組來肯定調用哪些函數,同時也將命令按指定的順序排列好,能夠處理用戶的亂序輸入。

int[] flags=new int[]{0,0,0,0,0};

        for(int i=0;i<args.length;i++){

            if(args[i].equals("-c"))

                flags[0]=1;

            if(args[i].equals("-w"))

                flags[1]=1;

            if(args[i].equals("-l"))

                flags[2]=1;

            if(args[i].equals("-a"))

                flags[4]=1;

            if(!args[i].equals("-c")&&!args[i].equals("-w")&&!args[i].equals("-l")&&!args[i].equals("-o")&&!args[i].equals("-a")&&!args[i].equals("-e")&&!args[i].equals("-s")){

                fname.add(args[i]);

            }

            if(args[i].equals("-o")){

                outname=args[++i];

            }

            if(args[i].equals("-e")){

                stopList=args[++i];

                flags[3]=1;

            }

        }

函數public static void wc(String ff,int[] use)計算字符數,單詞數,行數,包括停用詞表。

計算字符數,單詞數,行數的核心代碼以下:每讀一個字符,chars就加一,每讀到\n,lines就加一,計算單詞時則要立個flag,當讀到非字符,而且非符號字符以前的字符是符號字符,則words加一。

while((c=f.read())!=-1){

                chars++;

                if(c=='\n'){

                   lines++;

                   chars=chars-2;

                }

                if(whiteSpace.indexOf(c)==-1){

                   newlist.append((char)c);

                }

                if(whiteSpace.indexOf(c)!=-1){

                   if(lastNotWhite){

                       words++;

                       for(int j=0;j<stopwords.size();j++){

                           if(newlist.toString().equals(stopwords.get(j)))

                               words--;

                       }

                       newlist.delete(0, newlist.length());

                   }

                   lastNotWhite=false;

                   lastisword=true;

                }

                else{

                   lastisword=false;

                   lastNotWhite=true;

                }

            }

            if(!lastisword){

                for(int j=0;j<stopwords.size();j++){

                   if(newlist.toString().equals(stopwords.get(j)))

                       words--;

                }

            }

若是有使用停用詞表的命令,則讀取停用詞表指定的文件,並使用一個數組存停用詞,以下:

while((c=fs.read())!=-1){

                if(whiteSpace.indexOf(c)==-1){

                   list.append((char)c);

                }

                if(whiteSpace.indexOf(c)!=-1){

                   if(lastNotWhite){

                       stopwords.add(list.toString());

                       list.delete(0, list.length());

                      

                   }

                   lastisword=true;

                   lastNotWhite=false;

                }

                else{

                   lastisword=false;

                   lastNotWhite=true;

                }

            }

            if(!lastisword){

                stopwords.add(list.toString());

            }

            lastisword=false;

            lastNotWhite=false;

這樣,當讀到單詞時,遍歷停用詞數組,若是匹配則words不增長:

if(whiteSpace.indexOf(c)!=-1){

                   if(lastNotWhite){

                       words++;

                       for(int j=0;j<stopwords.size();j++){

                           if(newlist.toString().equals(stopwords.get(j)))

                               words--;

                       }

                       newlist.delete(0, newlist.length());

                   }

                   lastNotWhite=false;

                   lastisword=true;

                }

                else{

                   lastisword=false;

                   lastNotWhite=true;

                }

函數public static void oout(String f,String ff,int[] use)就是對指定文件的建立和寫入,沒什麼特別的。

函數       public static void hard(String f)響應-a指令,要求是:

代碼行:本行包括多於一個字符的代碼。

空   行:本行所有是空格或格式控制字符,若是包括代碼,則只有不超過一個可顯示的字符,例如「{」。

註釋行:本行不是代碼行,而且本行包括註釋。一個有趣的例子是有些程序員會在單字符後面加註釋:

}//註釋

在這種狀況下,這一行屬於註釋行。

根據需求,我認爲須要按行讀取文件,而且立個flag,當讀到第一個是字符,flag置爲1,當第二個仍是字符,則能夠斷定是代碼行,當第二個是「/」,而且其後仍是「/」,則斷定爲註釋行,斷定結束後,若是flag沒變(還是0),則爲空行。

代碼以下:

File ff=new File(f);

        InputStreamReader fff=new InputStreamReader(new FileInputStream(f));

        BufferedReader freader=new BufferedReader(fff);

        String l=null;

        int isflag=0;//isflag=10爲代碼行,=11爲註釋行,=0爲空行

            while((l=freader.readLine())!=null){

                isflag=0;

                for(int i=0;i<l.length();i++){

                   if(l.charAt(i)!=' '&&l.charAt(i)!='/'&&isflag==0&&l.charAt(i)!='\t')

                       {isflag=1;continue;}

                   if(l.charAt(i)!=' '&&l.charAt(i)!='/'&&isflag==1)

                       {isflag=10;break;}

                   if(l.charAt(i)=='/'&&isflag==0)

                       {isflag=2;continue;}

                   if(l.charAt(i)=='/'&&isflag==1)

                       {isflag=2;continue;}

                   if(l.charAt(i)=='/'&&isflag==2)

                       {isflag=11;break;}

                  

                }

                if(isflag==10||isflag==1)

                   t1++;

                else if(isflag==11)

                   t3++;

                else

                   t2++;

            }

            System.out.println(f+",代碼行/空行/註釋行:"+t1+"/"+t2+"/"+t3);

       

    }

測試設計過程:

測試須要儘量地覆蓋全部可能,WordCount程序的高風險地方在於許多不經常使用的,判斷比較模糊的邊界,好比:\n,\r,\t算不算字符,停用詞表是否能被正確停用,空文件的讀取,字符後面的註釋算代碼行仍是註釋行,註釋後面接代碼是註釋行仍是代碼行等等。

個人測試用例以下:

1:測試-c功能

       wc.exe -c test1.c

   指望輸出:

       test1.c,字符數:21

2:測試-l功能

       wc.exe -l test1.c

   指望輸出:

       test1.c,行數:7

3:測試-w功能

       wc.exe -w test1.c

   指望輸出:

       test1.c,單詞數:8

4:測試-a功能

       wc.exe -a test1.c

   指望輸出:

       test1.c,代碼行/空行/註釋行:4/1/2

5:測試是否能按要求順序輸出:

       wc.exe -a -l -w -c test1.c

   指望輸出:

       test1.c,字符數:21

       test1.c,單詞數:8

       test1.c,行數:7

       test1.c,代碼行/空行/註釋行:4/1/2

6:測試-o功能:

       wc.exe -c test1.c -o output.txt

   指望輸出:

       成功建立output.txt,裏面內容爲

       test1.c,字符數:21

7:測試可否按要求順序輸出到指定文件:

       wc.exe -a -l -w -c test1.c -o output.txt

   指望輸出:

       output.txt中內容爲:

       test1.c,字符數:21

       test1.c,單詞數:8

       test1.c,行數:7

       test1.c,代碼行/空行/註釋行:4/1/2

8:測試-e功能:

       wc.exe -w test1.c -e stop.txt

   指望輸出:

       test1.c,單詞數:6

9:測試單/多字符後接註釋的判斷:

       wc.exe -a test2.c

   指望輸出:

       test2.c,代碼行/空行/註釋行:3/1/0

10:測試空文本的輸出:

       wc.exe -a -l -w -c test3.c

    指望輸出:

       test3.c,字符數:0

       test3.c,單詞數:0

       test3.c,行數:1

       test3.c,代碼行/空行/註釋行:0/1/0

參考文獻鏈接:

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

[2]: http://www.cnblogs.com/fnlingnzb-learner/p/6010165.html

[3]: http://blog.csdn.net/Maxiao1204/article/details/52880308

[4]: http://blog.csdn.net/sunkun2013/article/details/13167099

相關文章
相關標籤/搜索