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

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

一、基本要求

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

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

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

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

  5. 数值的范围

  6. 题目中最多几个运算符

  7. 题目中是否有乘除法

  8. 题目中或运算过程中有无负数

根据老师的要求我们做的程序中解决了以上的问题。因为在第一次制作的四则运算中我的代码就已经制作了一个用户操作的界面,只是比较粗糙不够完善,所以这次我和我的组员完善了四则运算的界面和一些具体的功能。在这些要求中的运算符有几个的问题里,第一次制作的四则运算的运算符是做了三个,所以这次依旧使用三个运算符,没有进行大的修改。

二、小组成员

小组成员:李云龙、刘科宇

三、源代码

源代码:

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();
         }

    }
    
}

四、运行截图

五、总结

由于我们两个底子薄,本次作业难度便较大,但经过我们俩查资料和请教同学把 本次作业完成了,但是还有很多地方不足希望老师批请指正,通过本次作业我们也认识到编程的重要性,在以后的学习中更加努力。

原文地址:https://www.cnblogs.com/liyunlong/p/5362040.html