winform 计算器

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //点击运算符为true 点完数字为false
        private string ysf;//记录上一次运算符
        private string bds;//记录已经算完的表达式
        private decimal sum;//记录上一步结果
        private bool isok = true;//用来判断是否刚刚点过运算符

        private void button1_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;//sender为事件主题   强制转换为button类型
            if (isok)
            {
                textBox2.Text = btn.Text;//添加新的btn.text的字符串
                isok = false;
            }
            else
            {
                textBox2.Text += btn.Text; //添加btn.text的字符串
            }
        }

        private void buttonC_Click(object sender, EventArgs e)//清除所有数据从新开始
        {
            textBox1.Text="";
            textBox2.Text = "0";
            isok = true;
            ysf = null;
            sum = 0;
          
        }

        private void button加_Click(object sender, EventArgs e) //运算符
        {
            Button btn = (Button)sender;   
            string yunsuanfu = btn.Text;  //定义一个string类型的yunsuanfu 来接收按的运算符



            if (ysf== null)  //if判断的是 是不是第一次点击运算符 
            {
                sum = decimal.Parse(textBox2.Text); //第一次点击运算符计算出的结果转换类型并显示在box2里面
                bds = textBox2.Text;  //记录表达式
            }
            else   //不是第一次点击运算符
            {
                if (!isok)
                {
                    if (ysf == "+")//判断点击的运算符
                    {
                        sum = sum + decimal.Parse(textBox2.Text);//第二次点击运算符计算出的结果转换类型并显示在box2里面
                    }
                    if (ysf == "-")
                    {
                        sum = sum - decimal.Parse(textBox2.Text);
                    }
                    if (ysf == "*")
                    {
                        sum = sum * decimal.Parse(textBox2.Text);
                    }
                    if (ysf == "/")
                    {
                        sum = sum / decimal.Parse(textBox2.Text);
                    }
                    if (ysf == "%")
                    {
                        sum = sum % decimal.Parse(textBox2.Text);
                    }
                    bds = bds + ysf + textBox2.Text;//现在的表达式=原来的表达式+第二次的运算符+刚点进去的数字
                }
            }
            textBox1.Text = bds + yunsuanfu;//上面的显示表达式+你刚点过的运算符
            ysf = yunsuanfu;//保存上一次点击的运算符并赋到ysf变量中
            isok = true;
            textBox2.Text = sum.ToString(); //显示结果
        }

        private void button等于_Click(object sender, EventArgs e)//等于
        {
           
                textBox1.Text = "";
                if (ysf == "+")
                {
                    sum = sum + decimal.Parse(textBox2.Text);
                }
                if (ysf == "-")
                {
                    sum = sum - decimal.Parse(textBox2.Text);
                }
                if (ysf == "*")
                {
                    sum = sum * decimal.Parse(textBox2.Text);
                }
                if (ysf == "/")
                {
                    //if (decimal.Parse(textBox2.Text) == 0)
                    //{
                    //    textBox2.Text = "除数不可为0";
                    //}
                    sum = sum / decimal.Parse(textBox2.Text);
                }
                if (ysf == "%")
                {
                    sum = sum % decimal.Parse(textBox2.Text);
                }
                textBox2.Text = sum.ToString();
                sum = 0;
                bds = "";
                ysf = null;
                isok = true;
           
        }

        private void button加减_Click(object sender, EventArgs e)//正负
        {
            if (!isok)
            {
                if (decimal.Parse(textBox2.Text) > 0)
                {
                    textBox2.Text = "-" + textBox2.Text;
                }
                else
                {
                    textBox2.Text = textBox2.Text.Substring(1);
                }
            }
        }

        private void button点_Click(object sender, EventArgs e)//小数点
        {
            if (textBox2.Text == "0")
            {
                textBox2.Text = "0.";
            }
            if (!isok && !textBox2.Text.Contains("."))
            {
                textBox2.Text += ".";
            }
            isok = false;
        }

        private void button箭头_Click(object sender, EventArgs e)//撤销
        {
            if (!isok)//禁止撤销得出的结果
            {
                if (textBox2.Text.Length == 1)
                {
                    textBox2.Text = "0";
                    isok = true;
                }
                else
                {
                    textBox2.Text = textBox2.Text.Substring(0, textBox2.Text.Length - 1);
                }
            }
        }
    }
}

  

转载于:https://www.cnblogs.com/Mr-xue/p/4542303.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值