合作者:201631062131 201631062330
码云地址:https://gitee.com/ggtc/WordCount.git
作业地址:https://edu.cnblogs.com/campus/xnsy/2018softwaretest2398/homework/2187
一、互审代码
我们参考C# 语言的规范,邹欣老师的讲义“现代软件工程讲义 3 代码规范与代码复审”等初步制定了代码规范。在互审代码的过程中发现的问题也主要是在代码规范方面,比如命名不严谨、空格换行不规范,尤其是很多地方没有相应的注释或者不恰当等。
二、扩展功能
CountAll.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace WordCount { public class CountAll:InterfaceCountAllable { public string Count(string dic) { Lists1 = CountAllFile(dic); List s2 = CountAllDic(dic); //将所有统计结果赋值到返回变量中 if(s1!=null&&s2!=null) { string result = ""; foreach(string s in s1) { result += s+"\n\n"; } foreach(string s in s2) { result += s+"\n\n"; } return result; } else { return null; } } //统计目录下所有文件夹 public List CountAllDic(string dic) { if(DicExist(dic)) { List all = new List (); string[] dics = Directory.GetDirectories(dic); //循环统计所有文件夹下的文件 foreach(string s in dics) { List strs = new List (); strs = CountAllFile(s); foreach(string ss in strs) { all.Add(s); } } return all; } else { return null; } } //统计目录下所有文件 public List CountAllFile(string dic) { if(Directory.Exists(dic)) { List strs=new List (); if(FileExist(dic)) { string[] Files = Directory.GetFiles(dic); InterfaceCommandable ccmd = new ClassCharCount(); InterfaceCommandable wcmd = new ClassWordCount(); InterfaceCommandable lcmd = new ClassRowsCount(); foreach(string s in Files) { //统计一个文件全部信息 string result = ""; //统计字符 result += ccmd.Count(s) + "\n"; //统计单词 result += wcmd.Count(s) + "\n"; //统计行数 result += lcmd.Count(s) + "\n"; //将结果添加到集合 strs.Add(s); } return strs; } else { string result = "该目录下不存在文件"; strs.Add(result); return strs; } } else { return null; } } //判断目录下是否存在文件 public bool FileExist(string dic) { string[] str = Directory.GetFiles(dic); int count = str.Length; if (count != 0) { return true; } else { return false; } } //判断目录下是否存在文件夹 public bool DicExist(string dic) { string[] dics = Directory.GetDirectories(dic); int count = dics.Length; if(count!=0) { return true; } else { return false; } } } }
ComplexData.cs
using System.Text; using System.Threading.Tasks; using System.IO; namespace WordCount { public class ComplexDate:InterfaceCommandable { public string Count(string fileName) { string result = ""; int codeLine = 0; int blankLine = 0; int commentLine = 0; try { //读取文件 FileStream fs = new FileStream(fileName, FileMode.Open); StreamReader sr = new StreamReader(fs, Encoding.Default); string line = sr.ReadLine(); while (line != null) { //注释行 if (line.Substring(0, 2) == "//" || line.Substring(0, 2) == "/*" || line.Substring(0, 2) == "*/" || line.Substring(0, 1) == "*") { commentLine++; } //空行 else if (line == "" || line.Substring(0, 1) == "{" || line.Substring(0, 1) == "}") { blankLine++; } //代码行 else { codeLine++; } } fs.Close(); sr.Close(); result = "代码行:" + codeLine + "/n" + "空行:" + blankLine + "/n" + "注释行:" + "/n"; return result; } catch { Console.WriteLine("打开文件失败"); return result; } } } }
三、单元测试
ComplexData.cs
public class ComplexDataUnitTest { [TestMethod] public void CountTest() { InterfaceCommandable i=new ComplexDate(); //返回不为空 Assert.IsNotNull(i.Count("G:\\VS2015\\程序\\wc\\wc\\bin\\Debug\\test.txt")); //返回未空 //Assert.IsNull(i.Count("D:\\新建文本文档.txt")); } }
四、总结
本次结对编程让我学到了很多同伴编程时的良好习惯,也发现了自身的不足,但是我们也存在很多观点不同的地方,需要以后一步步的去改变自己的想法。这次实验也让我明白以后编程时要更加注重代码规范,对于测试工具的使用也要更加熟练。