C# 按钮计算器的实现

namespace 计算器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        // bool iskey = false;
        // 定义一个字符接收 符号
        char type;
        double temp, num;
        // 1
        private void button9_Click(object sender, EventArgs e)
        {
            textBox1.Text += "1";
        }
        // 2
        private void button10_Click(object sender, EventArgs e)
        {
            textBox1.Text +="2";
        }
        //3
        private void button11_Click(object sender, EventArgs e)
        {
            textBox1.Text += "3";
        }
        //4
        private void button12_Click(object sender, EventArgs e)
        {
            textBox1.Text +="4";
        }
        //5
        private void button13_Click(object sender, EventArgs e)
        {
            textBox1.Text += "5";
        }
        //6
        private void button14_Click(object sender, EventArgs e)
        {
            textBox1.Text +="6";
        }
        //7
        private void button15_Click(object sender, EventArgs e)
        {
            textBox1.Text +="7";
        }
        //8
        private void button16_Click(object sender, EventArgs e)
        {
            textBox1.Text += "8";
        }
        //9
        private void button17_Click(object sender, EventArgs e)
        {
            textBox1.Text += "9";
        }
        //0
        private void button8_Click(object sender, EventArgs e)
        {
            textBox1.Text += "0";
        }
        //+
        private void button2_Click(object sender, EventArgs e)
        {
            num = double.Parse(textBox1.Text);
            type= '+';
            textBox1.Text = null;
                
        }
        //-
        private void button3_Click(object sender, EventArgs e)
        {
            num = double.Parse(textBox1.Text);
            type = '-';
            textBox1.Text = null;
        }
        //*
        private void button4_Click(object sender, EventArgs e)
        {
            num = double.Parse(textBox1.Text);
            type = '*';
            textBox1.Text = null;
        }
        // ÷
        private void button5_Click(object sender, EventArgs e)
        {
            num = double.Parse(textBox1.Text);
            type = '/';
            textBox1.Text = null;
        }
        // %
        private void button6_Click(object sender, EventArgs e)
        {
            num = double.Parse(textBox1.Text);
            type = '%';
            textBox1.Text = null;
        }
        // =
        private void button7_Click(object sender, EventArgs e)
        {       //.Parse()方法只能将 数字类型转换为.前面的类型  只能将数字
            temp = double.Parse(textBox1.Text);
            switch (type)
            {
                case '+':
                num += temp;
                break;
                case '-':
                num -= temp;
                break;
                case '*':
                num *= temp;
                break;
                case '/':
                num += temp;
                break;
                case '%':
                num %= temp;
                break;
            }
            textBox1.Text = num.ToString();
            type = ' ';
            num = 0;
            temp = 0;


        }
        // .
        private void button18_Click(object sender, EventArgs e)
        {
            textBox1.Text += ".";
        }
        // 清零 CE
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }

        // 删除 C
        private void button19_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length > 0)
            {
                
                textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
            }

        }
    }
}
  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值