Click here to Skip to main content
15,881,862 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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.
Posted

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
 
Share this answer
 
Comments
theunk0wn 23-Mar-13 8:19am    
This looks like scary stuff. I'll have to read up more on it.
Steve44 23-Mar-13 21:22pm    
Indeed, since your current code is not taking operator precedence into account and it would be difficult to do that in your current structure.
In addition your code has a major bug: whenever you press an operator you check, if op != "", but then you take the first and second value and don't execute the stored op, but the currently pressed one, which is surely not what you intend.
Reading up on above algorithms and re-defining your calculator accordingly would definitely be beneficial.
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.

C#
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900