Github项目地址:https://github.com/3116005131/3116005131.git
PSP2.1 |
Personal Software Process Stages |
预估耗时(分钟) |
实际耗时(分钟) |
Planning |
计划 |
20 | 15 |
· Estimate |
· 估计这个任务需要多少时间 |
5 | 5 |
Development |
开发 |
660 | 1045 |
· Analysis |
· 需求分析 (包括学习新技术) |
300 | 660 |
· Design Spec |
· 生成设计文档 |
100 | 120 |
· Design Review |
· 设计复审 (和同事审核设计文档) |
20 | 15 |
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
10 | 30 |
· Design |
· 具体设计 |
60 | 20 |
· Coding |
· 具体编码 |
120 | 180 |
· Code Review |
· 代码复审 |
20 | 10 |
· Test |
· 测试(自我测试,修改代码,提交修改) |
30 | 10 |
Reporting |
报告 |
100 | 120 |
· Test Report |
· 测试报告 |
30 | 40 |
· Size Measurement |
· 计算工作量 |
30 | 40 |
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
40 | 40 |
total |
合计 |
780 | 1180 |
解题思路:统计文件的字符数量,首先我要学习用C#读取文件,因为之前只是做过处理基本数据类型的一些类和方法。所以估计在这方面的学习要花费大量的时间。学习的方法是先,上网查找,在其他人代码中,当需要调用文件的读写操作,他们是用什么函数,方法实现的。发现代码中出现FileStream类,然后就查找书本---《C#程序设计教程》,系统的学习FileStream类,以及与其一起使用的StreamReader类。学习后发现。由于只翻阅了一本参考书籍就找到了需要学习的内容,并有详细的介绍,故能快速的掌握,节省了不少学习的时间。在开始编程时,又遇到了第二个问题,设计文档虽然写好,由于太久没使用C#语言编程,但是不断的出现语法错误。导致不得不停下来,把C#的面向对象编程部分开完,并开了一个新建的命令行窗口项目来练习C#的语法。编译成功后,完成了三个基本功能,打算做下一步的单元测试,可发现单元测试必须是公共类或公共方法,而我把代码全都写在了main函数中,为实现单元测试不得,把代码写到类wc中。
设计实现过程:由于统计字符器,还算是逻辑简单的程序,所以打算只用一个公共类完成功能,使用默认的创造函数,输入函数,处理函数,输出函数。为了使代码更简洁,加多了一个执行函数调用另外三个函数。使main函数中更简洁。由于逻辑都比较简单,基本都是使用if的二分支选择语句,所以没有画流程图。
代码说明
public class wc { string path; //需统计文件的路径 char[] types = { ‘c‘, ‘C‘, ‘w‘, ‘W‘, ‘l‘, ‘L‘ }; //目前所能统计的类型(字符,单词,行数) int n; //统计结果 char[] type; //当前统计的类型 public void NCount() //统计方法 { GetMessage(); Count(); Show(); } public void GetMessage() //输入信息 { ...... } public void Count() //统计数量 { ...... } public void Show() //输出结果 { ...... } }
输入函数
1 public void GetMessage() //输入信息 2 { 3 Console.Write("WC.exe -"); 4 string Message = Console.ReadLine(); 5 string[] CategoryInfomation = Message.Split(‘ ‘); 6 path = CategoryInfomation[1]; 7 type = CategoryInfomation[0].ToCharArray(); 8 }
处理函数
1 public void Count() //统计数量 2 { 3 int c=0, w=0, l=0;//字符数,单词数,行数 4 int WFlag=0; //单词标识符 5 FileStream fs = null; 6 StreamReader reader = null; 7 try 8 { 9 fs = new FileStream(path, FileMode.Open, FileAccess.Read); 10 reader = new StreamReader(fs); 11 reader.BaseStream.Seek(0, SeekOrigin.Begin); 12 int i = reader.Read(); 13 while (i!=-1) 14 { 15 c++; 16 if ((i<=90&&i>=65)||(i>=97&&i<=122)) 17 { 18 if (WFlag == 0) 19 { 20 w++; 21 WFlag = 1; 22 } 23 24 } 25 else WFlag = 0; 26 char character = (char)i; 27 if (character == ‘\n‘) 28 { 29 l++; 30 } 31 i = reader.Read(); 32 } 33 } 34 catch (Exception) //path输入有误 35 { 36 Console.WriteLine("输入的地址有误,找不到该文件"); 37 } 38 finally 39 { 40 if(reader!=null)reader.Close(); 41 if(fs!=null)fs.Close(); 42 } 43 if (type[0] == ‘c‘ || type[0] == ‘C‘) 44 { 45 n = c; 46 } 47 else if (type[0] == ‘w‘ || type[0] == ‘W‘) 48 { 49 n = w; 50 } 51 else if (type[0] == ‘l‘ || type[0] == ‘L‘) 52 { 53 n = l; 54 } 55 }
输出函数
1 public void Show() //输出结果 2 { 3 if (type[0] == ‘c‘ || type[0] == ‘C‘) 4 { 5 Console.WriteLine("字符数为{0}", n); 6 } 7 else if (type[0] == ‘w‘ || type[0] == ‘W‘) 8 { 9 Console.WriteLine("单词数为{0}", n); 10 } 11 else if(type[0] == ‘l‘ || type[0] == ‘L‘) 12 { 13 Console.WriteLine("行数为{0}", n); 14 } 15 else 16 { 17 Console.WriteLine("Word count does not support this type of statistics."); //输入的参数有误,n=0 18 } 19 }
测试运行
项目小结:这次能成功完成作业,幸好因为WC需要实现的基本功能逻辑简单,只要掌握基本的语法就能完成。也多亏所有使用到的类的使用都能在一本书中全部找到,不然恐怕是完成不了项目。由于学习C#语法花费了太多时间,导致没有时间学习如何进行单元测试。完成项目后必须花费时间把单元测试做了,为下次的项目做充足的准备,不能在把绝大部分时间用来学习上了。
原文:https://www.cnblogs.com/3116005131-duanhaobin/p/9648864.html