WC-第二次作业
WordCount 第二次作业
码云地址:https://gitee.com/lgcj1218/WordCount/tree/master
一.解题思路
本次作业采用的c#语言 按功能分为了三个类 ,计算字符数,计算单词数,计算行数。然后在主程序中new三个类,当需要进行着三个功能时就调用其中的函数。
二.代码说明
1.主程序(包含输出文件的方法)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 static void Main(string[] args) 2 { 3 string cmd = ""; 4 while (cmd != "leave") 5 { 6 int i; 7 int charCount, wordCount, lineCount; 8 string testFile; 9 string outFile; 10 Console.Write("wc.exe "); 11 cmd = Console.ReadLine(); 12 string[] arrMessSplit = cmd.Split(' '); 13 List<string> arrParameter = new List<string>(); 14 for (i = 0; i < arrMessSplit.Length; i++) 15 { 16 arrParameter.Add(arrMessSplit[i]); 17 } 18 bool isOut = false; 19 for (i = 0; i < arrMessSplit.Length - 1; i++) 20 { 21 22 if (arrParameter[i] == "-o") 23 { 24 isOut = true; 25 } 26 } 27 28 CharCount cc = new CharCount(); 29 WordCount wc = new WordCount(); 30 LineCount lc = new LineCount(); 31 if (isOut) 32 { 33 testFile = arrParameter[arrParameter.Count - 3]; 34 for (i = 0; i < arrMessSplit.Length - 1; i++) 35 { 36 if (arrParameter[i] == "-c") 37 { 38 cc.CC(testFile); 39 } 40 if (arrParameter[i] == "-w") 41 { 42 wc.WC(testFile); 43 } 44 if (arrParameter[i] == "-l") 45 { 46 lc.LC(testFile); 47 } 48 } 49 outFile = arrParameter[arrParameter.Count - 1]; 50 FileStream fs = new FileStream(outFile, FileMode.Create); 51 StreamWriter sw = new StreamWriter(fs); 52 sw.Write("{0},charCount:{1}\r\n", testFile, cc.charCount); 53 sw.Write("{0},wordCount:{1}\r\n", testFile, wc.wordCount); 54 sw.Write("{0},lineCount:{1}\r\n", testFile, lc.lineCount); 55 sw.Flush(); 56 sw.Close(); 57 fs.Close(); 58 Console.WriteLine("WriteToFile:{0}", outFile); 59 } 60 else 61 { 62 testFile = arrParameter[arrParameter.Count - 1]; 63 for (i = 0; i < arrMessSplit.Length - 1; i++) 64 { 65 if (arrParameter[i] == "-c") 66 { 67 cc.CC(testFile); 68 } 69 70 if (arrParameter[i] == "-w") 71 { 72 wc.WC(testFile); 73 } 74 75 if (arrParameter[i] == "-l") 76 { 77 lc.LC(testFile); 78 } 79 } 80 } 81 Console.ReadLine(); 82 } 83 } 84 } 85 }
2.计算字符数的类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class CharCount 2 { 3 4 public string testFile; 5 public int charCount = 0; 6 public void CC(string testFile) 7 { 8 9 int nChar; 10 char[] symbol = { ' ', ',' }; 11 FileStream file = new FileStream(testFile, FileMode.Open, FileAccess.Read, FileShare.Read); 12 StreamReader sr = new StreamReader(file); 13 while ((nChar = sr.Read()) != -1) 14 { 15 charCount++; 16 } 17 Console.WriteLine("{0},charCount:{1}", testFile, charCount); 18 } 19 }
3.计算单词数的类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class WordCount 2 { 3 public string testFile; 4 public int wordCount=0; 5 public void WC(string testFile) 6 { 7 8 int nChar; 9 char[] divide = { ' ', ',' }; 10 FileStream file = new FileStream(testFile, FileMode.Open,FileAccess.Read, FileShare.Read); 11 StreamReader sr = new StreamReader(file); 12 while ((nChar = sr.Read()) != -1) 13 { 14 foreach (char c in divide) 15 { 16 if (nChar == (int)c) 17 { 18 wordCount++; 19 } 20 } 21 } 22 wordCount++; 23 Console.WriteLine("{0},wordCount:{1}", testFile, wordCount); 24 } 25 }
4.计算行数的类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class LineCount 2 { 3 public string testFile; 4 public int lineCount = 0; 5 public void LC(string testFile) 6 { 7 8 int nChar; 9 char[] divide = { ' ', ',' }; 10 FileStream file = new FileStream(testFile, FileMode.Open, FileAccess.Read, FileShare.Read); 11 StreamReader sr = new StreamReader(file); 12 while ((nChar = sr.Read()) != -1) 13 { 14 if (nChar == '\n') 15 { 16 lineCount++; 17 } 18 } 19 lineCount++; 20 Console.WriteLine("{0},lineCount:{1}", testFile, lineCount); 21 } 22 }
三.测试设计过程
1.测试写入文件
2.测试计算字符,单词和行数的功能