本文介绍了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.

推荐答案


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#计算器未正确评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:38