C#:Windows窗体应用程序 计算器(二)

C#:Windows窗体应用程序 计算器(二)_第1张图片

初代计算器:C#:Windows窗体应用程序计算器(一)

看了【C#】计算器这篇文章,结合初代计算器进行升级,二代计算器丑陋依旧,用数组装数字和运算符,实现了多数相加减乘除余

C#:Windows窗体应用程序 计算器(二)_第2张图片

calculator.cs代码如下:

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;
using System.Text.RegularExpressions;

namespace Complex
{
     
    public partial class Calculator : Form
    {
     
        private List<double> leftNumber = new List<double>(); //存用户输入的数字
        private List<int> operatorStr = new List<int>(); //存用户输入的运算符,定义+为0,-为1,×为2,÷为3,%为4

        private Boolean isDot; //判断小数点是否已输入过
        private Boolean result_flag = false; //判断是否已按过=
        private Boolean add_flag = false; //按下+
        private Boolean minus_flag = false; //按下-
        private Boolean multi_flag = false; //按下×
        private Boolean div_flag = false; //按下÷
        private Boolean mod_flag = false; //按下%
        private Boolean operate_flag = false;//按下=确定是否响应

        public Calculator()
        {
     
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        //按下+、-、*、/、%按钮
        private void bntOperation_Click(object sender, EventArgs e)
        {
     
            isDot = false;
            Button btn = (Button)sender;
            if (btn.Equals(btnPlus))
            {
     
                if (!add_flag) //防止用户多次输入一个符号键,符号键只允许输入一次
                {
     
                    result_flag = false;
                    leftNumber.Add(double.Parse(txtBResult.Text));//将当前已输入的数字放入leftNumber
                    operatorStr.Add(0);
                    label.Text += "+";
                    add_flag = true;
                    operate_flag = false; //刚输入完符号,不能构成一条正常的表达式,如11+,设置为不可运行状态
                }
            }
            else if (btn.Equals(btnMinus))
            {
     
                if (!minus_flag)
                {
     
                    result_flag = false;
                    leftNumber.Add(double.Parse(txtBResult.Text));
                    operatorStr.Add(1);
                    label.Text += "-";
                    minus_flag = true;
                    operate_flag = false;
                }
            }
            else if (btn.Equals(btnMultioly))
            {
     
                if (!multi_flag)
                {
     
                    result_flag = false;
                    leftNumber.Add(double.Parse(txtBResult.Text));
                    operatorStr.Add(2);
                    label.Text = "(" + label.Text + ")" + "×";//给前面的已经输入的东西加个括号。
                    multi_flag = true;
                    operate_flag = false;
                }
            }
            else if (btn.Equals(btndivision))
            {
     
                if (!div_flag)
                {
     
                    result_flag = false;
                    leftNumber.Add(double.Parse(txtBResult.Text));
                    operatorStr.Add(3);
                    label.Text = "(" + label.Text + ")" + "÷";
                    div_flag = true;
                    operate_flag = false;
                }
            }
            else if (!mod_flag)
            {
     
                result_flag = false;
                leftNumber.Add(double.Parse(txtBResult.Text));
                operatorStr.Add(4);
                label.Text = "(" + label.Text + ")" + "%";
                mod_flag = true;
                operate_flag = false;
            }
        }

        //按下=按钮
        private void bntEqual_Click(object sender, EventArgs e)
        {
     
            isDot = false;
            if (leftNumber.Count > 0 && operatorStr.Count > 0 && operate_flag)
            {
      //需要防止用户没输入数字,或者只输入了一个数,就按=
                leftNumber.Add(double.Parse(txtBResult.Text));
                double total = leftNumber[0];
                for (int i = 0; i < operatorStr.Count; i++)
                {
     
                    int Operator = operatorStr[i];
                    switch (Operator)
                    {
     
                        case 0:
                            total += leftNumber[i + 1];
                            break;
                        case 1:
                            total -= leftNumber[i + 1];
                            break;
                        case 2:
                            total *= leftNumber[i + 1];
                            break;
                        case 3:
                            total /= leftNumber[i + 1];
                            break;
                        case 4:
                            total %= leftNumber[i + 1];
                            break;
                    }
                }
                txtBResult.Text = total.ToString();
                label.Text = total.ToString();
                operatorStr.Clear(); //清空数字和运算符数组
                leftNumber.Clear();
                result_flag = true; //表示按下=
            }
        }

        //按下数字键
        private void btn_Click(object sender, EventArgs e)
        {
     
            Button btn = (Button)sender;
            if (btn.Equals(btn0))
                       num("0");
            else if (btn.Equals(btn1))
                       num("1");
            else if(btn.Equals(btn2))
                       num("2");
            else if(btn.Equals(btn3))
                       num("3");
            else if (btn.Equals(btn4))
                       num("4");
            else if (btn.Equals(btn5))
                       num("5");
            else if (btn.Equals(btn6))
                       num("6");
            else if (btn.Equals(btn7))
                       num("7");
            else if (btn.Equals(btn8))
                       num("8");
            else if (btn.Equals(btn9))
                       num("9");
        }

        private void num(string num)
        {
     
            if (add_flag || minus_flag || multi_flag || div_flag || mod_flag || result_flag)
            {
     
                if (result_flag) //已按下=,刚算完一个运算的状态
                {
     
                    label.Text = "";
                }
                txtBResult.Clear(); //刚输入一个运算符,要将txtBResult清空
                add_flag = false;
                minus_flag = false;
                multi_flag = false;
                div_flag = false;
                mod_flag = false;
                result_flag = false;
            }
            txtBResult.Text += num;
            label.Text += num;
            operate_flag = true;
        }

        //按下BackSpace按钮
        private void bntBackSpace_Click(object sender, EventArgs e)
        {
     
            if (txtBResult.Text.Substring(0, txtBResult.TextLength - 1) == "")
                txtBResult.Text = "0";
            else
            {
     
                if (txtBResult.Text[txtBResult.TextLength - 1] == '.')
                    isDot = false;
                txtBResult.Text = txtBResult.Text.Substring(0, txtBResult.TextLength - 1);
                label.Text = label.Text.Substring(0, label.Text.Length - 1);
            }
        }

        //按下Clear按钮
        private void bntClear_Click(object sender, EventArgs e)
        {
     
            operatorStr.Clear();
            leftNumber.Clear();
            add_flag = false;
            minus_flag = false;
            multi_flag = false;
            div_flag = false;
            result_flag = false;
            operate_flag = false;
            txtBResult.Clear();
            label.Text = "";
        }

        //按下小数点.按钮
        private void bntDot_Click(object sender, EventArgs e)
        {
     
            if (isDot == false)
            {
     
                isDot = true;
                txtBResult.Text += ".";
                label.Text += ".";
            }
        }

    }
}

项目下载链接:https://download.csdn.net/download/Cristiano272/12329735

另一个C# Winform程序
C#:Winform 打字测速程序 Typer:https://blog.csdn.net/Cristiano272/article/details/105598495

你可能感兴趣的:(C#,c#,winform)