Question: C++: We normally write arithmetical expressions using infix notation, meaning that the operator appears between its two operands, as in 4 + 5. In postfix
C++:
We normally write arithmetical expressions using infix notation, meaning that the operator appears between its two operands, as in "4 + 5". In postfix notation, the operator appears after its operands, as in "4 5 +". Here is a slightly more complex postfix expression: "25 12 7 - 2 * /". The equivalent infix expression is: "25 / ((12 - 7) * 2)". The result of that expression should be 2.5 (beware integer division). Postfix expressions don't require parentheses.
Write a function named postfixEval that uses a stack
Hint: Read a postfix expression from left to right. When you read a number, push it on the stack. When you read an operand, pop the top two numbers off the stack, apply the operator to them, and push the result on top of the stack. At the end, the result of the expression should be the only number on the stack.
File must be called: postfixEval.cpp
Sample test case:
char expr[] = "14 21 -"; double ans = postfixEval(expr); ASSERT_NEAR(ans, -7.0, 0.00001);
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
