博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
计算器代码
阅读量:6241 次
发布时间:2019-06-22

本文共 4463 字,大约阅读时间需要 14 分钟。

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;        }

 

转载于:https://www.cnblogs.com/liujianshe1990-/p/5120365.html

你可能感兴趣的文章
8、Python —— 输入输出
查看>>
我的友情链接
查看>>
[转]Shell 统计PV, UV ,独立IP
查看>>
Flash网页甘特图控件
查看>>
yii2 csrf验证以及token管理
查看>>
一步一步理解Java企业级应用的可扩展性
查看>>
存储非结构化数据之利器-minio
查看>>
苹果个人开发者账号申请
查看>>
SSH双机互信及错误解决大全
查看>>
adb命令详解
查看>>
php网页如何运作
查看>>
学艺不精 - 记一次性能问题排查
查看>>
Provisioning Services 7.6 入门到精通系列之五:PVS控制台安装
查看>>
awk工具
查看>>
设计模式-代理模式(Proxy)
查看>>
Windows Sharepoint services 3.0部署体验
查看>>
[分享] Mac 键盘和Pc键盘对照表
查看>>
windows下批量杀死进程
查看>>
第七章:面向对象(三)
查看>>
android-ripple-background
查看>>