### trying to write a calculator

I'm trying to write a calculator program that places int in one stack and char in another, checks the precedence of the char and then if the precedence is high than the char on the stack, performs the operation.

This is what I have so far, but it doesn't seem right to me.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273`` ``````#include #include #include "Calculator.h" void Calculator::processOperator () { int temp; while(!operators.isEmpty()) { 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 while }//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; for(int i; i < expression.length(); i++) { char op = operands.peek(); higherPrecedence(op); if(higherPrecedence(op) == false) //if a { if(!isOperator(expression[i]))// if b { int tempOp = getOperand(i); operands.push(tempOp); }//end if b else if(isOperator(expression[i])) { operators.push(expression[i]); }//end for }//end if a else { processOperator(); } }//end for }//end calculate() ``````
Topic archived. No new replies allowed.