WordCount結對編程

WordCount結對編程

合做人:軟件工程 徐建敏 201631062323; 軟件工程 林布爾 201631062223;git

源代碼地址:https://gitee.com/xjm861710023/wordcount_development;編程

做業連接:https://edu.cnblogs.com/campus/xnsy/2018softwaretest2398/homework/2187;數組

一,代碼互審部分

  兩人均使用C#語言進行編寫,故在合併階段只需將變量名稍做修改便可。且架構

命名意義清楚,註釋充分,故沒有遇到較大阻礙,只是因爲自己環境緣由(x86架工具

構匹配問題),在靜態代碼測試階段遇到了一些問題,不過獲得了有效解決。在代單元測試

碼檢測後修改了部分代碼,使得程序更加可靠,但仍有部分警告沒能得以解決。學習

二,代碼部分

1)WordCount部分、

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.IO;
  6 
  7 
  8 namespace wc
  9 {
 10     public class WC
 11     {
 12         public string filename;    // 文件名
 13         public string[] filenames;   //多個文件名
 14         public string stopwordfile;    //停用詞文件名
 15         public string[] msgSplit; //全部參數數組
 16         public string outfile;   //輸出文件名
 17         public string str;       //輸出字符串
 18         public int iccount;      // 字符數
 19         public int iwcount;      // 單詞數
 20         public int ilcount;      // 總行數
 21         public int inulllinecount;  //空行數
 22         public int icodelinecount;  //代碼行數
 23         public int inotelinecount;  //註釋行數
 24         //引入輸入字符串,並對字符串進行處理
 25         public void Operator(string[] msgSplit)
 26         {
 27             this.msgSplit = msgSplit;    //引入操做數組
 28             int Length;
 29             Length = msgSplit.Length;
 30             foreach (string s in msgSplit)
 31             {
 32                 if (s == "-e")
 33                 {
 34                     Length = Length - 2;
 35                 }
 36                 else if (s == "-o")
 37                 {
 38                     Length = Length - 2;
 39                 }
 40             }
 41             this.filename = msgSplit[Length - 1];//獲取文件名
 42             if (msgSplit[0] == "wc.exe")
 43             {
 44                 foreach (string s in msgSplit)
 45                 {
 46                     if (s == "-s")
 47                     {
 48                         //獲取全部後綴名符合的文件
 49                         string fileDir = Environment.CurrentDirectory;
 50                         filenames = Directory.GetFiles(fileDir, filename);
 51                     }
 52                     else if (s == "-e")
 53                     {
 54                         //獲取停用詞表文件
 55                         for (int i = 0; i < msgSplit.Length; i++)
 56                         {
 57                             if (msgSplit[i] == "-e")
 58                             {
 59                                 stopwordfile = msgSplit[i + 1];
 60                             }
 61                         }
 62                     }
 63                 }
 64                 if (filenames == null)
 65                 {
 66                     SuperCount(filename);
 67                     BaseCount(filename, stopwordfile);
 68                     Display(filename);
 69                 }
 70                 else
 71                 {
 72                     for (int i = 0; i < filenames.Length; i++)
 73                     {
 74                         SuperCount(filenames[i]);
 75                         BaseCount(filenames[i], stopwordfile);
 76                         Display(filenames[i]);
 77                     }
 78                 }
 79             }
 80             else
 81             {
 82                 Console.WriteLine("輸入指令不存在!請從新輸入。。。");
 83             }
 84         }
 85         // 統計基本信息:字符數 單詞數 行數
 86         public void BaseCount(string filename, string stopwordfile)
 87         {
 88             try
 89             {
 90                 // 打開文件
 91                 FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
 92                 StreamReader sr = new StreamReader(file);
 93                 int nChar;
 94                 int charcount = 0;
 95                 int wordcount = 0;
 96                 int linecount = 0;
 97                 //定義一個字符數組
 98                 char[] symbol = { ' ', '\t', '\r', ',', '.', '?', '!', ':', ';', '\'', '\"', '\n', '{', '}', '(', ')', '<', '>', '+', '-', '*', '=' };
 99                 while ((nChar = sr.Read()) != -1)
100                 {
101                     charcount++;     // 統計字符數
102                     if (nChar == '\n')
103                     {
104                         linecount++; // 統計行數
105                     }
106                 }
107                 sr.Close();
108                 file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
109                 sr = new StreamReader(file);
110                 string words = sr.ReadToEnd();
111                 string[] word = words.Split(symbol, StringSplitOptions.RemoveEmptyEntries);//將文件內全部單詞分割
112                 if (stopwordfile != null)
113                 {
114                     //有停用詞表
115                     FileStream stopfile = new FileStream(stopwordfile, FileMode.Open, FileAccess.Read, FileShare.Read);
116                     StreamReader stopsr = new StreamReader(stopfile);
117                     string stopwords = stopsr.ReadToEnd();
118                     string[] stopword = stopwords.Split(symbol, StringSplitOptions.RemoveEmptyEntries);//將停用詞表內全部單詞分割
119                     foreach (string s in word)
120                     {
121                         foreach (string stop in stopword)
122                         {
123                             if (s != stop)
124                             {
125                                 wordcount++;//統計單詞數
126                             }
127                         }
128                     }
129                     stopsr.Close();
130                 }
131                 else
132                 {
133                     //沒有停用詞表
134                     wordcount = word.Length;
135                 }
136                 sr.Close();
137                 iccount = charcount;
138                 iwcount = wordcount;
139                 ilcount = linecount + 1;
140             }
141             catch (IOException ex)
142             {
143                 Console.WriteLine(ex.Message);
144                 return;
145             }
146         }
147         // 統計代碼行 空行 註釋行 
148         public void SuperCount(string filename)
149         {
150             try
151             {
152                 // 打開文件
153                 using (FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
154                 {
155                     StreamReader sr = new StreamReader(file);
156                     String line;
157                     int nulllinecount = 0;
158                     int codelinecount = 0;
159                     int notelinecount = 0;
160                     while ((line = sr.ReadLine()) != null)
161                     {
162                         //   除去每行開頭多餘空格和格式控制字符
163                         line = line.Trim(' ');
164                         line = line.Trim('\t');
165                         //   空行
166                         if (line == "" || line.Length <= 1)
167                         {
168                             nulllinecount++;
169                         }
170                         //   註釋行
171                         else if (line.Substring(0, 2) == "//" || line.Substring(1, 2) == "//")
172                         {
173                             notelinecount++;
174                         }
175                         // 代碼行
176                         else
177                         {
178                             codelinecount++;
179                         }
180                     }
181                     inulllinecount = nulllinecount;
182                     icodelinecount = codelinecount;
183                     inotelinecount = notelinecount;
184                 }
185             }
186             catch (IOException ex)
187             {
188                 Console.WriteLine(ex.Message);
189                 return;
190             }
191         }
192     
193         // 打印信息
194         public void Display(string filename)
195         {
196             //將操做命令以-c,-w,-l的順序輸出
197             foreach (string s in msgSplit)
198             {
199                 if (s == "-c")              //遍歷第一次找-c命令,有的話輸出字符數
200                 {
201                     Console.WriteLine(filename + ",字符數:{0}", iccount);
202                     str = filename + ",字 符 數:" + iccount + "\r\n";
203                     File.AppendAllText("result.txt", str, Encoding.Default);
204                 }
205             }
206             foreach (string s in msgSplit)
207             {
208                 if (s == "-w")             //遍歷第二次找-w命令,有的話輸出單詞數
209                 {
210                     Console.WriteLine(filename + ",單詞數:{0}", iwcount);
211                     str = filename + ",單 詞 數:" + iwcount + "\r\n";
212                     File.AppendAllText("result.txt", str, Encoding.Default);
213                 }
214             }
215             foreach (string s in msgSplit)
216             {
217                 if (s == "-l")              //遍歷第三次找-l命令,有的話輸出行數
218                 {
219                     Console.WriteLine(filename + ",行 數:{0}", ilcount);
220                     str = filename + ",總 行 數:" + ilcount + "\r\n";
221                     File.AppendAllText("result.txt", str, Encoding.Default);
222                 }
223             }
224             foreach (string s in msgSplit)
225             {
226                 if (s == "-a")            //遍歷第四次找-a命令,有的話輸出代碼行/空行/註釋行
227                 {
228                     Console.WriteLine(filename + ",代碼行/空行/註釋行:{0}/{1}/{2}", icodelinecount, inulllinecount, inotelinecount);
229                     str = filename + ",代碼行/空行/註釋行:" + icodelinecount + '/' + inulllinecount + '/' + inotelinecount + "\r\n";
230                     File.AppendAllText("result.txt", str, Encoding.Default);
231                 }
232             }
233             foreach (string s in msgSplit)
234             {
235                 if (s == "-o")               //遍歷第四次找-o命令,有的話將結果存入輸出文件
236                 {
237                     this.outfile = msgSplit[msgSplit.Length - 1];   //引入輸出文件名
238                     foreach (string st in msgSplit)
239                     {
240                         if (st == "-c")          //遍歷第一次找-c命令,有的話將字符數存入輸出文檔
241                         {
242                             str = filename + ",字 符 數:" + iccount + "\r\n";
243                             File.AppendAllText("" + outfile + "", str, Encoding.Default);
244                         }
245                     }
246                     foreach (string st in msgSplit)
247                     {
248                         if (st == "-w")           //遍歷第二次找-w命令,有的話將單詞數存入輸出文檔
249                         {
250                             str = filename + ",單 詞 數:" + iwcount + "\r\n";
251                             File.AppendAllText("" + outfile + "", str, Encoding.Default);
252                         }
253                     }
254                     foreach (string st in msgSplit)
255                     {
256                         if (st == "-l")            //遍歷第三次找-l命令,有的話將行數存入輸出文檔
257                         {
258                             str = filename + ",總 行 數:" + ilcount + "\r\n";
259                             File.AppendAllText("" + outfile + "", str, Encoding.Default);
260                         }
261                     }
262                     foreach (string st in msgSplit)
263                     {
264                         if (st == "-a")          //遍歷第四次找-a命令,有的話將代碼行/空行/註釋行存入輸出文檔
265                         {
266                             str = filename + ",代碼行/空行/註釋行:" + icodelinecount + '/' + inulllinecount + '/' + inotelinecount + "\r\n";
267                             File.AppendAllText("" + outfile + "", str, Encoding.Default);
268                         }
269                     }
270                 }
271             }
272             Console.WriteLine();
273         }
274     }
275 }

2)單元測試部分

 1 using System;
 2 using System.Text;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using Microsoft.VisualStudio.TestTools.UnitTesting;
 6 using System.IO;
 7 
 8 namespace TestWC
 9 {
10     [TestClass]
11     public class UnitTest1
12     {
13         [TestMethod]
14         public void TestBaseCount()
15         {
16             //測試統計字符數,單詞書,行數
17             wc.WC wordcount = new wc.WC();
18             string filename = "D://myPROJECTS//wc//wc//bin//Debug//file1.c";
19             string stopwordfile = "D://myPROJECTS//wc//wc//bin//Debug//stop.txt";
20             wordcount.BaseCount(filename, stopwordfile);
21             int ccount = wordcount.iccount;
22             int wcount = wordcount.iwcount;
23             int lcount = wordcount.ilcount;
24             Assert.AreEqual(87, ccount);
25             Assert.AreEqual(10, wcount);
26             Assert.AreEqual(7, lcount);
27         }
28         [TestMethod]
29         public void TestOperator()
30         {
31             //測試對輸入字符串進行引入並處理
32             wc.WC wordcount = new wc.WC();
33             string[] msgSplit = { "wc.exe", "-c", "-w", "-l", "D://myPROJECTS//wc//wc//bin//Debug//file1.c"};
34             wordcount.Operator(msgSplit);
35             string filename = wordcount.filename;
36             string stopword = wordcount.stopwordfile;
37             string[] filenames = wordcount.filenames;
38             Assert.AreEqual("D://myPROJECTS//wc//wc//bin//Debug//file1.c", filename);
39             Assert.AreEqual(null, stopword);
40             Assert.AreEqual(null, filenames);
41         }
42         [TestMethod]
43         public void TestSuperCount()
44         {
45             //測試統計代碼行 空行 註釋行
46             wc.WC wordcount = new wc.WC();
47             string filename = "D://myPROJECTS//wc//wc//bin//Debug//file1.c";
48             wordcount.SuperCount(filename);
49             int codecount = wordcount.icodelinecount;
50             int nullcount = wordcount.inulllinecount;
51             int notecount = wordcount.inotelinecount;
52             Assert.AreEqual(4, codecount);
53             Assert.AreEqual(2, nullcount);
54             Assert.AreEqual(1, notecount);
55         }
56         [TestMethod]
57         public void TestDisplay()
58         {
59             //測試輸出
60             wc.WC wordcount = new wc.WC();
61             string[] msg={"wc.exe","-c","D://myPROJECTS//wc//wc//bin//Debug//file1.c"};
62             wordcount.msgSplit = msg;
63             string filename = "D://myPROJECTS//wc//wc//bin//Debug//file1.c";
64             wordcount.Display(filename);
65             string result = wordcount.str;
66             Assert.AreEqual("D://myPROJECTS//wc//wc//bin//Debug//file1.c,字 符 數:0\r\n", result);
67         }
68     }
69 }

三,靜態代碼檢查部分

靜態代碼檢查結果以下:

 所用靜態代碼檢測工具爲visual studio 2010自帶的代碼檢測工具測試

 

  對於SuperCount中出現的CA2000警告,已經過爲file對象添加using釋放了被引用的資源,而BaseCount中因爲file及stopfile被屢次引用,this

使用using則沒法有效解決問題,並未找到行之有效的處理方法且不會對程序產生重大影響,故此處再也不對此警告進行解決。spa

  初次靜態代碼檢測時則遇到了TestWC的「處理器架構「x86」不匹配。這種不匹配可能會致使運行時失敗。請考慮經過配置管理器更改您

的項目的目標處理器架構,以使您的項目與引用間的處理器架構保持一致,或者爲引用關聯一個與您的項目的目標處理器架構相符的處理器架構

」的警告,已經過TestWC配置屬性解決,故在此再也不贅述。

四,單元測試部分

單元測試所有經過。

五,功能展現部分

程序有WordCount基礎功能與拓展功能。

六,心得體會

經過這次結對編程,學會了各類工具的使用方法,相信對我從此的學習能有很大的幫助。

相關文章
相關標籤/搜索