C#实现简易版的计算器数

界面设计:textbook 和18个button按钮
在这里插入图片描述代码部分:

 private void Form1_Load(object sender, EventArgs e)
        {
            //计算计算器窗口居中
            int left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2;
            int top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2;
            this.Location = new Point(left, top);
            
        }
        double num1 = 0;
        double num2 = 0;
        bool iskey = false;
        
        //1
        private void Button7_Click(object sender, EventArgs e)
        {          
            if (iskey)
            {
                textBox1.Text = "";
                iskey = false;
            }
            textBox1.Text += "1";

        }
        //2
        private void Button6_Click(object sender, EventArgs e)
        {
            if (iskey)
            {
                textBox1.Text = "";
                iskey = false;
            }
            textBox1.Text += "2";
        }
        //3
        private void Button5_Click(object sender, EventArgs e)
        {
            if (iskey)
            {
                textBox1.Text = "";
                iskey = false;
            }
            textBox1.Text += "3";
        }
        //4
        private void Button11_Click(object sender, EventArgs e)
        {
            if (iskey)
            {
                textBox1.Text = "";
                iskey = false;
            }
            textBox1.Text += "4";
        }
        //5
        private void Button10_Click(object sender, EventArgs e)
        {
            if (iskey)
            {
                textBox1.Text = "";
                iskey = false;
            }
            textBox1.Text += "5";
        }
        //6
        private void Button9_Click(object sender, EventArgs e)
        {
            if (iskey)
            {
                textBox1.Text = "";
                iskey = false;
            }
            textBox1.Text += "6";
        }
        //7
        private void Button15_Click(object sender, EventArgs e)
        {
            if (iskey)
            {
                textBox1.Text = "";
                iskey = false;
            }
            textBox1.Text += "7";
        }
        //8
        private void Button14_Click(object sender, EventArgs e)
        {
            if (iskey)
            {
                textBox1.Text = "";
                iskey = false;
            }
            textBox1.Text += "8";
        }
        //9
        private void Button12_Click(object sender, EventArgs e)
        {
            if (iskey)
            {
                textBox1.Text = "";
                iskey = false;
            }
            textBox1.Text += "9";
        }
        //0
        private void Button18_Click(object sender, EventArgs e)
        {
            if (iskey)
            {
                textBox1.Text = "";
                iskey = false;
            }
            textBox1.Text += "0";
        }
        //小数点.
        private void Button17_Click(object sender, EventArgs e)
        {
            if (iskey)
            {
                textBox1.Text = "";
                iskey = false;
            }
            textBox1.Text += ".";
        }
        //CE
        private void Button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }
        string type = "";
        //=
        private void Button2_Click(object sender, EventArgs e)
        {
            if (iskey)
            {
                return;
            }
            iskey = true;
             if (textBox1.Text != "")
             {
                 num2 = Convert.ToDouble(textBox1.Text);
             }          
            switch (type)
            {
                case "+":
                    textBox1.Text = (num1 + num2).ToString();
                    break;
                case "-":
                    textBox1.Text = (num1 - num2).ToString();
                    break;
                case "×":
                    textBox1.Text = (num1 * num2).ToString();
                    break;
                case "/":
                    textBox1.Text = (num1 / num2).ToString();
                    break;
                case "%":
                    textBox1.Text = (num1 % num2).ToString();
                    break;
                default:
                    break;
            }
        }

        //-
        private void Button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text!="")
            {
                num1 = Convert.ToDouble(textBox1.Text);
            }
            type = "-";
            iskey = true;
        }
        //×
        private void Button4_Click(object sender, EventArgs e)
        {
            type = "×";
            if (textBox1.Text!="")
            {
                num1 = Convert.ToDouble(textBox1.Text);
            }
            iskey = true;
        }
        //除
        private void Button8_Click(object sender, EventArgs e)
        {
            type = "/";
            if (textBox1.Text!="")
            {
                num1 = Convert.ToDouble(textBox1.Text);
            }
            iskey = true;          
        }
        //+
        private void Button13_Click(object sender, EventArgs e)
        {
            type = "+";
            if (textBox1.Text!="")
            {
                num1 = Convert.ToDouble(textBox1.Text);
            }
            iskey = true;


        }

        //取余%
        private void Button16_Click(object sender, EventArgs e)
        {
            type = "%";
            if (textBox1.Text!="")
            {
                num1 = Convert.ToDouble(textBox1.Text);

            }
            iskey = true;


        }

    }
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值