C#计算器未正确评估 [英] C# Calculator not evaluating properly

查看:118
本文介绍了C#计算器未正确评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,最近我开始构建一个普通的计算器,最近我能够设计它,以便它可以在一次计算中读取两个以上的数字,但只能使用一个单一的运算符(例如1 + 2 + 3 + 4 = 10)。但是,当我尝试使用多个运算符进行计算时,它无法正确计算(例如4 + 3 + 2-1 = 6?)。我相信我需要添加某种类型的新标志以允许多个运算符计算,但我不知道如何去做。我到目前为止的代码如下:



Hey guys, recently I have started to build a normal calculator which recently I was able to design it so that it can read more than two numbers in a single calculation but only with one single operator (e.g. 1+2+3+4 = 10). However when I try to do calculations with multiple operators, it doesn't evaluate properly (e.g. 4+3+2-1 = 6?). I believe that I need to add new flags of some sort to allow for multiple operator calculations but I am not sure how to go about it. The code I have so far is as followed:

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 WindowsFormsApplication1
{
    public partial class Calculator : Form
    {
        
        int firstNum = 0;
        int secondNum = 0;
        int total;
        string op = "";
        bool NumReady = false;
        bool firstNumReady = false;


        public Calculator()
        {
            InitializeComponent();
        }

        private void Buttton0_Click(object sender, EventArgs e)
            {
            if (!NumReady)
            {
                textBox1.Text += "0";
            }
            else
            {
                textBox1.Text = "0";
                NumReady = false;
            }
        }
        private void Button1_Click(object sender, EventArgs e)
             {
            if (!NumReady)
            {
                textBox1.Text += "1";
            }
            else
            {
                textBox1.Text = "1";
                NumReady = false;
            }
        }

        private void Button2_Click(object sender, EventArgs e)
           {
            if (!NumReady)
            {
                textBox1.Text += "2";
            }
            else
            {
                textBox1.Text = "2";
                NumReady = false;
            }
        }

        private void Button3_Click(object sender, EventArgs e)
           {
            if (!NumReady)
            {
                textBox1.Text += "3";
            }
            else
            {
                textBox1.Text = "3";
                NumReady = false;
            }
        }

        private void Button4_Click(object sender, EventArgs e)
          {
            if (!NumReady)
            {
                textBox1.Text += "4";
            }
            else
            {
                textBox1.Text = "4";
                NumReady = false;
            }
        }
        private void Button5_Click(object sender, EventArgs e)
           {
            if (!NumReady)
            {
                textBox1.Text += "5";
            }
            else
            {
                textBox1.Text = "5";
                NumReady = false;
            }
        }

        private void Button6_Click(object sender, EventArgs e)
        {
            if (!NumReady)
            {
                textBox1.Text += "6";
            }
            else
            {
                textBox1.Text = "6";
                NumReady = false;
            }
        }

        private void Button7_Click(object sender, EventArgs e)
         {
            if (!NumReady)
            {
                textBox1.Text += "7";
            }
            else
            {
                textBox1.Text = "7";
                NumReady = false;
            }
        }

        private void Button8_Click(object sender, EventArgs e)
         {
            if (!NumReady)
            {
                textBox1.Text += "8";
            }
            else
            {
                textBox1.Text = "8";
                NumReady = false;
            }
        }

        private void Button9_Click(object sender, EventArgs e)
         {
            if (!NumReady)
            {
                textBox1.Text += "9";
            }
            else
            {
                textBox1.Text = "9";
                NumReady = false;
            }
        }
        
        private void ButtonAdd_Click(object sender, EventArgs e)
        {

            if (firstNumReady && op != "")
            {
                secondNum = int.Parse(textBox1.Text);
                total = firstNum + secondNum;
                textBox1.Text = total.ToString();
                firstNum = total;
                NumReady = true;

            }
            else
            {
                op = "+";
                firstNum = int.Parse(textBox1.Text);
                NumReady = true;
                firstNumReady = true;
            }
            //textBox1.Text = "";
        }

        private void ButtonSub_Click(object sender, EventArgs e)
        {

            if (firstNumReady && op != "")
            {
                secondNum = int.Parse(textBox1.Text);
                total = firstNum - secondNum;
                textBox1.Text = total.ToString();
                firstNum = total;
                NumReady = true;

            }
            else
            {
                op = "-";
                firstNum = int.Parse(textBox1.Text);
                NumReady = true;
                firstNumReady = true;
            }
            //textBox1.Text = "";
        }

        private void ButtonMult_Click(object sender, EventArgs e)
        {

            if (firstNumReady && op != "")
            {
                secondNum = int.Parse(textBox1.Text);
                total = firstNum * secondNum;
                textBox1.Text = total.ToString();
                firstNum = total;
                NumReady = true;

            }
            else
            {
                op = "*";
                firstNum = int.Parse(textBox1.Text);
                NumReady = true;
                firstNumReady = true;
            }
            //textBox1.Text = "";
        }


        private void ButtonDiv_Click(object sender, EventArgs e)
        {

            if (firstNumReady && op != "")
            {
                secondNum = int.Parse(textBox1.Text);
                total = firstNum / secondNum;
                textBox1.Text = total.ToString();
                firstNum = total;
                NumReady = true;

            }
            else
            {
                op = "/";
                firstNum = int.Parse(textBox1.Text);
                NumReady = true;
                firstNumReady = true;
            }
            //textBox1.Text = "";
        }

        private void ButtonClr_Click(object sender, EventArgs e)
        {
            textBox1.Text = op  = "";
            firstNum = secondNum = total = 0;
            NumReady = firstNumReady = false;
        }               
        

        private void ButtonEq_Click(object sender, EventArgs e)
         {
            
                secondNum = int.Parse(textBox1.Text);

                if (op == "+")
                {
                   total = firstNum + secondNum; 
                }
                else if (op == "-")
                {
                    total = firstNum - secondNum;
                }
                else if (op == "*")
                {
                    total = firstNum * secondNum;                    
                }
                else if (op == "/")
                {
                    total = firstNum / secondNum;
                }                                      
                    textBox1.Text = total.ToString();
                    NumReady = true;
                    firstNumReady = true;
                    op = "";
                }          

            }
        }





我的问题是如何去做解决了我的程序中能够进行多个运算符计算的问题。



My question is how do I go about fixing this problem of being able to have multiple operator calculations in my program.

推荐答案

我建​​议您使用Stack来评估数学表达式。



另一种评估数学表达式的方法是

1.通过递归下降

2.分流码算法

3.反向波兰表示法
I suggest you use Stack in order to evaluate mathematical expressions.

Another ways of evaluating mathematical expressions are
1. By Recursive Descent
2. Shunting-yard algorithm
3. Reverse Polish notation


我听到你,我有同样的问题。仍在全力以赴地工作,但到目前为止它按要求工作。



我通过添加一些变量来修复我的。一,它知道先前是否按下了操作员,一个是保持当前操作员,另一个是先前按下的操作员。如果我有1 + 1然后按=,它将检查使用的运算符并进行计算。



如果我按下运算符,它将设置变量如果它是假的,则为true,所以如果在等于之前按下另一个运算符,它将进行小计,然后根据当前按下的变量进行计算。



我的代码在该示例仅使用textBox,1键,+键和=键。



I hear ya, I had this same problem. Still working things out on a full scale, but so far it works as required.

I fixed mine by adding a few variables. One so it knows if an operator has been pressed previously, one which holds the current operator, and one for the previously pressed operator. If i had 1+1 and then pressed =, it would check for the operator used and make the calculation.

If I pressed an operator, it would set the variable to true if it was false, so if another operator was pressed before equals, it would make a subtotal and then calculate based on the currently pressed variable.

My code in the example uses only a textBox, 1 key, + key and = key.

double subtotal = 0; //create variable to hold ongoing totals
        bool operatorPressed = false; //create variable for if an operator key is pressed. This will enable the program to know to add/sub/mult/div the subtotal and the displayed amount
        string currentOperator = ""; //string to hold the last pressed operator.
        string previousOperator = ""; //string to hold previous operator when a new operator is pressed.

        private void button1_Click(object sender, EventArgs e) //1 key
        {
            textBox1.Text = button1.Text; //send button text to display
        }


        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text = button4.Text;
        }


        private void button8_Click(object sender, EventArgs e) //new +
        {
            if (operatorPressed == true) //check if an operator has previously been pressed
            {
                previousOperator = currentOperator; //set previousOperator variable to value of currentOperator
                currentOperator = "+"; // set currentOperator to value of pressed key
                switch (previousOperator)
                {
                    case "+":
                        subtotal = subtotal + double.Parse(textBox1.Text);
                        textBox1.Text = subtotal.ToString();
                        break;

                    case "*":
                        subtotal = subtotal * double.Parse(textBox1.Text);
                        textBox1.Text = subtotal.ToString();
                        break;
                }
                
            }
            else if(operatorPressed == false)
            {
                operatorPressed = true;
                currentOperator = "+";
                subtotal = double.Parse(textBox1.Text);
            }
        }

        private void button10_Click(object sender, EventArgs e) // new *
        {
            if (operatorPressed == true) //check if an operator has previously been pressed
            {
                previousOperator = currentOperator; //set previousOperator variable to value of currentOperator
                currentOperator = "*"; // set currentOperator to value of pressed key
                switch (previousOperator)
                {
                    case "+":
                        subtotal = subtotal + double.Parse(textBox1.Text);
                        textBox1.Text = subtotal.ToString();
                        break;

                    case "*":
                        subtotal = subtotal * double.Parse(textBox1.Text);
                        textBox1.Text = subtotal.ToString();
                        break;
                }
            }
            else if (operatorPressed == false)
            {
                operatorPressed = true;
                currentOperator = "*";
                subtotal = double.Parse(textBox1.Text);
            }
        }

        private void button9_Click(object sender, EventArgs e) //new =
        {
            switch(currentOperator)
            {
                case "+":
                    subtotal = subtotal + double.Parse(textBox1.Text);
                    textBox1.Text = subtotal.ToString();
                    subtotal = 0;
                    currentOperator = "";
                    previousOperator = "";
                    operatorPressed = false;
                    break;
                
                case "*":
                    subtotal = subtotal * double.Parse(textBox1.Text);
                    textBox1.Text = subtotal.ToString();
                    subtotal = 0;
                    currentOperator = "";
                    previousOperator = "";
                    operatorPressed = false;
                    break;
            }
        }





我希望这很清楚



I hope this is clear enough to understand


这篇关于C#计算器未正确评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆