Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int calculateReversePolishNotation(const string reversePolishNotation) // 1 2+ 2 3+* 2^
- {
- StackArray<int>* calculators = new StackArray<int>;
- int tempResult = 0;
- for (int i = 0; i < reversePolishNotation.length(); i++)
- {
- switch (reversePolishNotation[i])
- {
- case '+':
- {
- tempResult = calculators->pop() + calculators->pop();
- calculators->push(tempResult);
- break;
- }
- case '-':
- {
- int rightOperand = calculators->pop();
- int leftOperand = calculators->pop();
- tempResult = leftOperand - rightOperand;
- calculators->push(tempResult);
- break;
- }
- case '*':
- {
- tempResult = calculators->pop() * calculators->pop();
- calculators->push(tempResult);
- break;
- }
- case '/':
- {
- int rightOperand = calculators->pop();
- int leftOperand = calculators->pop();
- tempResult = leftOperand / rightOperand;
- calculators->push(tempResult);
- break;
- }
- case '^':
- {
- int rightOperand = calculators->pop();
- int leftOperand = calculators->pop();
- tempResult = pow(leftOperand, rightOperand);
- calculators->push(tempResult);
- break;
- }
- default:
- {
- //cout << reversePolishNotation[i] << '\n';
- //cout << reversePolishNotation[i] - '0';
- calculators->push((reversePolishNotation[i] - '0'));
- break;
- }
- }
- }
- return calculators->pop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement