码迷,mamicode.com
首页 > 其他好文 > 详细

计算器代码

时间:2016-01-11 11:43:05      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

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;
        }
技术分享

 

计算器代码

标签:

原文地址:http://www.cnblogs.com/liujianshe1990-/p/5120365.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!