作业四 四则运算

 结对编程项目---四则运算

基本功能要求:

1) 实现一个带有用户界面的四则运算。

2) 生成的题目不能重复。

3) 支持负数,例如-1,-1/2,-3‘4/5等。

需要支持的基本设定参数

1) 题目的数量(个人项目的要求)

2) 数值的范围(个人项目的要求)

3) 题目中最多几个运算符

4) 题目中或运算过程中有无有分数(比如进行整数除法的时候不能除尽)

5) 题目中是否有乘除法

6) 题目中是否有括号

7) 题目中或运算过程中有无负数

小组成员:

高亚南   博客地址:http://home.cnblogs.com/u/GGGGGG7/

工作分配:

高亚南:设计窗体,代码规划。

王梓萱:代码规划实现,功能调试。

  可以设定随机整数的范围,那么我首先想到的是在给出随机数的时候设定一个范围,其实很简单。拖出两个文本框,比如textBox4,textBox5,然后让这两个文本框的值转换成int类型,并让Random()方法从给出的这个范围出题。

 对于设定答题的题目数量,添加一个文本框让它存放我要答题的数量,并且判断一下,当答题数量等于预先设定的答题数量时,让它弹出Form2。

选择计算类型时,先用数组把四种运算存了起来,然后从这个数组里随机选出一种运算显示在label3里面,这样就可以switch(label3.Text)了。 

代码如下

Form1.cs

复制代码
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("wrong");
                    }
                    else
                    {
                        result = int.Parse(textBox5.Text) / int.Parse(textBox6.Text);
                    }
                    return;
            }
        }

        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("不符合标准");
                    }
                    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.cs

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 
10 namespace _Random
11 {
12     public partial class Form2 : Form
13     {
14         public Form2()
15         {
16             InitializeComponent();
17         }
18 
19         private void Form2_Load(object sender, EventArgs e)
20         {
21             textBox1.Text = Form1.Count.ToString();
22             textBox2.Text = Form1.right.ToString();
23             textBox3.Text = (Form1.Count - Form1.right).ToString();
24         }
25 
26     }
27 }
复制代码

运行图如下

总结:

在结对编程的时候,我和高亚南俩个先在一起列出了设计思路,然后参照书籍和网上的资料进行操作。

高亚南同学的优点不少,比如有不错的发散思维。缺点是拖拖拉拉。结对编程使原本看起来枯燥乏味的编写代码变得有趣起来,两个人之间的互相监督使得效率会有显著地提升,总而言之,在这次编程中收获颇多,体会到更多学习的乐趣。

原文地址:https://www.cnblogs.com/ccxx/p/5361924.html