WordCount扩展功能
代码地址:
https://gitee.com/dyh1621838953/WordCount1
1.任务简述
两人组队完成WordCount的基本功能及扩展功能(可以自行增加功能),并对其进行一系列测试和优化,形成可稳定运行的版本。
3.代码互审
在项目的第一阶段,我们用的都是C#语言。我们把不同的功能分开 来思考,然后通过函数逐步实现。因为上一次我们 是一起讨论实现的,所以我们的思路上都是大同小异 ,代码的结构也没有很大的差别,都实现了基本和扩展功能,这次 交流两个人的想法 ,融合一下代码,尽力去做。
4.代码说明
主函数
static void Main(string[] args) { string command = ""; while (command != "exit") { Console.Write("wc.exe "); command = Console.ReadLine(); // 得到输入命令 string[] commandSplitArray = command.Split(' '); // 分割命令 int commandLength = commandSplitArray.Length; string[] parameterArray = new string[commandLength - 1];// 获取命令参数数组 for (int i = 0; i < commandLength - 1; i++) { parameterArray[i] = commandSplitArray[i]; } string fileName = commandSplitArray[commandLength - 1];// 获取文件名 Console.WriteLine(); WC wc = new WC(); wc.getCommand(parameterArray, fileName); } }
功能部分
public void count(string fileName) { int charCount = 0; int wordCount = 0; int lineCount = 0; string path = fileName; FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); Stream stream = new MemoryStream(bytes);//把 byte[] 转换成 Stream charCount = (int)stream.Length; stream.Position = 0; StreamReader reader = new StreamReader(stream); string streamToString = reader.ReadToEnd(); lineCount = streamToString.Split('\r').Length; streamToString = Regex.Replace(streamToString, "[^\u4e00-\u9fa5a-zA-z0-9.].*?", " "); streamToString = Regex.Replace(streamToString, "\\s{2,}", " "); wordCount = streamToString.Split(' ').Length; charCountp = charCount; wordCountp = wordCount; lineCountp = lineCount; } public void extendCount(string fileName) { FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); StreamReader sr = new StreamReader(fs); String line; int nullLineCount = 0; int codeLineCount = 0; int noteLineCount = 0; while ((line = sr.ReadLine()) != null) { line = line.Trim(' '); line = line.Trim('\t');// 除去每行开头多余空格和格式控制字符 if (line == "" || line.Length <= 1)// 空行 { nullLineCount++; } else if (line.Substring(0, 2) == "//" || line.Substring(1, 2) == "//")// 注释行 { noteLineCount++; } else { codeLineCount++; // 代码行 } } sr.Close(); nullLineCountp = nullLineCount; noteLineCountp = noteLineCount; codeLineCountp = codeLineCount; }
5.代码测试以及运行结果
6.总结
在代码方面,两个人之间出现分歧的时候就各持己论,然后进行讨论,都在试图说服 对方,虽然过程有点激烈,但是最终总会静下来发现其中的缺点 和对方有哪些优点,我觉得这是组队的好处。但是需要能 好点掌控自己,需要更多的甲流,不然适得其反。在做项目写代码方面,我们两个人都还需要更多的努力,实在很差。