winform 计算器 两步走

namespace WindowsFormsApplication4
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        //记录是否刚点过运算符
        private bool isok = true;
        //记录结果
        private decimal Sum;
        //记录上一次的运算符
        private string PreYunsuanfu;
        //记录表达式
        private string Biaodashi;

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void btnling_Click(object sender, EventArgs e)
        {
            if (isok)
            {
                txtshuru.Text = "0";
               // isok = false;
            }
            else
            {
                txtshuru.Text += "0";
            }
        }

        private void btnyi_Click(object sender, EventArgs e)
        {
            if (isok)
            {
                txtshuru.Text = "1";
                isok = false;
            }
            else
            {
                txtshuru.Text += "1";
            }
        }

        private void btner_Click(object sender, EventArgs e)
        {
            if (isok)
            {
                txtshuru.Text = "2";
                isok = false;
            }
            else
            {
                txtshuru.Text += "2";
            }
        }

        private void btnsan_Click(object sender, EventArgs e)
        {
             if (isok)
            {
                txtshuru.Text = "3";
                isok = false;
            }
            else
            {
                txtshuru.Text += "3";
            }
        }

        private void btnsi_Click(object sender, EventArgs e)
        {
            if (isok)
            {
                txtshuru.Text = "4";
                isok = false;
            }
            else
            {
                txtshuru.Text += "4";
            }
        }

        private void btnwu_Click(object sender, EventArgs e)
        {
            if (isok)
            {
                txtshuru.Text = "5";
                isok = false;
            }
            else
            {
                txtshuru.Text += "5";
            }
        }

        private void btnliu_Click(object sender, EventArgs e)
        {
            if (isok)
            {
                txtshuru.Text = "6";
                isok = false;
            }
            else
            {
                txtshuru.Text += "6";
            }
        }

        private void btnqi_Click(object sender, EventArgs e)
        {
            if (isok)
            {
                txtshuru.Text = "7";
                isok = false;
            }
            else
            {
                txtshuru.Text += "7";
            }
        }

        private void btnba_Click(object sender, EventArgs e)
        {
            if (isok)
            {
                txtshuru.Text = "8";
                isok = false;
            }
            else
            {
                txtshuru.Text += "8";
            }
        }

        private void btnjiu_Click(object sender, EventArgs e)
        {
            if (isok)
            {
                txtshuru.Text = "9";
                isok = false;
            }
            else
            {
                txtshuru.Text += "9";
            }
        }

        private void btnjia_Click(object sender, EventArgs e)
        {
            if (PreYunsuanfu == null)
            {
                Sum = decimal.Parse(txtshuru.Text);
                Biaodashi = txtshuru.Text;
            }
            else
            {
                if (!isok)
                {
                    if (PreYunsuanfu == "+")
                    {
                        Sum = Sum + decimal.Parse(txtshuru.Text);
                    }
                    if (PreYunsuanfu == "-")
                    {
                        Sum = Sum - decimal.Parse(txtshuru.Text);
                    }
                    if (PreYunsuanfu == "*")
                    {
                        Sum = Sum * decimal.Parse(txtshuru.Text);
                    }
                    if (PreYunsuanfu == "/")
                    {
                        Sum = Sum / decimal.Parse(txtshuru.Text);
                    }
                    if (PreYunsuanfu == "%")
                    {
                        Sum = Sum % decimal.Parse(txtshuru.Text);
                    }

                    Biaodashi = Biaodashi + PreYunsuanfu + txtshuru.Text;
                }
            }

            txtjisuanshi.Text = Biaodashi + "+";
            PreYunsuanfu = "+";
            isok = true;
            txtshuru.Text = Sum.ToString();
        }

        private void btnjian_Click(object sender, EventArgs e)
        {
            if (PreYunsuanfu == null)
            {
                Sum = decimal.Parse(txtshuru.Text);
                Biaodashi = txtshuru.Text;
            }
            else
            {
                if (!isok)
                {
                    if (PreYunsuanfu == "+")
                    {
                        Sum = Sum + decimal.Parse(txtshuru.Text);
                    }
                    if (PreYunsuanfu == "-")
                    {
                        Sum = Sum - decimal.Parse(txtshuru.Text);
                    }
                    if (PreYunsuanfu == "*")
                    {
                        Sum = Sum * decimal.Parse(txtshuru.Text);
                    }
                    if (PreYunsuanfu == "/")
                    {
                        Sum = Sum / decimal.Parse(txtshuru.Text);
                    }
                    if (PreYunsuanfu == "%")
                    {
                        Sum = Sum % decimal.Parse(txtshuru.Text);
                    }

                    Biaodashi = Biaodashi + PreYunsuanfu + txtshuru.Text;
                }
            }

            txtjisuanshi.Text = Biaodashi + "-";
            PreYunsuanfu = "-";
            isok = true;
            txtshuru.Text = Sum.ToString();
        }

        private void btncheng_Click(object sender, EventArgs e)
        {
            if (PreYunsuanfu == null)
            {
                Sum = decimal.Parse(txtshuru.Text);
                Biaodashi = txtshuru.Text;
            }
            else
            {
                if (!isok)
                {
                    if (PreYunsuanfu == "+")
                    {
                        Sum = Sum + decimal.Parse(txtshuru.Text);
                    }
                    if (PreYunsuanfu == "-")
                    {
                        Sum = Sum - decimal.Parse(txtshuru.Text);
                    }
                    if (PreYunsuanfu == "*")
                    {
                        Sum = Sum * decimal.Parse(txtshuru.Text);
                    }
                    if (PreYunsuanfu == "/")
                    {
                        Sum = Sum / decimal.Parse(txtshuru.Text);
                    }
                    if (PreYunsuanfu == "%")
                    {
                        Sum = Sum % decimal.Parse(txtshuru.Text);
                    }

                    Biaodashi = Biaodashi + PreYunsuanfu + txtshuru.Text;
                }
            }

            txtjisuanshi.Text = Biaodashi + "*";
            PreYunsuanfu = "*";
            isok = true;
            txtshuru.Text = Sum.ToString();
        }

        private void btnchu_Click(object sender, EventArgs e)
        {
            if (PreYunsuanfu == null)
            {
                Sum = decimal.Parse(txtshuru.Text);
                Biaodashi = txtshuru.Text;
            }
            else
            {
                if (!isok)
                {
                    if (PreYunsuanfu == "+")
                    {
                        Sum = Sum + decimal.Parse(txtshuru.Text);
                    }
                    if (PreYunsuanfu == "-")
                    {
                        Sum = Sum - decimal.Parse(txtshuru.Text);
                    }
                    if (PreYunsuanfu == "*")
                    {
                        Sum = Sum * decimal.Parse(txtshuru.Text);
                    }
                    if (PreYunsuanfu == "/")
                    {
                        Sum = Sum / decimal.Parse(txtshuru.Text);
                    }
                    if (PreYunsuanfu == "%")
                    {
                        Sum = Sum % decimal.Parse(txtshuru.Text);
                    }

                    Biaodashi = Biaodashi + PreYunsuanfu + txtshuru.Text;
                }
            }

            txtjisuanshi.Text = Biaodashi + "/";
            PreYunsuanfu = "/";
            isok = true;
            txtshuru.Text = Sum.ToString();
        }

        private void btnmo_Click(object sender, EventArgs e)
        {
            if (PreYunsuanfu == null)
            {
                Sum = decimal.Parse(txtshuru.Text);
                Biaodashi = txtshuru.Text;
            }
            else
            {
                if (!isok)
                {
                    if (PreYunsuanfu == "+")
                    {
                        Sum = Sum + decimal.Parse(txtshuru.Text);
                    }
                    if (PreYunsuanfu == "-")
                    {
                        Sum = Sum - decimal.Parse(txtshuru.Text);
                    }
                    if (PreYunsuanfu == "*")
                    {
                        Sum = Sum * decimal.Parse(txtshuru.Text);
                    }
                    if (PreYunsuanfu == "/")
                    {
                        Sum = Sum / decimal.Parse(txtshuru.Text);
                    }
                    if (PreYunsuanfu == "%")
                    {
                        Sum = Sum % decimal.Parse(txtshuru.Text);
                    }

                    Biaodashi = Biaodashi + PreYunsuanfu + txtshuru.Text;
                }
            }

            txtjisuanshi.Text = Biaodashi + "%";
            PreYunsuanfu = "%";
            isok = true;
            txtshuru.Text = Sum.ToString();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtshuru.Text = "0";
            txtjisuanshi.Text = "";
            isok = true;
            PreYunsuanfu = null;
            Sum = 0;
            Biaodashi = "";
        }

        private void btnchexiao_Click(object sender, EventArgs e)
        {
            if (!isok)
            {
                if (txtshuru.Text.Length == 1)
                {
                    txtshuru.Text = "0";
                    isok = true;
                }
                else
                {
                    txtshuru.Text = txtshuru.Text.Substring(0, txtshuru.Text.Length - 1);
                }
            }
        }

        private void btndian_Click(object sender, EventArgs e)
        {
            if (txtshuru.Text == "0")
            {
                txtshuru.Text = "0.";
            }
            if (!isok  && !txtshuru.Text.Contains("."))
            {
                txtshuru.Text += ".";
            }
            isok = false;
        }

        private void btndeng_Click(object sender, EventArgs e)
        {
            txtjisuanshi.Text = "";
            if (PreYunsuanfu == "+")
            {
                Sum = Sum + decimal.Parse(txtshuru.Text);
            }
            if (PreYunsuanfu == "-")
            {
                Sum = Sum - decimal.Parse(txtshuru.Text);
            }
            if (PreYunsuanfu == "*")
            {
                Sum = Sum * decimal.Parse(txtshuru.Text);
            }
            if (PreYunsuanfu == "/")
            {
                Sum = Sum / decimal.Parse(txtshuru.Text);
            }
            if (PreYunsuanfu == "%")
            {
                Sum = Sum % decimal.Parse(txtshuru.Text);
            }
            txtshuru.Text = Sum.ToString();
            isok = true;
            Biaodashi = "";
            Sum = 0;
            PreYunsuanfu = null;
        }

        private void btnzhengfu_Click(object sender, EventArgs e)
        {
            if (!isok)
            {
                if (decimal.Parse(txtshuru.Text) > 0)
                {
                    txtshuru.Text = "-" + txtshuru.Text;
                }
                else
                {
                    txtshuru.Text = txtshuru.Text.Substring(1);
                }
            }
        }
    }
}

  

TWO  button事件

namespace FirstForm
{
    public partial class JiSuanQi : Form
    {
        public JiSuanQi()
        {
            InitializeComponent();
        }

        private double sum=0;//记录上一步的运算结果
        private string biaodashi;//点击运算符之前的算式
        private string Preyunsuanfu;//存放之前的运算符
        private bool isok = true;//用来判断是否刚刚点过运算符
        private void btnling_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            if (isok)
            {
                txtshuru.Text = btn.Text;
                isok = false;
            }
            else
            {
                txtshuru.Text += btn.Text;
            }

        }
        private void btnjia_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            string yunsuanfu = btn.Text;
            if (Preyunsuanfu == null)
            {
                biaodashi += txtshuru.Text;
                sum = double.Parse(txtshuru.Text);
            }
            else
            {
                if (!isok)
                {
                    if (Preyunsuanfu == "+")
                    {
                        sum += double.Parse(txtshuru.Text);
                    }
                    if (Preyunsuanfu == "-")
                    {
                        sum -= double.Parse(txtshuru.Text);
                    }
                    if (Preyunsuanfu == "*")
                    {
                        sum *= double.Parse(txtshuru.Text);
                    }
                    if (Preyunsuanfu == "/")
                    {
                        sum /= double.Parse(txtshuru.Text);
                    }
                    if (Preyunsuanfu == "%")
                    {
                        sum %= double.Parse(txtshuru.Text);
                    }

                    biaodashi = biaodashi+ Preyunsuanfu + txtshuru.Text;
                    txtshuru.Text = sum.ToString();
                }
            }
            txtjisuanshi.Text = biaodashi + yunsuanfu;
            Preyunsuanfu = yunsuanfu;
            isok = true;

        }

    }
}

  

时间: 2024-10-12 14:46:44

winform 计算器 两步走的相关文章

Winform——计算器局部功能(+-*/%)

namespace 计算器2._0 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private bool Isok = true;//记录刚刚是否点过运算符,或是否第一次进入界面 private string Biaodashi; //记录运算符之前的表达式(在第一栏) private decimal Sum;//记录之前计算的结果(在第二栏) private string Pre

Winform——计算器(大部分功能)

namespace 计算器2._0 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private bool Isok = true;//记录刚刚是否点过运算符,或是否第一次进入界面 private string Biaodashi; //记录运算符之前的表达式(在第一栏) private decimal Sum;//记录之前计算的结果(在第二栏) private string Pre

Winform——计算器

namespace 计算器2._0 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private bool Isok = true;//记录刚刚是否点过运算符,或是否第一次进入界面(用来存储判断是否刚刚点过运算符) private string Biaodashi; //记录运算符之前的表达式(在第一栏) private decimal Sum;//记录之前计算的结果(在第二栏) p

winform计算器练习

临近年关,今日在学习的过程中感觉甚是无聊,便想用C#来开发一个简易的计算器程序,这里记录下今日下午的实现过程,同时也记录下自己的第一遍博客. 一,需求 首先我们先来决定我们的计算器要实现什么功能 功能需求:1.能够实现加.减.乘.除.求余等两个操作数的运算,以及开方.平方单个操作数的运算 2.能够清除错误的输入,能够实现清零操作 显示需求:能够显示操作数与运算内容,显示结果 二.设计界面 1.在明白我们的功能需求后,我们来设计界面,界面主要包括三个部分,用于显示的两个textBox,以及数字键B

winform 计算器

1 bool a; //判断上一次按键是加减乘除还是数字 2 string c = ""; //判断加减乘除操作 3 decimal f;//记录第一个值以及接收运算结果 4 /// <summary> 5 /// 数字按键 6 /// </summary> 7 /// <param name="sender"></param> 8 /// <param name="e"></pa

winform计算器

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace FirstForm { public partial class JiSuanQi : Form { publi

Winform——计算器进制转换

namespace 进制转换2._0 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public string Shizhuaner(int a, int jinzhi) //十转二 { string jieguo = ""; while (true) { string s = (a % jinzhi).ToString(); //s的值为a%2后的数,因为进制是2. ji

1.C#WinForm基础制作简单计算器

利用c#语言编写简单计算器: 源码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace 简单计算器 { public partial class Form1 : Form { public For

[原][C#][winForm]分级基金折溢价WinForm网络计算器

分级基金折溢价WinForm网络计算器 通过子/母基金代码,从 [ 东方财富网,天天基金网,新浪 ] 抓取分级基金的子母基金数据(代码,名称,净值,价格), 并计算出子基金(A基金,B基金)以及母基金的溢价率与折价率, 便于投资/投机客从中套利. 数据通过网站获取,使用基金净值,非估值,一般晚间网站会更新当天基金净值,不可用于实时计算. ------ 效果图: ---- 代码: 1.窗体如效果图 2.常用变量与数据抓取代码: 1 namespace fundGetAndCal 2 { 3 pub