1. 程式人生 > >WordCount程式碼實現及測試

WordCount程式碼實現及測試

1.專案地址

開發者:201631062515 201631062415

碼雲地址:https://gitee.com/heshuxiang/WordCount/tree/master

2.專案需求

    對程式設計語言原始檔統計字元數、單詞數、行數,統計結果以指定格式輸出到預設檔案中,以及其他擴充套件功能,並能夠快速地處理多個檔案。

(1)基本功能: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

 

(2)程式碼互審情況

大部分的程式碼都沒有問題,在主函式傳遞和計算單詞量時出現了一些函式傳遞不一致和變數問題,已修改。

 

3.基本思路

    拿到該專案時,我首先想到的是要先確定字元,單詞,行數的判斷條件。我想只要這個確定了,那麼剩下的工作就很簡單了,我只需要按照這個判斷條件去進行編碼實現。由於現階段我們只需要完成這些基礎功能,因而在程式語言方面我選擇了最早接觸的C語言。

當然在確定判斷條件時我們還是要動點腦子的,可以聯絡實際去思考。例如在統計字元時,我們可以通過迴圈來判斷當前字元是否為空格,或其他非字元元素,如果是,字元數就加一。又例如在判斷單詞數時,我們可以先確定單詞之間可以是逗號,可以是空格,通過這些條件來判斷哪些字元組成了單詞。當然如果實在不行,我們還可以藉助網路去查詢,畢竟這是一個網路資訊時代。

4.原始碼

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.IO;
  6 using System.Threading.Tasks;
  7 
  8 
  9 namespace WordCount
 10 {
 11     class Program
 12     {
 13         public class WC
 14         {
 15             public string sFilename;    // 檔名
 16             public string[] sParameter; // 引數陣列  
 17             public int iCharcount;      // 字元數
 18             public int iWordcount;      // 單詞數
 19             public int iLinecount;      // 總行數
 20 
 21             // 引數控制資訊
 22             public void Operator(string[] sParameter, string sFilename)
 23             {
 24                 this.sParameter = sParameter;
 25                 this.sFilename = sFilename;
 26                 foreach (string s in sParameter)
 27                 {
 28                     //  基本功能
 29                     if (s == "-c" || s == "-w" || s == "-l")
 30                     {
 31                         break;
 32                     }
 33                     else
 34                     {
 35                         Console.WriteLine("引數 {0} 不存在", s);
 36                         break;
 37                     }
 38                 }
 39             }
 40             // 統計基本資訊:字元數 單詞數 行數
 41             public void BaseCount()
 42             {
 43                 try
 44                 {
 45                     // 開啟檔案
 46                     FileStream file = new FileStream(sFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
 47                     StreamReader sr = new StreamReader(file);
 48                     int nChar;
 49                     int charcount = 0;
 50                     int wordcount = 0;
 51                     int linecount = 0;
 52                     //定義一個字元陣列
 53                     char[] symbol = { ' ', '\t', ',', '.', '?', '!', ':', ';', '\'', '\"', '\n', '{', '}', '(', ')', '+' ,'-',
 54               '*', '='};
 55                     while ((nChar = sr.Read()) != -1)
 56                     {
 57                         charcount++;     // 統計字元數
 58 
 59                         foreach (char c in symbol)
 60                         {
 61                             if (nChar == (int)c)
 62                             {
 63                                 wordcount++; // 統計單詞數
 64                             }
 65                         }
 66                         if (nChar == '\n')
 67                         {
 68                             linecount++; // 統計行數
 69                         }
 70                     }
 71                     iCharcount = charcount;
 72                     iWordcount = wordcount + 1;
 73                     iLinecount = linecount + 1;
 74                     sr.Close();
 75                 }
 76                 catch (IOException ex)
 77                 {
 78                     Console.WriteLine(ex.Message);
 79                     return;
 80                 }
 81             }
 82             // 列印資訊
 83             public void Display()
 84             {
 85                 foreach (string s in sParameter)
 86                 {
 87                     if (s == "-c")
 88                     {
 89                         Console.WriteLine("字 符 數:{0}", iCharcount);
 90                     }
 91                     else if (s == "-w")
 92                     {
 93                         Console.WriteLine("單 詞 數:{0}", iWordcount);
 94                     }
 95                     else if (s == "-l")
 96                     {
 97                         Console.WriteLine("總 行 數:{0}", iLinecount);
 98                     }
 99                 }
100                 Console.WriteLine();
101             }
102         }
103 
104         [STAThread]
105         static void Main(string[] args)
106         {
107             string message = ""; // 儲存使用者命令
108             while (message != "exit")
109             {
110                 Console.Write("wc.exe ");
111                 message = Console.ReadLine();               // 得到輸入命令
112                 string[] arrMessSplit = message.Split(' '); // 分割命令
113                 int iMessLength = arrMessSplit.Length;
114                 string[] sParameter = new string[iMessLength - 1];
115                 // 獲取命令引數陣列
116                 for (int i = 0; i < iMessLength - 1; i++)
117                 {
118                     sParameter[i] = arrMessSplit[i];
119                 }
120                 // 獲取檔名
121                 string sFilename = arrMessSplit[iMessLength - 1];
122                 Console.WriteLine();
123                 WC wc = new WC();
124                 wc.Operator(sParameter, sFilename);//執行
125                 wc.BaseCount();//統計資訊
126                 wc.Display();//列印資訊
127                 
128               }
129         }
130 
131     }
132 }

 

 

5.測試文字

 

6.測試結果

 

 

7.總結

 通過這回的結對程式設計專案,我發現了一些結對程式設計的優點和更廣闊的提升技術的平臺碼雲能幫助我儲存程式碼版本,部落格園能提供我軟體開發的知識和視野,有許多軟體開發者在部落格園這個平臺上發表自己的學習體會和開發經驗

1.結對程式設計可是通過隊友的討論使編碼更有思路
2.可以及時的發現一些致命的錯誤並及時改正