c#语言windowform计算器,参考代码

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
      
        
       

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            MessageBox.Show("确定要关闭吗?");
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
       /* private void button13_Click(object sender, EventArgs e)  //事件主体,事件数据
        {
            Button btn = (Button)sender;

            if (textBox1.Text == "0")
            {
                textBox1.Text = btn.Text;
            }
            else
            {
                textBox1.Text += btn.Text;
            }
        }*/
        private void button21_Click(object sender, EventArgs e)
        {
            textBox1.Text = "0";
            textBox2.Text = "";
        }
            //记录是否刚点过运算符 
            //记录结果
           //记录上一次的运算符
           //记录表达式
       private bool isok=true;
       private decimal sum;
       private string yunsuanfu;
       private string biaodashi;

      //当点击0的时候,不管有没有点击运算符,还是显示0
       private void button26_Click(object sender, EventArgs e)
       {
           if (isok)
           {
               textBox1.Text = "0";
               //isok = false;
           }
           else 
           {
               textBox1.Text = "0";
           }

       }
       //当点击1的时候,不管没有点击运算符,那就显示1,isok设为false,就是说如果没有点击运算符。就可以累加输入比如说输入123
       private void button13_Click(object sender, EventArgs e)
       {
           if (isok)
           {
               textBox1.Text = "1";
              isok = false;
           }
           else
           {
               textBox1.Text += "1";
           }
       }

       private void button12_Click(object sender, EventArgs e)
       {
           if (isok)
           {
               textBox1.Text = "2";
               isok = false;
           }
           else
           {
               textBox1.Text += "2";
           }
       }

       private void button28_Click(object sender, EventArgs e)
       {
           if (isok)
           {
               textBox1.Text = "3";
               isok = false;
           }
           else
           {
               textBox1.Text += "3";
           }
       }

       private void button16_Click(object sender, EventArgs e)
       {
           if (isok)
           {
               textBox1.Text = "4";
               isok = false;
           }
           else
           {
               textBox1.Text += "4";
           }
       }

       private void button15_Click(object sender, EventArgs e)
       {
           if (isok)
           {
               textBox1.Text = "5";
               isok = false;
           }
           else
           {
               textBox1.Text += "5";
           }
       }

       private void button25_Click(object sender, EventArgs e)
       {
           if (isok)
           {
               textBox1.Text = "6";
               isok = false;
           }
           else
           {
               textBox1.Text += "6";
           }
       }

       private void button19_Click(object sender, EventArgs e)
       {
           if (isok)
           {
               textBox1.Text = "7";
               isok = false;
           }
           else
           {
               textBox1.Text += "7";
           }
       }

       private void button18_Click(object sender, EventArgs e)
       {
           if (isok)
           {
               textBox1.Text = "8";
               isok = false;
           }
           else
           {
               textBox1.Text += "8";
           }
       }

       private void button17_Click(object sender, EventArgs e)
       {
           if (isok)
           {
               textBox1.Text = "9";
               isok = false;
           }
           else
           {
               textBox1.Text += "9";
           }
       }
        //当点击加号的时候,
       private void button22_Click(object sender, EventArgs e)
       {
           if (yunsuanfu == null) //如果还没有输入运算符,
           {
               sum = decimal.Parse(textBox1.Text);  //显示结果是输入的数
               biaodashi = textBox1.Text;   //显示的表达式也是输入的数
           }
           else    //如果点击了运算符
           {
               if (!isok)  //如果点击了加号,而又没有再点击其他运算符就开始下面的运算过程
               {
                   if (yunsuanfu == "+")
                   {
                       sum = sum + decimal.Parse(textBox1.Text);
                   }
                   if (yunsuanfu == "-")
                   {
                       sum = sum - decimal.Parse(textBox1.Text);
                   }
                   if (yunsuanfu == "*")
                   {
                       sum = sum * decimal.Parse(textBox1.Text);
                   }
                   if (yunsuanfu == "/")
                   {
                       sum = sum / decimal.Parse(textBox1.Text);
                   }
                   if (yunsuanfu == "%")
                   {
                       sum = sum % decimal.Parse(textBox1.Text);
                   }
                   biaodashi = biaodashi + yunsuanfu + textBox1.Text;  //表达式的格式
               }
           }
           textBox2.Text = biaodashi + "+"; //表达式在textBox2中显示
           yunsuanfu = "+";
           isok = true;   //把isok设为true表示已经点了运算符,不能在点另一个运算符
           textBox1.Text = sum.ToString(); //在textBox1中显示结果
       }
        //当点击减号的时候
       private void button27_Click(object sender, EventArgs e)
       {
           if (yunsuanfu == null)
           {
               sum = decimal.Parse(textBox1.Text);
               biaodashi = textBox1.Text;
           }
           else
           {
               if (!isok)
               {
                   if (yunsuanfu == "+")
                   {
                       sum = sum + decimal.Parse(textBox1.Text);
                   }
                   if (yunsuanfu == "-")
                   {
                       sum = sum - decimal.Parse(textBox1.Text);
                   }
                   if (yunsuanfu == "*")
                   {
                       sum = sum * decimal.Parse(textBox1.Text);
                   }
                   if (yunsuanfu == "/")
                   {
                       sum = sum / decimal.Parse(textBox1.Text);
                   }
                   if (yunsuanfu == "%")
                   {
                       sum = sum % decimal.Parse(textBox1.Text);
                   }
                   biaodashi = biaodashi + yunsuanfu + textBox1.Text;
               }
           }
           textBox2.Text = biaodashi + "-";
           yunsuanfu = "-";
           isok = true;
           textBox1.Text = sum.ToString();
       }
        //当点击乘号的时候
       private void button23_Click(object sender, EventArgs e)
       {

           if (yunsuanfu == null)
           {
               sum = decimal.Parse(textBox1.Text);
               biaodashi = textBox1.Text;
           }
           else
           {
               if (!isok)
               {
                   if (yunsuanfu == "+")
                   {
                       sum = sum + decimal.Parse(textBox1.Text);
                   }
                   if (yunsuanfu == "-")
                   {
                       sum = sum - decimal.Parse(textBox1.Text);
                   }
                   if (yunsuanfu == "*")
                   {
                       sum = sum * decimal.Parse(textBox1.Text);
                   }
                   if (yunsuanfu == "/")
                   {
                       sum = sum / decimal.Parse(textBox1.Text);
                   }
                   if (yunsuanfu == "%")
                   {
                       sum = sum % decimal.Parse(textBox1.Text);
                   }
                   biaodashi = biaodashi + yunsuanfu + textBox1.Text;
               }
           }
           textBox2.Text = biaodashi + "*";
           yunsuanfu = "*";
           isok = true;
           textBox1.Text = sum.ToString();

       }

       private void button8_Click(object sender, EventArgs e)
       {
           if (yunsuanfu == null)
           {
               sum = decimal.Parse(textBox1.Text);
               biaodashi = textBox1.Text;
           }
           else
           {
               if (!isok)
               {
                   if (yunsuanfu == "+")
                   {
                       sum = sum + decimal.Parse(textBox1.Text);
                   }
                   if (yunsuanfu == "-")
                   {
                       sum = sum - decimal.Parse(textBox1.Text);
                   }
                   if (yunsuanfu == "*")
                   {
                       sum = sum * decimal.Parse(textBox1.Text);
                   }
                   if (yunsuanfu == "/")
                   {
                       sum = sum / decimal.Parse(textBox1.Text);
                   }
                   if (yunsuanfu == "%")
                   {
                       sum = sum % decimal.Parse(textBox1.Text);
                   }
                   biaodashi = biaodashi + yunsuanfu + textBox1.Text;
               }
           }
           textBox2.Text = biaodashi + "/";
           yunsuanfu = "/";
           isok = true;
           textBox1.Text = sum.ToString();
       }
        //当点击取余号的时候
       private void button9_Click(object sender, EventArgs e)
       {
           if (yunsuanfu == null)
           {
               sum = decimal.Parse(textBox1.Text);
               biaodashi = textBox1.Text;
           }
           else
           {
               if (!isok)//只有不是刚点过运算符的时候,才会进行下面的运算
               {
                   if (yunsuanfu == "+")
                   {
                       sum = sum + decimal.Parse(textBox1.Text);
                   }
                   if (yunsuanfu == "-")
                   {
                       sum = sum - decimal.Parse(textBox1.Text);
                   }
                   if (yunsuanfu == "*")
                   {
                       sum = sum * decimal.Parse(textBox1.Text);
                   }
                   if (yunsuanfu == "/")
                   {
                       sum = sum / decimal.Parse(textBox1.Text);
                   }
                   if (yunsuanfu == "%")
                   {
                       sum = sum % decimal.Parse(textBox1.Text);
                   }
                   biaodashi = biaodashi + yunsuanfu + textBox1;
               }
           }
           textBox2.Text = biaodashi + "%";
           yunsuanfu = "%";
           isok = true;
           textBox1.Text = sum.ToString();
       }
 //当点击后退按钮的时候,触发这个事件
       private void button3_Click(object sender, EventArgs e)
       {
           if (!isok)
           {
           if (textBox1.Text.Length == 1) //如果textbox1里显示只有一个数字时,显示下面的结果,把isok设为true,意思是已经点击过运算符,再点击其他数字的时候0才会没有
           {
               textBox1.Text = "0";
               isok = true;
           }
           else   //截取字符串,每点击一次,截取一位
           {
               textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
           }  
           }
       }
        //当点击小数点的时候
       private void button14_Click(object sender, EventArgs e)
       {
           if (textBox1.Text =="0")
           {
               textBox1.Text = "0.";
           }
           if (!isok&&!textBox1.Text.Contains("."))
           {
               textBox1.Text = textBox1.Text + ".";
           }
           isok = false;
       }
        //当点击等于号的时候
       private void button11_Click(object sender, EventArgs e)
       {
           textBox2.Text = " ";
           if (yunsuanfu == "+")
           {
               sum = sum + decimal.Parse(textBox1.Text);
           }
           if (yunsuanfu == "-")
           {
               sum = sum - decimal.Parse(textBox1.Text);
           }
           if (yunsuanfu == "*")
           {
               sum = sum * decimal.Parse(textBox1.Text);
           }
           if (yunsuanfu == "/")
           {
               sum = sum / decimal.Parse(textBox1.Text);
           }
           if (yunsuanfu == "%")
           {
               sum = sum % decimal.Parse(textBox1.Text);
           }
           textBox1.Text = sum.ToString ();
           isok = true;
           yunsuanfu = null;
           sum = 0;
       }
        //当点击后退键的时候
       private void button20_Click(object sender, EventArgs e)
       {
           if (!isok)
           {
               if (decimal.Parse(textBox1.Text) > 0)
               {
                    textBox1.Text  ="-"+ textBox1.Text ;
               }
               else
               {
                   textBox1.Text = textBox1.Text.Substring(1);
               }
           }
       }

       

    }
}
原文地址:https://www.cnblogs.com/275147378abc/p/4533554.html