1. 结对编程项目---四则运算 (10分)
基本功能要求:
1) 实现一个带有用户界面的四则运算。2) 生成的题目不能重复。3) 支持负数,例如-1,-1/2,-3‘4/5等。(达成)
需要支持的基本设定参数
1) 题目的数量 2) 数值的范围 3) 题目中最多几个运算符(目前没有达成) 4) 题目中或运算过程中有无有分数(比如进行整数除法的时候不能除尽) 5) 题目中是否有乘除法 6) 题目中是否有括号 (目前没有达成) 7) 题目中或运算过程中有无负数
学习感受:
本次作业难度较大,做的过程中遇到许多问题,通过本次作业,更加深入对组员的了解,也明白了编写过程中合作的重要性。
同组人:李云龙:http://www.cnblogs.com/liyunlong/
form1:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using 四则运算; namespace _RandomNum { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public static int Count = 0; private int t = 60; public static int right = 0; private void button1_Click(object sender, EventArgs e) { label2.Text = t.ToString(); timer1.Enabled = true; timer1.Interval = 1000; timer1.Start(); } private void RDN() { Random rd = new Random(); int r1, r2; if (textBox2.Text == "" && textBox3.Text == "") { MessageBox.Show("请输入取值范围!"); return; } r1 = rd.Next(int.Parse(textBox2.Text), int.Parse(textBox3.Text)); r2 = rd.Next(int.Parse(textBox2.Text), int.Parse(textBox3.Text)); textBox1.Text = r1.ToString(); textBox2.Text = r2.ToString(); string[] fuhao = new string[] { "+", "-", "×", "÷" }; label3.Text = fuhao[rd.Next(0, 5)]; int result = 0; switch (label3.Text) { case "+": result = int.Parse(textBox4.Text) + int.Parse(textBox5.Text); return; case "-": if (int.Parse(textBox4.Text) >= int.Parse(textBox5.Text)) { result = int.Parse(textBox4.Text) - int.Parse(textBox5.Text); } else { MessageBox.Show("请回车进行下一题!此题不计入答题总数!"); } return; case "×": result = int.Parse(textBox5.Text) * int.Parse(textBox6.Text); return; case "÷": if (textBox5.Text == "0") { MessageBox.Show("分母为0,不计入答题总数,请回车继续答题!"); } else { result = int.Parse(textBox5.Text) / int.Parse(textBox6.Text); } return; } } private void RandomNum() { Random ran = new Random(); int n1, n2; if (textBox2.Text == "" && textBox3.Text == "") { MessageBox.Show("请输入取值范围!"); return; } n1 = ran.Next(int.Parse(textBox2.Text), int.Parse(textBox3.Text)); n2 = ran.Next(int.Parse(textBox2.Text), int.Parse(textBox3.Text)); textBox1.Text = n1.ToString(); textBox2.Text = n2.ToString(); textBox3.Text = ""; } private void timer1_Tick(object sender, EventArgs e) { } private void button10_Click(object sender, EventArgs e) { timer1.Stop(); Form2 frm2 = new Form2(); frm2.ShowDialog(); } private void textBox3_KeyDown(object sender, KeyEventArgs e) { int result = 0; string s = label3.Text; if (Count == int.Parse(textBox6.Text)) { Form2 frm2 = new Form2(); frm2.ShowDialog(); } switch (s) { case "+": result = int.Parse(textBox1.Text) + int.Parse(textBox2.Text); break; case "-": if (int.Parse(textBox1.Text) >= int.Parse(textBox2.Text)) { result = int.Parse(textBox1.Text) - int.Parse(textBox2.Text); } else { MessageBox.Show("请回车进行下一题!此题不计入答题总数!"); } break; case "×": result = int.Parse(textBox1.Text) * int.Parse(textBox2.Text); break; case "÷": if (textBox2.Text == "0") { MessageBox.Show("分母为0,不计入答题总数,请回车继续答题!"); } else { result = int.Parse(textBox1.Text) / int.Parse(textBox2.Text); } break; } if (e.KeyCode == Keys.Enter) { if (textBox3.Text == result.ToString()) { right++; Count++; MessageBox.Show("回答正确!"); } else { if (textBox2.Text == "0" || int.Parse(textBox1.Text) - int.Parse(textBox2.Text) < 0) { RandomNum(); } else { MessageBox.Show("答题错误!"); RandomNum(); Count++; } } RandomNum(); } } private void button3_Click(object sender, EventArgs e) { label7.Text = button1.Text; RandomNum(); } private void button4_Click(object sender, EventArgs e) { label7.Text = button2.Text; RandomNum(); } private void button5_Click(object sender, EventArgs e) { label7.Text = button3.Text; RandomNum(); } private void button6_Click(object sender, EventArgs e) { label7.Text = button4.Text; RandomNum(); } } }
form2:
using _RandomNum; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 四则运算 { 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 - Form1.right).ToString(); } } }
程序截图: