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

WordCount代碼實現及測試

splay 快速 基礎 網絡信息 測試 宋體 當前 計算 ret

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.可以及時的發現一些致命的錯誤並及時改正

WordCount代碼實現及測試