winfrom实现简单计算器V2版本

前面,使用winform实现了个简单的计算器,今天,再来给大家看一个稍微复杂点的计算器,效果图如下:
winfrom实现简单计算器V2版本
winfrom实现简单计算器V2版本
包括归零,退格,加减乘除都已经实现,如果想要继续扩展的可以在稍微改改即可。
现在看看怎么实现的:
1.先从工具箱里面拉出来一个这样的窗体:
winfrom实现简单计算器V2版本
2.归零的方法:

private void qingchu_Click(object sender, EventArgs e)
        {
            this.lbljieguo.Text = "0";
            sb = new StringBuilder("");
        }

3.退格的方法:

 //退格的按钮
        private void tuige_Click(object sender, EventArgs e)
        {
            //判断当前能不能点击退格

            //判断显示框是否有值,有值的话可以删除,没有值的话提示不能删除
            if (this.lbljieguo.Text.Length > 0)
            {
                char ch = 'a';      //保存符号

                //遍历字符串
                foreach (char c in sb.ToString())
                {
                    ch = c;     //最后一个值放在ch里面
                }
                //判断最后一个是否是+-*/%
                if (ch != '+')
                {
                    //如果是,不能动num1和num2,否则可以动

                    if (flag == false)
                    {
                        num1 = new StringBuilder(num1.ToString().Substring(0, num1.Length - 1));

                    }
                    else
                    {
                        num2 = new StringBuilder(num2.ToString().Substring(0, num2.Length - 1));
                    }

                }
                ji = false;
                this.lbljieguo.Text = sb.ToString().Substring(0, this.lbljieguo.Text.Length - 1);
                sb = new StringBuilder(this.lbljieguo.Text);
            }
            else
            {
                MessageBox.Show("显示框都没有值,你删啥?", "提示信息");
            }


        }

4.(1 2 3 4 5 6 7 8 9 0)的点击事件:

 //第一个数
        StringBuilder num1 = new StringBuilder();
        //第二个数
        StringBuilder num2 = new StringBuilder();
        //判断是否是第一次输入
        bool flag = false;
        //运算符
        string fu = "";
        //当计算完结果时,不能退格
        bool ji = false;


        StringBuilder sb = new StringBuilder();
        //7
        private void btn7_Click(object sender, EventArgs e)
        {
            if (flag == false)
            {

                num1.Append(7);
            }
            else
            {

                num2.Append(7);
            }
            sb.Append(7);
            this.lbljieguo.Text = sb.ToString();
        }

        //8
        private void btn8_Click(object sender, EventArgs e)
        {
            if (flag == false)
            {

                num1.Append(8);
            }
            else
            {

                num2.Append(8);
            }
            sb.Append(8);
            this.lbljieguo.Text = sb.ToString();
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            if (flag == false)
            {

                num1.Append(9);
            }
            else
            {
                num2.Append(9);
            }
            sb.Append(9);
            this.lbljieguo.Text = sb.ToString();
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            if (flag == false)
            {

                num1.Append(4);
            }
            else
            {

                num2.Append(4);
            }
            sb.Append(4);
            this.lbljieguo.Text = sb.ToString();
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            if (flag == false)
            {

                num1.Append(5);
            }
            else
            {

                num2.Append(5);
            }
            sb.Append(5);
            this.lbljieguo.Text = sb.ToString();
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            if (flag == false)
            {

                num1.Append(6);
            }
            else
            {

                num2.Append(6);
            }
            sb.Append(6);
            this.lbljieguo.Text = sb.ToString();
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            if (flag == false)
            {

                num1.Append(1);
            }
            else
            {

                num2.Append(1);
            }
            sb.Append(1);
            this.lbljieguo.Text = sb.ToString();
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            if (flag == false)
            {

                num1.Append(2);
            }
            else
            {

                num2.Append(2);
            }
            sb.Append(2);
            this.lbljieguo.Text = sb.ToString();
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            if (flag == false)
            {

                num1.Append(3);
            }
            else
            {

                num2.Append(3);
            }
            sb.Append(3);
            this.lbljieguo.Text = sb.ToString();
        }
         private void btn0_Click(object sender, EventArgs e)
        {
            if (flag == false)
            {

                num1.Append(0);
            }
            else
            {

                num2.Append(0);
            }
            sb.Append(0);
            this.lbljieguo.Text = sb.ToString();
        }

5.加减乘除的点击事件:

private void btnchu_Click(object sender, EventArgs e)
        {
            flag = true;
            sb.Append("/");
            this.lbljieguo.Text = sb.ToString();
            fu = "/";
        }
         private void btnyu_Click(object sender, EventArgs e)
        {
            flag = true;
            sb.Append("%");
            this.lbljieguo.Text = sb.ToString();
            fu = "%";
        }
        private void btnjia_Click(object sender, EventArgs e)
        {
            flag = true;
            sb.Append("+");
            this.lbljieguo.Text = sb.ToString();
            fu = "+";

        }
          private void btnjian_Click(object sender, EventArgs e)
        {
            flag = true;
            sb.Append("-");
            this.lbljieguo.Text = sb.ToString();
            fu = "-";
        }
        private void btncheng_Click(object sender, EventArgs e)
        {
            flag = true;
            sb.Append("*");
            this.lbljieguo.Text = sb.ToString();
            fu = "*";
        }

6.等于的方法:

 //等于的按钮
        private void btndeng_Click(object sender, EventArgs e)
        {
            //string xianshi = sb.ToString();
            switch (fu)
            {
                case "+":
                    this.lbljieguo.Text = (int.Parse(num1.ToString()) + int.Parse(num2.ToString())).ToString();
                    break;
                case "-":
                    this.lbljieguo.Text = (int.Parse(num1.ToString()) - int.Parse(num2.ToString())).ToString();

                    break;
                case "*":
                    this.lbljieguo.Text = (int.Parse(num1.ToString()) * int.Parse(num2.ToString())).ToString();

                    break;
                case "/":
                    this.lbljieguo.Text = (int.Parse(num1.ToString()) / int.Parse(num2.ToString())).ToString();

                    break;
                case "%":
                    this.lbljieguo.Text = (int.Parse(num1.ToString()) % int.Parse(num2.ToString())).ToString();
                    break;
            }

            num1 = new StringBuilder("");
            num2 = new StringBuilder("");
            sb = new StringBuilder("");
            flag = false;
            ji = true;     //显示的是结果

        }

整个的实现方法以及代码都已经给出,不做伸手党,自己动脑思考思考,加油啊,在编码路上的你们~~

上一篇:UVA278 Chess


下一篇:快速上手 Linkerd v2 Service Mesh(服务网格)