C#计算器

8 篇文章 0 订阅
4 篇文章 0 订阅

效果图如下
在这里插入图片描述
代码如下

        double num1, num2;
        //定义第一次输入的数字与第二次输入的数字
        string ysf;
        //定义运算符
        //第一次输入的数字都显示在文本框中
        private void button7_Click(object sender, EventArgs e)
        {
            textBox1.Text += button7.Text;
            textBox2.Text += button7.Text;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            textBox1.Text += button8.Text;
            textBox2.Text += button8.Text;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            textBox1.Text += button9.Text;
            textBox2.Text += button9.Text;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text += button4.Text;
            textBox2.Text += button4.Text;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox1.Text += button5.Text;
            textBox2.Text += button5.Text;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            textBox1.Text += button6.Text;
            textBox2.Text += button6.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text += button1.Text;
            textBox2.Text += button1.Text;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text += button2.Text;
            textBox2.Text += button2.Text;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text += button3.Text;
            textBox2.Text += button3.Text;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            textBox1.Text += button11.Text;
            textBox2.Text += button11.Text;
        }
        //+    判断运算符为+
        private void button17_Click(object sender, EventArgs e)
        {
            textBox2.Text += button17.Text;
            num1 = double.Parse(textBox1.Text);
            textBox1.Text = "";
            ysf = "+";
        }
        //-    判断运算符为-
        private void button15_Click(object sender, EventArgs e)
        {
            textBox2.Text += button15.Text;
            num1 = double.Parse(textBox1.Text);
            textBox1.Text = "";
            ysf = "-";
        }
        //X    判断运算符为X
        private void button14_Click(object sender, EventArgs e)
        {
            textBox2.Text += button14.Text;
            num1 = double.Parse(textBox1.Text);
            textBox1.Text = "";
            ysf = "X";
        }
        //    判断运算符为/
        private void button16_Click(object sender, EventArgs e)
        {
            textBox2.Text += button16.Text;
            num1 = double.Parse(textBox1.Text);
            textBox1.Text = "";
            ysf = "/";
        }
        //=
        private void button13_Click(object sender, EventArgs e)
        {
            num2 = double.Parse(textBox1.Text);
            //判断运算符为+-X/时的算法
            switch (ysf)
            {
                case ("+"):
                    textBox1.Text = (num1 + num2).ToString();
                    textBox2.Text += "=" + textBox1.Text;
                    break;
                case ("-"):
                    textBox1.Text = (num1 - num2).ToString();
                    textBox2.Text += "=" + textBox1.Text;
                    break;
                case ("X"):
                    textBox1.Text = (num1 * num2).ToString();
                    textBox2.Text += "=" + textBox1.Text;
                    break;
                case ("/"):
                    textBox1.Text = (num1 / num2).ToString();
                    textBox2.Text += "=" + textBox1.Text;
                    break;

            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
        }

        private void button10_Click(object sender, EventArgs e)
        {
            textBox1.Text += button10.Text;
            textBox2.Text += button10.Text;
        }
    }
}

  • 12
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值