一 需求分析
本次的作业是基于上一次的作业,来做增量开发.如果把老师当成是用户,那么这次的作业要求就应该是用户需求。作业是创建一个四则混合运算,并要求出现括号,但需求里并没有出现括号应该加在哪里,所以我们把括号出现的位置交给用户来选择,也可已选择没有括号,使其更加的人性化。用户自由程度更高,并且还可以自由的控制计算的时间,题目的总数和出现数字的大小。把这些选择完全的交给用户来做,不管用户是一年级的学生 还是学过括号的学生,这个软件都比较适合他们。
二 设计思路
1 用户第一步可以先自由设定题目的数量(我感觉设定题目数就不应该再设置测试时间,二者应该只存在一个,但我的小伙伴,还是坚持。在他的坚持下,我发现这样做也并不是全无道理,毕竟不设置题目总数还是可以运行,增加了用户的选择空间。) 测试时间,随机数产生的范围,括号出现的位置等。
2 如果用户没有进行相关内容的设置,(PS题目数量用户不设置也可以进行下一步)那么系统会自动进行提示,这样可以使软件与客户的交互性更强,进而更加符合用户的使用习惯。
3 用户相关内容设置完毕,并且点击计时开始按钮,系统会根据用户设定的相关内容,自动产生符合用户的随机数、题目数量和测试时间等,那么,接下来用户就可以进行系统给出的相关内容进行计算。
4 当用户设置的测试时间或者题目数量进行完毕时,那么系统会自动给出提示,并且统计和显示出来题目总数,题目正确数,题目错误数,题目正确率,做题速度等内容。当用户点击结束按钮的时候,程序也会结束,并打开form2窗体。
三 代码书写
Form1代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace sizeyunsuan { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public static int count = 0; //定义题目总数,正确数,四则运算等变量 public static int right = 0; public static int sum; public static int lefttime; public static int m1, m2; public static int time; public void RandomNum() //定义随机产生随机数的方法 { try { Random ran = new Random(); int n1, n2,n3; m1 = Convert.ToInt32(textBox5.Text); m2 = Convert.ToInt32(textBox6.Text); n1 = ran.Next(m1, m2); n2 = ran.Next(m1, n1); n3 = ran.Next(m1, n2); textBox2.Text = n1.ToString(); textBox3.Text = n2.ToString(); textBox1.Text = n3.ToString(); textBox4.Text = ""; } catch (Exception ex) { MessageBox.Show("请输入数值范围"); } } public void fuhao() //定义随机产生符号的方法 { Random ran = new Random(); int n1, n2; n1 = ran.Next(1,5); n2 = ran.Next(1,5); if ((n1 == 1)) { label2.Text = "+"; } if ((n1 == 2)) { label2.Text = "-"; } if ((n1 == 3)) { label2.Text = "*"; } if ((n1 == 4)) { label2.Text = "/"; } if ((n2 == 1)) { label3.Text = "+"; } if ((n2 == 2)) { label3.Text = "-"; } if ((n2 == 3)) { label3.Text = "*"; } if ((n2 == 4)) { label3.Text = "/"; } } private void button1_Click(object sender, EventArgs e) //计时开始按钮的单击事件 { int minute; try { minute = int.Parse(this.textBox7.Text); } catch (System.Exception ex) { this.label8.Text = "输入错误"; return; } lefttime = minute; this.timer1.Interval = 1000; this.timer1.Enabled = true; this.timer1.Start(); fuhao(); RandomNum(); } private void textBox4_KeyDown(object sender, KeyEventArgs e) //触发enter键的事件 { int n = Convert.ToInt32(textBox8.Text); int result; int a = (int.Parse(textBox2.Text)); int b = (int.Parse(textBox3.Text)); int c = (int.Parse(textBox1.Text)); string m = label2.Text + label3.Text; if((label1.Text == "")&&(label13.Text == "")&&(label16.Text == "")&&(label15.Text == "")) //无括号的运算方式 { switch (m) { case "+-": sum = a + b - c; break; case "++": sum = a + b + c; break; case "+*": sum = a + b * c; break; case "+/": sum = a + b * c; break; case "--": sum = a - b - c; break; case "-+": sum = a - b + c; break; case "-*": sum = a - b * c; break; case "-/": sum = a + b /c; break; case "*-": sum = a *b - c; break; case "*+": sum = a * b + c; break; case "**": sum = a * b * c; break; case "*/": sum = a *b /c; break; case "/-": sum = a / b - c; break; case "/+": sum = a / b + c; break; case "/*": sum = a / b * c; break; case "//": sum = a / b /c; break; default: break; } } if ((label1.Text == "(") && (label13.Text == ")") ) //前两个数字有括号的方法 { switch (m) { case "+-": sum = (a + b) - c; break; case "++": sum = (a + b )+ c; break; case "+*": sum = (a + b) * c; break; case "+/": sum = (a + b) * c; break; case "--": sum = (a - b) - c; break; case "-+": sum = (a - b) + c; break; case "-*": sum = (a - b) * c; break; case "-/": sum = (a + b) / c; break; case "*-": sum = (a * b) - c; break; case "*+": sum = (a * b) + c; break; case "**": sum = (a * b) * c; break; case "*/": sum = (a * b) / c; break; case "/-": sum = (a / b )- c; break; case "/+": sum = (a / b) + c; break; case "/*": sum = (a / b) * c; break; case "//": sum = (a / b) / c; break; default: break; } } if ((label16.Text == "(") && (label15.Text == ")")) //后两个数字有括号 { switch (m) { case "+-": sum = a + (b - c); break; case "++": sum = a + (b + c); break; case "+*": sum = a + (b * c); break; case "+/": sum = a + (b * c); break; case "--": sum = a - (b - c); break; case "-+": sum = a - (b + c); break; case "-*": sum = a - (b * c); break; case "-/": sum = a + b / c; break; case "*-": sum = a * (b - c); break; case "*+": sum = a * (b + c); break; case "**": sum = a * (b * c); break; case "*/": sum = a *( b / c); break; case "/-": sum = a / (b - c); break; case "/+": sum = a /( b + c); break; case "/*": sum = a / (b * c); break; case "//": sum = a / (b / c); break; default: break; } } if (e.KeyCode == Keys.Enter) //判断输入值是否为数字 { if (int.TryParse(textBox4.Text, out result) == false) { MessageBox.Show("请输入数字"); count++; } else { if (textBox4.Text == sum.ToString()) { right++; count++; RandomNum(); fuhao(); } else { count++; RandomNum(); fuhao(); } } } if (n == count) //判断题目数量,是否结束运算 { MessageBox.Show("题目答完,请重新开始"); textBox3.Enabled = false; Form2 frm2 = new Form2(); frm2.ShowDialog(); this.Close(); } } private void button2_Click(object sender, EventArgs e) //点击结束按钮事件 { textBox3.Enabled = false; Form2 frm2 = new Form2(); frm2.ShowDialog(); this.Close(); } private void textBox1_TextChanged(object sender, EventArgs e) { this.label3.Text = this.textBox1.Text; } public void timer1_Tick(object sender, EventArgs e )//定义计时器 { time = Convert.ToInt32(textBox7.Text); if (lefttime <= 0) { timer1.Enabled = false; MessageBox.Show("答题时间到!"); textBox4.Enabled = false; Form2 frm2 = new Form2(); frm2.ShowDialog(); } this.label8.Text = "剩余时间" + lefttime.ToString() + "秒"; lefttime--; } private void button3_Click(object sender, EventArgs e) { label1.Text = "("; label13.Text = ")"; } private void button4_Click(object sender, EventArgs e) { label16.Text = "("; label15.Text = ")"; } private void button5_Click(object sender, EventArgs e) { label1.Text = ""; label13.Text = ""; label16.Text = ""; label15.Text = ""; } } }
Form2代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace sizeyunsuan { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { textBox1.Text = Form1.count.ToString(); textBox2.Text = Form1.right.ToString(); textBox3.Text = ((Form1.count - (double)(Form1.right))).ToString(); textBox4.Text = ((Form1.right / (double)(Form1.count)) * 100).ToString("f2") + "%"; textBox5.Text = (((Form1.time) - (Form1.lefttime)) / (double)(Form1.count)).ToString("f2"); textBox6.Text = ((Form1.time) - (Form1.lefttime)).ToString(); } } }
四 PSP分析
五 代码测试
六 这是我们一起工作的照片。
八 总结
这次的增量开发,实在是太难了,我们两个写了好多的代码,考虑了很多的计算组合,一条一条的写,想想都感觉都头疼,不过当软件做出来的时候,那种感觉也是超爽的。
一:基础知识很重要,一定要掌握牢固。
二:两人的配合也很重要。
三:在开发的过程中,如果有什么问题,两个人一定要及时的沟通。