• 计算器代码


    private string biaodashi="";//存放表达式
            private string preyunxuanfu="";//存放上一步的运算符
            private double sum = 0;
            private bool isok = false;//用于判断刚才点过的是数字还是运算符,初始化的时候是false
            private void btn1_Click(object sender, EventArgs e)
            {
                Button btn = (Button)sender;
                if (!isok||txtshuru.Text=="0")//刚刚初始化或者刚刚点过运算符
                { txtshuru.Text = btn.Text; }
                else//刚刚点过数字或者小数点
                {
                    txtshuru.Text += btn.Text;
                }
                isok = true;
            }
    
            private void btnjia_Click(object sender, EventArgs e)
            {
                Button btn = (Button)sender;
                if (isok)//刚刚初始化或者刚刚点过运算符
                {
                    if (biaodashi == "" || preyunxuanfu == "")//刚刚初始化
                    {
                        sum = double.Parse(txtshuru.Text);
                        biaodashi = txtshuru.Text;
                    }
                    else
                    {
                        if (preyunxuanfu == "+")
                        {
                            sum = sum + double.Parse(txtshuru.Text);
                            biaodashi = biaodashi + preyunxuanfu + txtshuru.Text;
                        }
                        if (preyunxuanfu == "-")
                        {
                            sum = sum - double.Parse(txtshuru.Text);
                            biaodashi = biaodashi + preyunxuanfu + txtshuru.Text;
                        }
                        if (preyunxuanfu == "*")
                        {
                            sum = sum * double.Parse(txtshuru.Text);
                            biaodashi = biaodashi + preyunxuanfu + txtshuru.Text;
                        }
                        if (preyunxuanfu == "/")
                        {
                            sum = sum / double.Parse(txtshuru.Text);
                            biaodashi = biaodashi + preyunxuanfu + txtshuru.Text;
                        }
                        if (preyunxuanfu == "%")
                        {
                            sum = sum % double.Parse(txtshuru.Text);
                            biaodashi = biaodashi + preyunxuanfu + txtshuru.Text;
                        }
                    }
                }
                preyunxuanfu = btn.Text;//将刚刚点过的运算符存到preyunsuanfu 里面
                txtbiaodashi.Text = biaodashi + btn.Text;
                txtshuru.Text = sum.ToString();
                isok = false;
            }
    
            private void button12_Click(object sender, EventArgs e)
            {
                if (preyunxuanfu == "+")
                {
                    sum = sum + double.Parse(txtshuru.Text);
                    biaodashi = biaodashi + preyunxuanfu + txtshuru.Text;
                }
                if (preyunxuanfu == "-")
                {
                    sum = sum - double.Parse(txtshuru.Text);
                    biaodashi = biaodashi + preyunxuanfu + txtshuru.Text;
                }
                if (preyunxuanfu == "*")
                {
                    sum = sum * double.Parse(txtshuru.Text);
                    biaodashi = biaodashi + preyunxuanfu + txtshuru.Text;
                }
                if (preyunxuanfu == "/")
                {
                    sum = sum / double.Parse(txtshuru.Text);
                    biaodashi = biaodashi + preyunxuanfu + txtshuru.Text;
                }
                if (preyunxuanfu == "%")
                {
                    sum = sum % double.Parse(txtshuru.Text);
                    biaodashi = biaodashi + preyunxuanfu + txtshuru.Text;
                }
                txtbiaodashi.Text = "";
                txtshuru.Text = sum.ToString();
                preyunxuanfu = "";
                isok = false;
                biaodashi = "";
            }
    
            private void button15_Click(object sender, EventArgs e)
            {
                txtbiaodashi.Text = "";
                txtshuru.Text = "0";
                preyunxuanfu = "";
                isok = false;
                biaodashi = "";
            }
    
            private void btnback_Click(object sender, EventArgs e)
            {
                if (txtshuru.Text.Length > 1)
                {
                    txtshuru.Text = txtshuru.Text.Substring(0, txtshuru.Text.Length - 1);
                }
                else
                {
                    txtshuru.Text = "0";
                }
            }
    
            private void btndian_Click(object sender, EventArgs e)
            {
                if (isok)//刚刚点过数字
                {
                    if (!txtshuru.Text.Contains("."))//判断输入放入数字中是否有小数点
                    {
                        txtshuru.Text = txtshuru.Text + ".";
                    }
                }
                else//刚刚初始化、点过运算符、归零或者等号
                {
                    txtshuru.Text = "0.";
                }
                isok = true;
            }

  • 相关阅读:
    Python学习笔记10:上下文协议
    Python学习笔记9:类
    使用率激增250%,这份报告再将 Serverless 推向幕前
    国内首篇云厂商 Serverless 论文入选全球顶会:突发流量下,如何加速容器启动?
    如何评估 Serverless 服务能力?这份报告给出了 40 条标准
    飞猪基于 Serverless 的云+端实践与思考
    Serverless:这真的是未来吗?(二)
    Serverless:这真的是未来吗?(一)
    被解救的代码
    云厂商下一块必争之地就是它了!
  • 原文地址:https://www.cnblogs.com/liujianshe1990-/p/5120365.html
Copyright © 2020-2023  润新知