第三次作业
GIT地址 | https://github.com/zq6p/WordCount.git |
GIT用户名 | zq6p |
结对伙伴地址 | |
博客地址 | https://www.cnblogs.com/ZHANG-Q/ |
作业链接 | https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2/homework/2879 |
一、PSP表格
PSP2.1 |
Personal Software Process Stages |
预估耗时(分钟) |
实际耗时(分钟) |
Planning |
计划 |
30 | 20 |
· Estimate |
· 估计这个任务需要多少时间 |
30 | 20 |
Development |
开发 |
180 | 220 |
· Analysis |
· 需求分析 (包括学习新技术) |
60 | 40 |
· Design Spec |
· 生成设计文档 |
10 | 15 |
· Design Review |
· 设计复审 (和同事审核设计文档) |
20 | 10 |
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
30 | 20 |
· Design |
· 具体设计 |
40 | 40 |
· Coding |
· 具体编码 |
180 | 230 |
· Code Review |
· 代码复审 |
60 | 40 |
· Test |
· 测试(自我测试,修改代码,提交修改) |
30 | 30 |
Reporting |
报告 |
||
· Test Report |
· 测试报告 |
10 | 5 |
· Size Measurement |
· 计算工作量 |
||
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
10 | 10 |
合计 |
700 | 720 |
二、流程设计图
由于单词统计这个方法比较繁琐,于是输出到文件的操作就直接在方法里进行,
而没有返回值,其它两个方法则是返回具体的值在主函数中输出。
三、结对编程
由于我和敬海是一个寝室的,所以为了方便我俩就组了队,我负责代码编写,敬海负责测试。
四、代码以及思想
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace _201731062511 { public class Program { static void Main(string[] args) { string gettext = Console.ReadLine(); string text = @gettext; if (!File.Exists(text)) { Console.WriteLine("文件不存在!"); return; } Hashtable ht = new Hashtable(StringComparer.OrdinalIgnoreCase); StreamReader sr = new StreamReader(text, System.Text.Encoding.UTF8); string line = sr.ReadToEnd(); StreamWriter sw = new StreamWriter(@"C:\Users\VULCAN\WordCount\out.txt"); Console.SetOut(sw); Console.WriteLine("字符数:{0}", CountChar(line)); Console.WriteLine("换行数:{0}", CountLines(line)); sw.Flush(); sw.Close(); CountWord(text); //Console.ReadKey(); } public static int CountChar(string text)//统计字符数 { int counter = 0; foreach (var num in text) { if (num < 128 && num >= 0) counter++; } return counter; } public static int CountLines(string text)//统计换行数 { int counter = 0; foreach (var num in text) { if (num == '\n') counter++; } return counter; } public static void CountWord(string text)//统计单词 { if (!File.Exists(text)) { Console.WriteLine("文件不存在!"); return; } Hashtable ht = new Hashtable(StringComparer.OrdinalIgnoreCase); StreamReader sr = new StreamReader(text, System.Text.Encoding.UTF8); string line = sr.ReadLine(); string[] wordArr = null; int num = 0; while (!sr.EndOfStream) { wordArr = line.Split(' '); foreach (string s in wordArr) { if (s.Length == 0) continue; //去除标点 line = Regex.Replace(line, @"[\p{P}*]", "", RegexOptions.Compiled); //将单词加入哈希表 if (ht.ContainsKey(s)) { num = Convert.ToInt32(ht[s]) + 1; ht[s] = num; } else { ht.Add(s, 1); } } line = sr.ReadLine(); } ArrayList keysList = new ArrayList(ht.Keys); //对Hashtable中的Keys按字母序排列 keysList.Sort(); //按次数进行插入排序【稳定排序】,所以相同次数的单词依旧是字母序 string tmp = String.Empty; int valueTmp = 0; for (int i = 1; i < keysList.Count; i++) { tmp = keysList[i].ToString(); valueTmp = (int)ht[keysList[i]];//次数 int j = i; while (j > 0 && valueTmp > (int)ht[keysList[j - 1]]) { keysList[j] = keysList[j - 1]; j--; } keysList[j] = tmp;//j=0 } //打印出来 StreamWriter sw = new StreamWriter(@"C:\Users\VULCAN\WordCount\out.txt", true); Console.SetOut(sw); Console.WriteLine($"单词总数:{keysList.Count}"); int f = 0; foreach (object item in keysList) { Console.WriteLine(item.ToString() + ":" + ht[item].ToString()); f++; if (f > 10) break; } sw.Flush(); sw.Close(); } } }
通过对文件的输入输出读入和输出;
单词的计数是通过哈希表操作的,使用的Hashtable(高效)集合,
记录每个单词出现的次数,采用ArrayList对Hashtable中的Keys按字母序排列,
再使用插入排序以及条件限制获得重复个数最多的前十个单词。
下面是程序运行的截图:
我测试的是《了不起的盖茨比》,通过word统计的结果如下:
可以看出字符数是相差无几的,但是单词数稍微有一些偏差,我想了一下原因应该是word统计了所有的单词,而我使用的方法统计的是单个单词不重复的个数,所以和word的结果有偏差。
五、上传到github
有了上次的经验,这次上传github就容易多了。
总结:
刚开始看到这次作业的时候感觉头很大,感觉过程比较繁琐,但是通过和伙伴的合作大大降低了繁琐程度,也让我认识到团队合作能够使开发项目变得高效。
除此之外也能够进一步加强自己对软件项目的了解以及各方面的操作流程的熟悉。
当然也有存在不足的一些地方,比如一开始的设计显得比较草率,通过后面慢慢去修改作业中的不足以及完善程序中的功能。
//
//
//
//
//
//