四则运算
增量内容:1)处理用户的错误输入,比如输入字母或符号等,处理除法运算中分母为0的情况,处理结果为负数的情况,保证是小学水平不出现数,比如不能出现5-8=-3这种情况;2)用户可以设定倒计时; 3)用户可以设定随机整数的范围和题目数量;4)用户可以选择哪种计算类型,比如加减乘除,或可选择软件随机生成四则运算中的一种; 5)用户可以选择随机生成的题目中是否带有小括号,比如(2+3)*5,如果是gui程序,添加这个功能可以用复选框实现; 6)保证生成过的题目不再重复出现。。
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 _Simple_arithmetic { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public static int Count = 0; public static int right = 0; public static int fault = 0; public static int t; private void RandomNum() { Random ran = new Random(); int a, b; string m; a = ran.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text)); b = ran.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text)); if (a >= b) { textBox1.Text = a.ToString(); textBox2.Text = b.ToString(); textBox3.Text = ""; Count++; textBox6.Text = Form1.Count.ToString(); if (textBox7.Text == textBox6.Text) { MessageBox.Show("你已做完计算!"); } } else if (a < b) { m = b.ToString(); textBox2.Text = a.ToString(); textBox1.Text = m; textBox3.Text = ""; Count++; textBox6.Text = Form1.Count.ToString(); if (textBox7.Text == textBox6.Text) { MessageBox.Show("你已做完计算!"); } } } private void timer1_Tick(object sender, EventArgs e) { if (Convert.ToInt32(textBox8.Text) <= 0) { timer1.Enabled = false; textBox3.Enabled = false; MessageBox.Show("时间到!"); textBox3.Enabled = false; Form2 frm2 = new Form2(); frm2.ShowDialog(); } int t = Convert.ToInt32(textBox8.Text); t = t - 1; textBox8.Text = t.ToString(); } private void textBox3_KeyDown(object sender, KeyEventArgs e) { int sum = 0; string m = label3.Text; if (m == "+") { sum = int.Parse(textBox1.Text) + int.Parse(textBox2.Text); if (e.KeyCode == Keys.Enter) { if (textBox3.Text == sum.ToString()) { right++; RandomNum(); } else { fault++; RandomNum(); } } } else if (m == "-") { sum = int.Parse(textBox1.Text) - int.Parse(textBox2.Text); if (e.KeyCode == Keys.Enter) { if (textBox3.Text == sum.ToString()) { right++; RandomNum(); } else { fault++; RandomNum(); } } } else if (m == "x") { sum = int.Parse(textBox1.Text) * int.Parse(textBox2.Text); if (e.KeyCode == Keys.Enter) { if (textBox3.Text == sum.ToString()) { right++; RandomNum(); } else { fault++; RandomNum(); } } } else { sum = int.Parse(textBox1.Text) / int.Parse(textBox2.Text); } } private void button1_Click(object sender, EventArgs e) { label3.Text = "+"; int t = Convert.ToInt32(textBox8.Text); textBox8.Text = t.ToString(); timer1.Enabled = true; timer1.Interval = 1000; timer1.Start(); RandomNum(); } private void button2_Click(object sender, EventArgs e) { label3.Text = "-"; int t = Convert.ToInt32(textBox8.Text); textBox8.Text = t.ToString(); timer1.Enabled = true; timer1.Interval = 1000; timer1.Start(); RandomNum(); } private void button3_Click(object sender, EventArgs e) { label3.Text = "x"; int t = Convert.ToInt32(textBox8.Text); textBox8.Text = t.ToString(); timer1.Enabled = true; timer1.Interval = 1000; timer1.Start(); RandomNum(); } private void button4_Click(object sender, EventArgs e) { label3.Text = "/"; int t = Convert.ToInt32(textBox8.Text); textBox8.Text = t.ToString(); timer1.Enabled = true; timer1.Interval = 1000; timer1.Start(); Random ran = new Random(); int a, b; string m; a = ran.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text)); b = ran.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text)); if (b != 0) { textBox1.Text = a.ToString(); textBox2.Text = b.ToString(); textBox3.Text = ""; Count++; textBox6.Text = Form1.Count.ToString(); if (textBox7.Text == textBox6.Text) { MessageBox.Show("你已做完计算!"); } } else { m = b.ToString(); textBox2.Text = a.ToString(); textBox1.Text = m; textBox3.Text = ""; Count++; textBox6.Text = Form1.Count.ToString(); if (textBox7.Text == textBox6.Text) { MessageBox.Show("你已做完计算!"); } } } private void button7_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); frm2.ShowDialog(); } private void Form1_Load(object sender, EventArgs e) { } private void textBox4_TextChanged(object sender, EventArgs e) { int result; if (int.TryParse(textBox4.Text, out result) == false) { if (!(textBox4.Text == "")) { MessageBox.Show("请输入数字!", "提示"); textBox4.Clear(); } } } private void textBox5_TextChanged(object sender, EventArgs e) { int result; if (int.TryParse(textBox5.Text, out result) == false) { if (!(textBox5.Text == "")) { MessageBox.Show("请输入数字!", "提示"); textBox5.Clear(); } } } private void textBox7_TextChanged(object sender, EventArgs e) { int result; if (int.TryParse(textBox7.Text, out result) ==false) { if (!(textBox7.Text == "")) { MessageBox.Show("请输入数字!", "提示"); textBox7.Clear(); } } } private void button5_Click(object sender, EventArgs e) { textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); textBox4.Clear(); textBox5.Clear(); textBox7.Clear(); } } }
测试截图
结对编程时的照片
设计思路:上次作业我们用的是控制台,这次原本还想用控制台,但是毛连成建议用窗体,他这块学的比较好。所以这次就按照原来的基础上进行修改。因为是在之前的程序上新加了一些功能,所以我们这次还好,没有感到无从下手。中间也遇到了不少困难,但是很快就解决了,完成其它实现的时候我们也会有一些分歧,不如要不要做个提示告诉用户在同一个界面上出现你已经做了多少道题,当然经过商量,也很快解决了,同时我们也对界面进行美化设计,虽然不太美观,但这次又是一次进步!
需求分析:这次的主要考虑到是保证小学水平,所以界面我们做的相对全面,能想到的就加上了,虽然还是不够好,但我们也是尽力了,这个也比较费脑力啊,我们还得想到用户会用到什么功能,会不会有些多余的,等等,还是比较有挑战性的。
PSP耗时
总结:每次的合作都会有不小的进步,通过两人的共同思考,分析,交流。在面对困难的时候,我们就会很快地解决,不得不说,结对编程这种方式还是很适用的。当然,我的伙伴也是很了不起的,他的思维比较严谨,编码出错后,也能很快找到解决之法,和他一起编程,自己也会跟着进步,同时也对编程语言有了进一步的认识!也感谢老师们的教育方式!