第三次作业结对编程
运行效果
计算模块接口的设计与实现过程
static void Main(string[] args) { string fileName = @"G:\input.txt"; Count count1 = new Count(fileName); if (File.Exists(fileName)) { count1.count_characters();//统计字符数 count1.count_word();//统计单词总数 count1.count_lines();//统计有效行数 count1.count_words();//统计单词出现次数 } Console.ReadKey(); }
统计字符:
public void count_characters()//统计字符数 { FileStream file1 = new FileStream(fileName1, FileMode.Open, FileAccess.Read);//打开的文件 int count; count = 0; byte[] buf = new byte[file1.Length];//缓存区接受文件夹中的内容 file1.Read(buf, 0, buf.Length); for (int a = 0; a < buf.Length; a++)//利用循环判断是否为字符 { if ((int)buf[a] <= 127) count++; } //File.AppendAllText(@"G:\output.txt", "characters:");//强制输入某段数据到指定文件夹 string avaible1 = "character: "; string avaible2; avaible2 = Convert.ToString(count); Console.WriteLine(avaible1 + avaible2); avaible1 = avaible1 + avaible2; StreamWriter file2 = new StreamWriter(fileName2, true);//写入该文件夹中,true表示追加到问价下一行 file2.WriteLine(avaible1); file2.Flush(); file1.Close(); file2.Close(); }
统计单词总数
public void count_word()//统计单词总数 { Dictionary<string, int> dictionary_1; List<string> listLines = new List<string>(); StreamReader reader = new StreamReader(@"G:\input.txt"); string line = reader.ReadLine(); while (line != "" && line != null) { listLines.Add(line); line = reader.ReadLine(); } string str1; str1 = ""; for (int i = 0; i < listLines.Count; i++) { str1 = str1 + listLines[i]; } dictionary_1 = new Dictionary<string, int>(); string[] words = Regex.Split(str1, @"\W+"); int counts; counts = 0; foreach (string word in words) { if (dictionary_1.ContainsKey(word)) { dictionary_1[word]++; } else { dictionary_1[word] = 1; } } foreach (KeyValuePair<string, int> avaible_1 in dictionary_1) { if(avaible_1.Key.Length>=4) { string word = avaible_1.Key; int count = avaible_1.Value; counts = counts + count; } //Console.WriteLine("{0}:{1}", word, count); } string avaible_2, avaible_3; avaible_2 = "words: "; Console.WriteLine(avaible_2 + counts); avaible_3 = Convert.ToString(counts); avaible_2 = avaible_2 + avaible_3; StreamWriter file2 = new StreamWriter(fileName2, true); file2.WriteLine(avaible_2); file2.Flush(); file2.Close(); }
统计有效行数
public void count_lines()//统计有效行数 { int count; count = 0; string[] str = File.ReadAllLines(fileName1, System.Text.Encoding.Default); count = str.Length; string avaible_1, avaible_2; avaible_1 = "lines: "; Console.WriteLine(avaible_1+count); avaible_2 = Convert.ToString(count); avaible_1 = avaible_1 + avaible_2; StreamWriter file = new StreamWriter(fileName2, true); file.WriteLine(avaible_1); file.Flush(); file.Close(); }
统计单词数
public void count_words()//统计单词出现次数 { Dictionary<string, int> dictory_1 = new Dictionary<string, int>(); dictory_1 = sortdictionary_desc(dictory_1); Dictionary<string, int> dictory_2 = new Dictionary<string, int>(); int avaible_2; avaible_2 = 0; foreach(KeyValuePair<string, int> sw in dictory_1) { avaible_2++; dictory_2.Add(sw.Key,sw.Value); if (avaible_2 == 10) break; } List<KeyValuePair<string, int>> myList = new List<KeyValuePair<string, int>>(dictory_2); myList.Sort(delegate (KeyValuePair<string, int> s1, KeyValuePair<string, int> s2) { return s1.Key.CompareTo(s2.Key); }); dictory_2.Clear(); foreach (KeyValuePair<string, int> pair in myList) { dictory_2.Add(pair.Key, pair.Value); } foreach (KeyValuePair<string, int> avaible_1 in dictory_2) { string avaible_3 ="<"+ avaible_1.Key+">"; int avaible_4 = avaible_1.Value; Console.WriteLine("{0}:{1}", avaible_3, avaible_4); string sr = Convert.ToString(avaible_4); avaible_3 = avaible_3 + ": "; avaible_3 = avaible_3 + sr; StreamWriter stream = new StreamWriter(fileName2, true); stream.WriteLine(avaible_3); stream.Flush(); stream.Close(); } } public Dictionary<string,int> sortdictionary_desc(Dictionary<string,int> dic)//对字典中的数据按照value排序 { Dictionary<string, int> dictionary_1; List<string> listLines = new List<string>(); StreamReader reader = new StreamReader(@"G:\input.txt"); string line = reader.ReadLine(); while (line != "" && line != null) { listLines.Add(line); line = reader.ReadLine(); } string str1; str1 = ""; for (int i = 0; i < listLines.Count; i++) { str1 = str1 + listLines[i]; } dictionary_1 = new Dictionary<string, int>(); string[] words = Regex.Split(str1, @"\W+"); foreach (string word in words) { if (dictionary_1.ContainsKey(word)) { dictionary_1[word]++; } else { dictionary_1[word] = 1; } } List<KeyValuePair<string, int>> myList = new List<KeyValuePair<string, int>>(dictionary_1); myList.Sort(delegate (KeyValuePair<string, int> s1, KeyValuePair<string, int> s2) { return s2.Value.CompareTo(s1.Value); }); dictionary_1.Clear(); int n = 0; foreach (KeyValuePair<string, int> pair in myList) { if(pair.Key.Length>=4) { n++; dictionary_1.Add(pair.Key, pair.Value); } if (n == 10) break; } return dictionary_1; }
4.代码复审过程。
写了代码后,发现不合题意。我是直接用网上说的正则表达式算出来,发现有的单词是一个字母,然后就改写了代码。
5.计算模块接口部分的性能改进、效能分析。
PSP表格
PSP2.1 |
Personal Software Process Stages |
预估耗时(分钟) |
实际耗时(分钟) |
Planning |
计划 |
30 | 60 |
· Estimate |
· 估计这个任务需要多少时间 |
600 | 972 |
Development |
开发 |
500 | 800 |
· Analysis |
· 需求分析 (包括学习新技术) |
50 | 60 |
· Design Spec |
· 生成设计文档 |
30 | 30 |
· Design Review |
· 设计复审 (和同事审核设计文档) |
30 | 50 |
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
30 | 50 |
· Design |
· 具体设计 |
50 | 100 |
· Coding |
· 具体编码 |
100 | 500 |
· Code Review |
· 代码复审 |
30 | 40 |
· Test |
· 测试(自我测试,修改代码,提交修改) |
10 | 30 |
Reporting |
报告 |
20 | 20 |
· Test Report |
· 测试报告 |
30 | 30 |
· Size Measurement |
· 计算工作量 |
10 | 20 |
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
20 | 30 |
合计 |
455 | 1132 |