C# C语言、计算器

C# C语言、计算器,c#,calculator,C#,Calculator,大家好,谢谢你们帮助我 我用C#做了这个计算器,我有一个问题。 当我加上像5+5+5这样的东西时,它给了我正确的结果,但当我想减去两个以上的数字,同时也要除以或乘以两个以上的数字时,我得不到正确的结果 你知道我做错了什么吗 多谢各位 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Li

大家好,谢谢你们帮助我

我用C#做了这个计算器,我有一个问题。 当我加上像5+5+5这样的东西时,它给了我正确的结果,但当我想减去两个以上的数字,同时也要除以或乘以两个以上的数字时,我得不到正确的结果

你知道我做错了什么吗

多谢各位

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 calculator
{
    public partial class Calculator : Form
    {
        public Calculator()
        {
            InitializeComponent();
        }

        private void btnOne_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btnOne.Text;
            //txtDisplay.Text = btnOne.Text;
        }

        private void btnTwo_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btnTwo.Text;
        }

        private void btnThree_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btnThree.Text;
        }

        private void btnFour_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btnFour.Text;
        }

        private void btnFive_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btnFive.Text;
        }

        private void btnSix_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btnSix.Text;
        }

        private void btnSeven_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btnSeven.Text;
        }

        private void btnEight_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btnEight.Text;
        }

        private void btnNine_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btnNine.Text;
        }

        private void btnZero_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btnZero.Text;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtDisplay.Clear();
        }

        private void btnPoint_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + ",";
        }


        double total1 = 0;
        double total2 = 0;

        bool plusButtonClicked = false;
        bool minusButtonClicked = false;
        bool divideButtonClicked = false;
        bool multiplyButtonClicked = false;

        private void btnPlus_Click(object sender, EventArgs e)
        {
            plusButtonClicked = true;
            minusButtonClicked = false;
            divideButtonClicked = false;
            multiplyButtonClicked = false;

            total1 = total1 + double.Parse(txtDisplay.Text);
            txtDisplay.Clear();
        }

        private void btnMinus_Click(object sender, EventArgs e)
        {
            plusButtonClicked = false;
            minusButtonClicked = true;
            divideButtonClicked = false;
            multiplyButtonClicked = false;

            total1 = total1 + double.Parse(txtDisplay.Text);
            txtDisplay.Clear();
        }



        private void btnDivide_Click(object sender, EventArgs e)
        {
            total1 = total1 + double.Parse(txtDisplay.Text);
            txtDisplay.Clear();

            plusButtonClicked = false;
            minusButtonClicked = false;
            divideButtonClicked = true;
            multiplyButtonClicked = false;
        }

        private void btnMultiply_Click(object sender, EventArgs e)
        {
            total1 = total1 + double.Parse(txtDisplay.Text);
            txtDisplay.Clear();

            plusButtonClicked = false;
            minusButtonClicked = false;
            divideButtonClicked = false;
            multiplyButtonClicked = true;
        }



        private void btnEquals_Click(object sender, EventArgs e)
        {

            if (plusButtonClicked == true)
            {
                total2 = total1 + double.Parse(txtDisplay.Text);
            }

            else if (minusButtonClicked == true)
            {
                total2 = total1 - double.Parse(txtDisplay.Text);
            }

            else if (divideButtonClicked == true)
            {
                total2 = total1 / double.Parse(txtDisplay.Text);
            }

            else if (multiplyButtonClicked == true)
            {
                total2 = total1 * double.Parse(txtDisplay.Text);
            }


            txtDisplay.Text = total2.ToString();
            total1 = 0;
        }




    }
}

此代码尚未经过彻底测试。你为什么不试试下面的方法呢:

using System;
using System.Windows.Forms;

namespace Calculator
{
    public enum Operator
    {
        None,
        Add,
        Minus,
        Divide,
        Multiply
    }

    public partial class Calculator : Form
    {
        private double total = 0;
        private double currentValue = 0;
        private Operator currentOperator;

        public Calculator()
        {
            InitializeComponent();
        }

        private void btnOne_Click(object sender, EventArgs e)
        {
            ShowInput(btnOne.Text);
        }

        private void btnTwo_Click(object sender, EventArgs e)
        {
            ShowInput(btnTwo.Text);
        }

        private void btnThree_Click(object sender, EventArgs e)
        {
            ShowInput(btnThree.Text);
        }

        private void btnFour_Click(object sender, EventArgs e)
        {
            ShowInput(btnFour.Text);
        }

        private void btnFive_Click(object sender, EventArgs e)
        {
            ShowInput(btnFive.Text);
        }

        private void btnSix_Click(object sender, EventArgs e)
        {
            ShowInput(btnSix.Text);
        }

        private void btnSeven_Click(object sender, EventArgs e)
        {
            ShowInput(btnSeven.Text);
        }

        private void btnEight_Click(object sender, EventArgs e)
        {
            ShowInput(btnEight.Text);
        }

        private void btnNine_Click(object sender, EventArgs e)
        {
            ShowInput(btnNine.Text);
        }

        private void btnZero_Click(object sender, EventArgs e)
        {
            ShowInput(btnZero.Text);
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            currentOperator = Operator.None;
            txtDisplay.Clear();
            total = 0;
        }

        private void btnPoint_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + '.';
        }

        private void btnPlus_Click(object sender, EventArgs e)
        {
            ApplyOperator(Operator.Add);
        }

        private void btnMinus_Click(object sender, EventArgs e)
        {
            ApplyOperator(Operator.Minus);
        }

        private void btnDivide_Click(object sender, EventArgs e)
        {
            ApplyOperator(Operator.Divide);
        }

        private void btnMultiply_Click(object sender, EventArgs e)
        {
            ApplyOperator(Operator.Multiply);
        }

        private void btnEquals_Click(object sender, EventArgs e)
        {
            Evaluate();
            txtDisplay.Text = Convert.ToString(total);
        }

        private void Evaluate()
        {
            switch (currentOperator)
            {
                case Operator.Add:
                    total += currentValue;
                    break;
                case Operator.Minus:
                    total -= currentValue;
                    break;
                case Operator.Divide:
                    total /= currentValue;
                    break;
                case Operator.Multiply:
                    total *= currentValue;
                    break;
                case Operator.None:
                    break;
            }
            currentValue = 0;
            currentOperator = Operator.None;
        }

        private void ApplyOperator(Operator op)
        {
            if (currentOperator != Operator.None)
            {
                Evaluate();
            }
            else
            {
                total = double.Parse(txtDisplay.Text);
            }
            txtDisplay.Clear();
            currentOperator = op;
        }

        private void ShowInput(String n)
        {
            txtDisplay.Text = txtDisplay.Text + n;
            currentValue = double.Parse(txtDisplay.Text);
        }
    }
}

我仍然建议您最终制作某种形式的运算符解析器。亲自查看或查看“调车场”算法。

计算代码中乘积、商和差的逻辑是
total1=total1+double.Parse(txtDisplay.Text)这就是为什么添加有效,但没有其他作用。所以,改变逻辑,让它要么除法,要么乘法,要么减法,而不是加法。

想想看。减号单击的代码是将除最后一个操作数之外的所有操作数相加,然后等号单击的代码对减号单击的结果和文本框的值(我假设是最后一个操作数)进行算术运算。所以,因为你在负号中所做的运算是加法,你在x-y-z中得到的是:

(X + Y) - Z

我会考虑重构一点,但是如果你想保持代码的方式,我可能只会改变MimuScript点击代码来减去而不是添加。

而且,@rhysw是对的。如果你不想让它完全发挥作用,你还必须给它添加优先级逻辑。

在你的代码中:

 private void btnMultiply_Click(object sender, EventArgs e)
        {
            total1 = total1 + double.Parse(txtDisplay.Text);
            txtDisplay.Clear();

            plusButtonClicked = false;
            minusButtonClicked = false;
            divideButtonClicked = false;
            multiplyButtonClicked = true;
        }

您没有应用正确的运算符,您有total1=total1+。。。将操作符更改为*

我查看了代码,它看起来像是每次添加的每个按钮。所以任何时候只要点击一个按钮,你就可以继续添加。只需将OPP更改为相应的按钮。像这样:

private void btnMinus\u单击(对象发送方,事件参数e)
{
plusButtonClicked=false;
minusButtonClicked=true;
divideButtonClicked=假;
multiplyButtonClicked=false;
total1=total1-double.Parse(txtDisplay.Text);
txtDisplay.Clear();
}
私有void b未分配单击(对象发送者,事件参数)
{
total1=total1/double.Parse(txtDisplay.Text);
txtDisplay.Clear();
plusButtonClicked=false;
minusButtonClicked=false;
divideButtonClicked=true;
multiplyButtonClicked=false;
}

放置一个断点,逐行遍历代码,看看每个点的结果是否符合预期,一旦你看到出错点,你就会知道如何修复它:科学计算器使用括号分割每个总和是有原因的,以显示什么有优先级,例如你有数字10,你想把它除以10,然后再除以2,10/10=1,1/2=0.5,但这不同于10除以(10/2),10/5等于2,而不是2的一半,BIDMAS对理解非常重要,更不用说BIDMAS对确定每个动作的优先级很重要has@RhysW绝对地我想说这个。事实上,我要补充一点。然而,他一次只做一个运算。虽然我在讨论BIDMAS和优先级的重要性时,我甚至没有注意到所有的数字都加了XD,我明白这一点,但如果我在这行中加上负号,我得到了这个结果:例如:10-5-3结果是-18,因为total1在开始时是0,然后他做了这个:0-10=-10是因为“total1=total1-double.Parse(txtDisplay.Text);”,然后-10-5-3是-18。我怎样才能解决这个问题?@RhysW这种方法通过将计算限制在一次一个操作来解决这个BIDMAS问题