结对作业
合作者:201631062101,201631062103
代码地址:https://gitee.com/Yuuotsu
本次作业的链接地址:https://edu.cnblogs.com/campus/xnsy/2018Systemanalysisanddesign/homework/2188
PHP表格
PSP2.1 |
PSP阶段 |
预估耗时 (分钟) |
实际耗时 (分钟) |
Planning |
计划 |
30 |
30 |
· Estimate |
· 估计这个任务需要多少时间 |
20 |
20 |
Development |
开发 |
360 |
600 |
· Analysis |
· 需求分析 (包括学习新技术) |
30 |
20 |
· Design Spec |
· 生成设计文档 |
0 |
0 |
· Design Review |
· 设计复审 (和同事审核设计文档) |
60 |
30 |
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
30 |
20 |
· Design |
· 具体设计 |
60 |
60 |
· Coding |
· 具体编码 |
300 |
420 |
· Code Review |
· 代码复审 |
120 |
150 |
· Test |
· 测试(自我测试,修改代码,提交修改) |
120 |
120 |
Reporting |
报告 |
120 |
60 |
· Test Report |
· 测试报告 |
30 |
20 |
· Size Measurement |
· 计算工作量 |
30 |
40 |
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
60 |
30 |
|
合计 |
1370 |
1620 |
代码互审情况
同伴代码注释较少,看着有点吃力,但逻辑条理清晰。
设计过程
项目截图:
统计数据方法说明:
统计字符数:直接统计文件的长度
统计字数:使用正则表达式统计
正则表达式的手册:http://tool.oschina.net/uploads/apidocs/jquery/regexp.html
统计行数:逐行读取,行数加一
统计空行数: 若一行为空或者这一行的长度为0则空行数加一
统计注释行:统计//的注释行数,若两个相邻的字符都为‘/’,则注释行加一
统计代码行:总行数-空行数-注释行=代码行
流程图:
代码说明
类CountMethod 统计各种数据
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace NewWordCount { class CountMethod { public static int wordNum; public static int lineNum; public static int charNum; public static int emptyNum; public static int commentNum; public static int codeNum; //统计字符数 public static void countChar (){ string content = Form1.Content; Console.WriteLine(content); charNum = content.Length;//字符即文件内容字符串的长度 Console.WriteLine("字符数 " + charNum); } //统计字数(1) public static void countWord(){ string content = Form1.Content; wordNum = Regex.Matches(content, "\\s+").Count + 1;//通过正则表达式分隔字符串 统计分割后的字符串的长度 Console.WriteLine("字数 " + wordNum); } //统计行数(2) public static void countLine() { lineNum = Form1.countLine; Console.WriteLine("行数 " + lineNum); } //统计杂项(代码行/空行/注释行) /* (1)注释行:当读取到“//”(连续两个斜杠)算作注释行 (这里“* /”无法统计) (2)空行:在读取文件时统计 (3)代码行:总行数-空行-注释行 */ public static void countOther() { //空白行 emptyNum = Form1.emptyLine; Console.WriteLine("空行 " + emptyNum); ////注释行 commentNum = Form1.commentLine; Console.WriteLine("注释行2 " + commentNum); //代码行数 countLine(); codeNum = lineNum - emptyNum - commentNum; Console.WriteLine("代码行 " + codeNum); } } }
类Form1 读取文件并统计行数和空行数
private void button1_Click(object sender, EventArgs e) { fileName = string.Empty; //文件名 //string line; //打开文件 OpenFileDialog dlg = new OpenFileDialog(); dlg.DefaultExt = "txt"; dlg.Filter = "Txt Files|*.txt"; if (dlg.ShowDialog() == DialogResult.OK) fileName = dlg.FileName; if (fileName == null) return; //记录行数 StreamReader sr = new StreamReader(fileName, System.Text.Encoding.Default); string line; //Boolean TAG = false; //逐行读取 while((line = sr.ReadLine()) != null) { //if (TAG == true) { // commentLine++; //} if (line == "" || line.Length <= 1) { emptyLine++; } countLine++; for (int i = 0; i < line.Length; i++) { Console.WriteLine("aaaaaaaaa"); if ((line[i] == '/') && (line[i + 1] == '/')) { commentLine++; Console.WriteLine("注释ssssssssssss" + commentLine); continue; } //这里本来统计“/** /”类型 的注释,但是无法打开含有此注释的文件,暂未解决 //else if ((line[i] == '/') && (line[i + 1] == '*')) //{ // TAG = true; // commentLine++; //} //else if ((line[i] == '*') && (line[i + 1] == '/')) //{ // TAG = false; //} } } //记录文件内容 StreamReader sr2 = new StreamReader(fileName, System.Text.Encoding.Default); string content = sr2.ReadToEnd().TrimStart(); Content = content.ToString(); //关闭 sr.Close(); sr2.Close(); Form1_Load(null, null);//刷新文件名字 }
类Form2 输出统计结果
public partial class Form2 : Form { string result1; string result2; string result3; string result4; string result5; string result6; string finalResult; public Form2() { InitializeComponent(); textBox2.Text = Form1.fileName; //输出统计结果 if (Form1.flag1 == true){ CountMethod.countWord(); result1 = "字数:" + CountMethod.wordNum.ToString() + " "; } if (Form1.flag2 == true) { CountMethod.countLine(); result2 = "行数:" + CountMethod.lineNum.ToString() + " "; } if (Form1.flag3 == true) { CountMethod.countChar(); result3 = "字符数:" + CountMethod.charNum.ToString(); } if (Form1.flag4 == true) { CountMethod.countOther(); result4 = "空行数:" + CountMethod.emptyNum.ToString(); } if (Form1.flag5 == true) { CountMethod.countOther(); result5 = "注释数: " + CountMethod.commentNum.ToString(); Console.WriteLine("注释行3 " + result5); } if (Form1.flag6 == true) { CountMethod.countOther(); result6 = "代码数: " + CountMethod.codeNum.ToString(); } finalResult = result1 + result2 + result3 + result4 + result5 + result6; textBox1.Text = finalResult; }
类Form2 输出结果到文件文件
private void button1_Click(object sender, EventArgs e) { //输出文件 FileStream fs = new FileStream("C:\\Users\\13548\\Desktop\\Result.txt", FileMode.Create); //获得字节数组 byte[] data = System.Text.Encoding.Default.GetBytes(finalResult); //开始写入 fs.Write(data, 0, data.Length); //清空缓冲区、关闭流 fs.Flush(); fs.Close(); MessageBox.Show("输出完成!"); this.Close(); Form1 f1 = new Form1(); }
总结
一个人在写代码时往往不容易发现自己的错误,例如对注释的要求和代码规范等。而结对编程对这些要求就较高,自己的代码要给同伴看并且要保证同伴可以看懂,因此在写代码时就有一定的约束,同时结对编程中双方的交流也很重要,对我来说,1+1>2是成立的。