Somethings not right

I am writing a calculator that puts the operands in one stack and the operators in the other stack. It then checks the precedence and if the precedence is higher performs the operation and puts the answer back on the operands stack to continue processing the equation using order of operations. The numbers are going on the stack but there is something wrong in the processing of the equation. Please help!

Here's the header file which cannot be changed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  #include <string>
#include "ArrayStack.h"
using namespace std;

class Calculator
{
private:
   /**
    * stores the operands (the numbers) in the arithmetic expression while the 
    * expression is being evaluated
    */
   ArrayStack<int> operands; 

   /**
    * stores the operators in the arithmetic expression while the expression is
    * being evaluated
    * 
    */
   ArrayStack<char> operators; 

   /**
    * original arithmetic expression
    */
   string expression;

   /**
    * Determines if the character argument is one of the 4 valid operators: +, -.
    * *, or /.
    * @param ch - character to examine to determine if it is a valid operator
    * @return true if the argument is a valid operator, false otherwise
    */
   bool isOperator (char ch)
   {
      return (ch == '+' || ch == '*' || ch == '-' || ch == '/');
   }


   /**
    * Beginning at location index in the expression, collects all the digits in
    * the operand, stopping when a non-digit character is encountered. Converts
    * the digits in the operand to an integer.
    * @param index - location in expression of first character in the operand
    * @return operand's integer value
    */
   int getOperand (int &index)
   {
      int operand = 0;
      while (index < expression.length() && isdigit(expression[index]))
      {
         operand = (operand * 10) + (expression[index] - '0');
         index++;
      }
      return operand;
   }


   /**
    * Processes the top operator on the operators stack by removing 2 operands
    * from the operands stack and one operator from the operators stack. Performs
    * the designated calculation and pushes the result back onto the operands 
    * stack. Checks to be certain the stacks are not empty before attempting to
    * remove items from them.
    */
   void processOperator ();

   /**
    * Determines if the argument has a higher precedence than the operator 
    * currently on top of the operators stack.
    * @param op - operator being compared to the one on top of the operators stack
    * @return - true if op has a higher precedence than the one on top of the 
    *           operators stack; false otherwise
    */
   bool higherPrecedence(char op);
public:
   /**
    * Default constructor. Initializes expression to an empty string.
    */
   Calculator () 
   {
      expression = "";
   }


   /**
    * Overloaded constructor that initializes arithmetic expression.
    * @param - newExpression - value to use to initialize arithmetic expression
    */
   Calculator (string newExpression) 
   {
      expression = newExpression;
   }
   
   /**
    * Evaluates the arithmetic expression and returns the result, following all
    * the precedence rules for the 4 allowed operators: =, -, *, and /
    * @return result of calculation for arithmetic expression
    */
   int calculate (); 
};


Here's the .cpp(most of the couts are error checking):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <cstdlib>
#include <iostream>
#include "Calculator.h"

void Calculator::processOperator ()
{
   int temp;
   char op = operators.peek();

   if(!operators.isEmpty() && (higherPrecedence(op)== true))
   {
      int x = operands.peek();
      operands.pop();
      int y = operands.peek();
      operands.pop();
      char op = operators.peek();
      operators.pop();
      switch (op)
      {
         case '+': temp = x + y;
            break;
         case '-': temp = x - y;
            break;
         case '*': temp = x * y;
            break;
         case '/': temp = x / y;
            break;
         default: cout << "Invalid operator, press any key to terminate.";
      }//end switch
      operands.push(temp);
   }//end if
}//end processOperator()


bool Calculator::higherPrecedence(char op)
{
   if(op >= operators.peek())
   { 
      return true;
   }// end if
   else
   {
      return false;
   }//end else
}//end higherPrecedence()
   

int Calculator::calculate ()
{
   cout << "Please enter an expression: " << endl;
   cin >> expression;
   cout << "Your expression is: " << expression << endl;
   cout << "The length of your expression " << expression.length() << endl;
   int i = 0;
   cout << "Entering While Loop" << endl;
   
   while(i < expression.length())
   {
      char op = expression[i];
      
      if(isOperator(op)== true)// if b
      {
         cout << "Checking if Empty." << endl;
         if(operators.isEmpty())
         {
         cout << "Pushing operator" << endl;
         char tempChar = expression[i];
         operators.push(tempChar);
         cout << "operators stack: " << operators.peek() << endl;
         i++;
         }
         else
         {
         cout << "Processing Operator" << endl;
         processOperator();
         i++;
         }
      }//end if b
      else 
      {
         cout << "Pushing Operand" << endl;
         int tempOp = getOperand(i);
         operands.push(tempOp);
         cout << "operands stack: " << operands.peek() << endl;
      }//end else
   }//end while
   cout << "Returning" << endl;
   return operands.peek();
}//end calculate() 
Topic archived. No new replies allowed.