码迷,mamicode.com
首页 > 编程语言 > 详细

四则运算法

时间:2015-10-02 14:54:46      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

1.  窗体设计好,如下图所示:

技术分享

2. 定义总计和正确的变量 运算的方法。

3. 给加减乘除四个按钮的单击事件分别赋值lable1为+,-,*,/。

4. 给总计,正确,正确率这三个textBox赋值,并引用运算的方法。

其代码如下:

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 szys.c

{   

  public partial class Form1 : Form  

   {  

       public Form1()      

   {          

   InitializeComponent();      

   }

       private static int     

        Count = 0,     

       right = 0;       

  //总计的个数和正确的个数   

      private void ys()     

    {        

     Random rn = new Random();      

       int i, j;     

        i = rn.Next(11);     

        j = rn.Next(1, 11);         

        textBox1.Text = i.ToString();

         textBox2.Text = j.ToString();    

         textBox3.Text = "";     

    }        //算法的方法        

private void button1_Click_1(object sender, EventArgs e)     

    {        

     label1.Text = "+";       

      label1.Visible = true;       

  }

        private void button2_Click_1(object sender, EventArgs e)     

    {          

                     label1.Text = "-";       

                   label1.Visible = true;     

    }

        private void button3_Click(object sender, EventArgs e)       

  {          

                    label1.Text = "*";        

                            label1.Visible = true;   

      }

        private void button4_Click(object sender, EventArgs e)       

  {         

                      label1.Text = "/";         

                         label1.Visible = true;   

      } //四个button的单击事件,分别赋值lable1为+,-,*,/      

   private void button5_Click_1(object sender, EventArgs e)

        {      

                   ys();       

              } //开始的单击事件,运用算法     

    private void button6_Click_1(object sender, EventArgs e)      

   {     

        textBox3.Enabled = false;   

          MessageBox.Show("运算结束!");   

      }         //结束的单击事件        

private void textBox3_KeyDown_1(object sender, KeyEventArgs e)   

      {

                 if (label1.Text == "+")      

       {                

                         int sum;         

                      sum = int.Parse(textBox1.Text) + int.Parse(textBox2.Text);   

              if (e.KeyCode == Keys.Enter)            

     {              

       if (textBox3.Text == sum.ToString())  

                   {       right++;         

                         Count++;            

                                  }             

        else                

                 Count++;    

                 textBox4.Text = Count.ToString();//总计值的赋值                                         textBox4.Enabled = false;

                    textBox5.Text = right.ToString();//正确值的赋值                     textBox5.Enabled = false;                     

textBox6.Text = ((right / (double)Count) * 100).ToString() + "%";//正确率的赋值                     textBox6.Enabled = false;    

                 ys();      

           }         

    }      

       //以下三个赋值同上     

        else if (label1.Text=="-")     

        {     int cha;     

            cha = int.Parse(textBox1.Text) - int.Parse(textBox2.Text);   

              if (e.KeyCode == Keys.Enter)     

            {             

        if (textBox3.Text == cha.ToString())     

                {             

            right++;               

          Count++;                

     }                   

  else          

               Count++;      

               textBox4.Text = Count.ToString();    

                 textBox4.Enabled = false;       

              textBox5.Text = right.ToString();     

                textBox5.Enabled = false;             

                textBox6.Text = ((right / (double)Count) * 100).ToString() + "%";                     textBox6.Enabled = false;              

                 ys();            

          }        

     }

           else  if (label1.Text=="*")     

        {               

              int qj;        

         qj  = int.Parse(textBox1.Text) * int.Parse(textBox2.Text);   

              if (e.KeyCode == Keys.Enter)       

          {                 

    if (textBox3.Text == qj.ToString())    

                 {              

           right++;             

            Count++;          

           }          

           else         

                Count++;      

               textBox4.Text = Count.ToString();  

                   textBox4.Enabled = false;

                    textBox5.Text = right.ToString();   

                  textBox5.Enabled = false;         

            textBox6.Text = ((right / (double)Count) * 100).ToString() + "%";                     textBox6.Enabled = false;    

                 ys();        

         }            

 

}           

  if (label1.Text=="/")     

        {           

      double chu;         

        chu  = int.Parse(textBox1.Text) /int.Parse(textBox2.Text);  

               if (e.KeyCode == Keys.Enter)      

           {      

               if (double.Parse (textBox3.Text)==chu )   

                  {    

                     right++;   

                      Count++;  

                   }           

          else                  

       Count++;            

         textBox4.Text = Count.ToString();    

                 textBox4.Enabled = false;      

               textBox5.Text = right.ToString();   

                  textBox5.Enabled = false;          

           textBox6.Text = ((right / (double)Count) * 100).ToString() + "%";                     textBox6.Enabled = false;       

              ys();           

      }           

  }            

        }

                   } }

5. 运行时选择要使用的运算符号,然后点击开始,即开始算题,按enter键继续下一题,点击结束,即运算结束,总计的个数和正确数,正确率都随计算的过程,个数而出现不同结果。

 

运算过程还有点缺陷,比如进行了加法运算结束后,需要关闭运行,再次运行程序才能进行其他的运算。希望老师给点意见 ,该怎么改进。

 

四则运算法

标签:

原文地址:http://www.cnblogs.com/xueyanan/p/4852213.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!