第一阶段结对作业验收小学生四则运算:

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 小学算数

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private int ti_shu, result, f1, f2;

 

        private void chu_ti()

        {

            Random suiji = new Random();

            int a = suiji.Next(0, 10);

            int b = suiji.Next(1, 10);

            int c = suiji.Next(0, 10);

            int d = suiji.Next(1, 10);

            int p = 4;

            //int p = suiji.Next(0, 7);

            if (p == 0)//出加法题

            {

                label1.Text = a.ToString() + "+" + b.ToString() + "=";

                result = a + b;

                f1 = result;

                f2 = 1;

            }

            if (p == 1) //出减法题

            {

                label1.Text = a.ToString() + "-" + b.ToString() + "+" + c.ToString() + "-" + d.ToString();

                result = a - b;

                f1 = result;

                f2 = 1;

            }

            ti_shu = ti_shu + 1;

            textBox1.Text = "";

            if (p == 2) //出乘法题

            {

                label1.Text = a.ToString() + "*" + b.ToString() + "=";

                result = a * b;

                f1 = result;

                f2 = 1;

            }

            if (p == 3) //出除法题

            {

                label1.Text = a.ToString() + "/" + b.ToString() + "=";

                f1 = a;

                f2 = b;

                int g;

                g = Gcd(f1, f2);

                if (g>1)

                {

                    f1 = f1 / g;

                    f2 = f2 / g;

                }

                if (f2==1)

                {

                    result = f1;

                }

 

 

            }

            if (p == 4) //出分数加法题

            {

                if (a > b)

                {

                    int t = a; a = b; b = t;

                }

                if (c > d)

                {

                    int t = c; c = d; d = t;

                }

                label1.Text = a.ToString() + "/" + b.ToString() + "+" + c.ToString() + "/" + d.ToString() + "=";

                f1 = a * d + b * c;

                f2 = b * d;

                int g;

                g = Gcd(f1, f2);

                f1 = f1 / g;

                f2 = f2 / g;

                result = f1 / f2;

                if (f1 == 0)

                {

                    result = 0;

                }

                if (f2 == 1)

                {

                    result = f1;

                }

            }

            if (p == 5) //出分数减法题

            {

                if (a > b)

                {

                    int t = a; a = b; b = t;

                }

                if (c > d)

                {

                    int t = c; c = d; d = t;

                }

                label1.Text = a.ToString() + "/" + b.ToString() + "-" + c.ToString() + "/" + d.ToString() + "=";

                f1 = a * d - b * c;

                f2 = b * d;

                int g;

                g=Gcd(f1, f2);

                f1 = f1 / g;

                f2 = f2 / g;

                result = f1 / f2;

                if (f1 == 0)

                {

                    result = 0;

                }

                if (f2 == 1)

                {

                    result = f1;

                }

 

            }

            if (p == 6) //出分数乘法题

            {

                if (a > b)

                {

                    int t = a; a = b; b = t;

                }

                if (c > d)

                {

                    int t = c; c = d; d = t;

                }

                label1.Text = a.ToString() + "/" + b.ToString() + "*" + c.ToString() + "/" + d.ToString() + "=";

                f1 = a * c;

                f2 = b * d;

                int g;

                g = Gcd(f1, f2);

                f1 = f1 / g;

                f2 = f2 / g;

                result = f1 / f2;

                if (f1 == 0)

                {

                    result = 0;

                }

                if (f2 == 1)

                {

                    result = f1;

                }

            }

            if (p == 7) //出分数除法题

            {

                if (c == 0)

                {

                    c++;

                }

                if (a > b)

                {

                    int t = a; a = b; b = t;

                }

                if (c > d)

                {

                    int t = c; c = d; d = t;

                }

                label1.Text = a.ToString() + "/" + b.ToString() + " " + "/" + " " + c.ToString() + "/" + d.ToString() + "=";

                f1 = a * d;

                f2 = b * c;

                int g;

                g = Gcd(f1, f2);

                f1 = f1 / g;

                f2 = f2 / g;

                if (f1 == 0)

                {

                    result = 0;

                }

                if (f2 == 1)

                {

                    result = f1;

                }

                //result = f1 / f2;

            }

        }

 

        private void label1_Click(object sender, EventArgs e)

        {

 

        }

 

 

        int Gcd(int a, int b)//求最大公约数,以便对结果进行约分

        {

            while (a != 0 && b != 0)

            {

                if (a > b) a = a % b;

                else if (a < b) b = b % a;

            }

            return (a > b ? a : b);

        }

 

 

        private void Form1_Load(object sender, EventArgs e)

        {

            ti_shu = 0;

            chu_ti();

        }

 

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)

        {

            string Item;

            /*if (pp <= 3 || f1 == 0 || f2 == 1)

            {

                if (e.KeyChar == 13)//表示按下的回车键

                {

                    if (Convert.ToInt16(textBox1.Text) == result)

                    {

                        Item = label1.Text + textBox1.Text + "√";

 

                    }

                    else

                        Item = label1.Text + textBox1.Text + "×";            

                    this.listBox1.Items.Add(Item);

                    this.textBox1.Text = "";

                    chu_ti();

                    }

                    }

                     else

                     {*/

                         if (e.KeyChar == 13)//表示按下的回车键

                {

                    if (f2==1||f1==0)

                    {

                        if (Convert.ToInt16(textBox1.Text) == result)

                        {

                            Item = label1.Text + textBox1.Text + "√";

                            this.listBox1.Items.Add(Item);

                        }

                        else

                        {

                            Item = label1.Text + textBox1.Text + "×";

                            this.listBox1.Items.Add(Item);

                            this.listBox2.Items.Add(Item);

                        }

                            

                    }

                    else

                    {

                        if (Convert.ToString(textBox1.Text) == Convert.ToString(f1) + "/" + Convert.ToString(f2))

                        {

                            Item = label1.Text + textBox1.Text + "√";

                            this.listBox1.Items.Add(Item);

                        }

                        else

                        {

                            Item = label1.Text + textBox1.Text + "×";

                            this.listBox1.Items.Add(Item);

                            this.listBox2.Items.Add(Item);

                        }

                            

                    }

                     

                              

                             this.textBox1.Text = "";

                             chu_ti();

                         }

                          

                }

 

        private void button1_Click(object sender, EventArgs e)

        {

            Application.Exit();

        }

            }

 

        }

posted @ 2019-01-10 22:29  言宇  阅读(118)  评论(0编辑  收藏  举报