混合运算改进(代码)

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();
        }
        //定义变量
        public static int count = -1;
        public static int right = 0;
        public static int sun;
        public static int shi = 0;
        //产生随机数
        private void suiji() 
        {
            Random ra = new Random();
            int h = int.Parse(textBox4.Text) + 1;
            int j = int.Parse(textBox5.Text);
            int a, b, c;
            a = ra.Next(h, j);
            b = ra.Next(h, a);
            c = ra.Next(a, j);
            label2.Text = a.ToString();
            label4.Text = b.ToString();
            label7.Text = c.ToString();           
            textBox1.Text = "";
            count++;
        }
        //随机产生加减乘除
        private void suanfa()
        {
            string[] oper = new string[] { "+", "-", "*", "/" };
            string f = oper[new Random().Next(0, 4)];
            string h = oper[new Random().Next(0, 4)];
            label3.Text = f;
            label6.Text = h;
 
        }
        //得到正确答案!
        private void zhenque()
        {
           
            if (comboBox1.Text == "前两个加括号")
            {
               
            }
            if (comboBox1.Text == "不加括号")
            {
                kuohao()
            }
            if (comboBox1.Text == "后两个加括号")
            {
                
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (shi <= 0)
            {
                timer1.Enabled = false;
                textBox1.Enabled = false;
                MessageBox.Show("时间到!");
                Form2 da = new Form2();
                da.ShowDialog();

            }


            shi = shi - 1;
            textBox2.Text = shi.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            shi = int.Parse(textBox2.Text);
            textBox2.Text = shi.ToString();
            timer1.Enabled = true;
            timer1.Interval = 1000;
            timer1.Start();
            suiji();
            suanfa();

        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //判定数字是否合法!
                int i = 0;
                string an = textBox1.Text;
                if (int.TryParse(an, out i) == false)
                {
                    MessageBox.Show("你输得的数字不合法!");

                }
                //判定是否正确
                zhenque();

                if (textBox1.Text == sun.ToString())
                {
                    right++;
                }
                suiji();
                suanfa();
                //判断是否达到总题数
                int q = int.Parse(textBox3.Text);
                if (count == q)
                {
                    timer1.Enabled = false;
                    textBox3.Enabled = false;
                    Form2 da = new Form2();
                    da.ShowDialog();
                }

            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            textBox3.Enabled = false;
            Form2 da = new Form2();
            da.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string a=comboBox1.Text;
            if (a == "前两个加括号")
            {
                av("(",")","" ,"" );
            }
            if (a == "后两个加括号")
            {
                av("", "", "(", ")");
            }
            if (a == "不加括号")
            {
                av("", "", "", "");
            }
         
        }
        private  void kuohao()
        {
            string f = label3.Text;
            String g = label6.Text;
            int x= int.Parse(label2.Text);
            int y=int.Parse(label4.Text);
            int z= int.Parse(label7.Text);
            string fg = f + g;
            switch (fg)
            {
                case "++":
                    sun =x+y+z;
                    break;
                case "+-":
                    sun = x + y - z;
                    break;
                case "+*":
                    sun = x + y * z;
                    break;
                case "+/":
                    sun = x +y / z;
                    break;
                case "-+":
                    sun = x-y +z;
                    break;
                case "--":
                    sun = x- y - z;
                    break;
                case "-*":
                    sun =  x- y * z;
                    break;
                case "-/":
                    sun =  x- y / z;
                    break;
                case "*+":
                    sun =  x* y +z;
                    break;
                case "*-":
                    sun =  x* y-z;
                    break;
                case "**":
                    sun = x*  y* z;
                    break;
                case "*/":
                    sun =  x*  y/ z;
                    break;
                case "/+":
                    sun =  x/ y+ z;
                    break;
                case "/-":
                    sun = x/  y- z;
                    break;
                case "/*":
                    sun =  x/  y*z;
                    break;
                case "//":
                    sun =  x/ y / z;
                    break;

            }

 
        }
        private void av(string va1,string va2,string va3,string va4)
        {
            label1.Text = va1;
            label5.Text = va2;
            label14.Text = va3;
            label15.Text = va4;
 
        }
       
      
    }
}

 总结

这样写感觉很轻松也很整齐,没有的代码的重复性!代码也简洁了不少,就像老师说的一样有一些代码美,虽然还不够完善!不够美!

还有就是上面的代码有一个地方没有实现,就是有无括号写的正确判断!所以也没有引用自定义的方法!

        private void zhenque()
        {
          
            if (comboBox1.Text == "前两个加括号")
            {
              
            }
            if (comboBox1.Text == "不加括号")
            {
               
            }
            if (comboBox1.Text == "后两个加括号")
            {
               
            }
        }
不知道怎么给自定义的方法传括号的参数
   private  void kuohao()
        {
            string f = label3.Text;
            String g = label6.Text;
            int x= int.Parse(label2.Text);
            int y=int.Parse(label4.Text);
            int z= int.Parse(label7.Text);
            string fg = f + g;
            switch (fg)
            {
                case "++":
                    sun =x+y+z;
                    break;
                case "+-":
                    sun = x + y - z;
                    break;
                case "+*":
                    sun = x + y * z;
                    break;
                case "+/":
                    sun = x +y / z;
                    break;
                case "-+":
                    sun = x-y +z;
                    break;
                case "--":
                    sun = x- y - z;
                    break;
                case "-*":
                    sun =  x- y * z;
                    break;
                case "-/":
                    sun =  x- y / z;
                    break;
                case "*+":
                    sun =  x* y +z;
                    break;
                case "*-":
                    sun =  x* y-z;
                    break;
                case "**":
                    sun = x*  y* z;
                    break;
                case "*/":
                    sun =  x*  y/ z;
                    break;
                case "/+":
                    sun =  x/ y+ z;
                    break;
                case "/-":
                    sun = x/  y- z;
                    break;
                case "/*":
                    sun =  x/  y*z;
                    break;
                case "//":
                    sun =  x/ y / z;
                    break;

            }

 

posted @ 2015-11-12 14:37  小李少  阅读(254)  评论(2编辑  收藏  举报