用winform编写简易计算器

 public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }         private string FuHao;  //定义操作符号(+,-,*,/)         private string num1;   //定义第一个参数(就是参与运算的第一个数)         private string num2;   //定义第二个参数         private string oldValue = "0";   //定义老参数

        public void InputName(string value)         {             if (oldValue != "0")             {                 txt_Vlaue.Text = oldValue + value;             }             else             {                 if (value == ".")                 {                     txt_Vlaue.Text = "0" + value;                 }                 else                 {                     txt_Vlaue.Text = value;                 }             }             oldValue = txt_Vlaue.Text;

            if (FuHao != null && FuHao.ToString().Trim() != string.Empty)             {                 num2 = txt_Vlaue.Text;             }             else             {                 num1 = txt_Vlaue.Text;             }         }

        private void GetValue()         {             double numa = double.Parse(num1);             double numb = double.Parse(num2);             double result = 0;             switch (FuHao)             {                 case "+":                     result = numa + numb;                     break;                 case "-":                     result = numa - numb;                     break;                 case "*":                     result = numa * numb;                     break;                 default:                     result = numa / numb;                     break;             }             txt_Vlaue.Text = result.ToString();             FuHao = string.Empty;             oldValue = "0";             num1 = txt_Vlaue.Text;             num2 = string.Empty;         }

        private void button0_Click(object sender, EventArgs e)         {             InputName("0");         }

        private void button1_Click(object sender, EventArgs e)         {             InputName("1");         }

        private void button2_Click(object sender, EventArgs e)         {             InputName("2");         }

        private void button3_Click(object sender, EventArgs e)         {             InputName("3");         }

        private void button4_Click(object sender, EventArgs e)         {             InputName("4");         }

        private void button5_Click(object sender, EventArgs e)         {             InputName("5");         }

        private void button6_Click(object sender, EventArgs e)         {             InputName("6");         }

        private void button7_Click(object sender, EventArgs e)         {             InputName("7");         }

        private void button8_Click(object sender, EventArgs e)         {             InputName("8");         }

        private void button9_Click(object sender, EventArgs e)         {             InputName("9");         }

        private void buttonjia_Click(object sender, EventArgs e)         {             if (num2 != null && num2 != string.Empty)             {                 GetValue();             }             FuHao = "+";             oldValue = "0";         }

        private void buttonjian_Click(object sender, EventArgs e)         {             if (num2 != null && num2 != string.Empty)             {                 GetValue();             }             FuHao = "-";             oldValue = "0";         }

        private void buttoncheng_Click(object sender, EventArgs e)         {             if (num2 != null && num2 != string.Empty)             {                 GetValue();             }             FuHao = "*";             oldValue = "0";         }

        private void buttonchu_Click(object sender, EventArgs e)         {             if (num2 != null && num2 != string.Empty)             {                 GetValue();             }             FuHao = "/";             oldValue = "0";         }

        private void buttonqing_Click(object sender, EventArgs e)         {             txt_Vlaue.Text = "0";             FuHao = string.Empty;             oldValue = "0";         }

        private void buttondeng_Click(object sender, EventArgs e)         {             if (FuHao == string.Empty)             {                 txt_Vlaue.Text = num1.ToString();             }             else             {                 GetValue();             }         }     }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值