Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a program that uses C++ STL stacks to evaluate a postfix expression. The postfix expression can use the binary operators: *, /,+,-, and %.
Write a program that uses C++ STL stacks to evaluate a postfix expression. The postfix expression can use the binary operators: *, /,+,-, and %. The program should read the expression in postfix notation. Your program should work only on single digits operands (0 through 9). Do not add code to handle more than one digit operands.
Your program should use a while loop or for-loop to allow the user to enter and evaluate as many expressions as the user needs. The program should issue an error message if the expression is invalid (for example: if the stack is empty or does not have enough operands for a given operator or if the stack is not empty after the entire expression has been evaluated) It should also check for division by zero (% and /).
At the end, the program should display the number of invalid expressions, the number of valid expressions, and the total number of expressions the program processed.
Use the following expressions to test your program after converting them to postfix notation. You should not hard-code these expressions in your program.
(2+3) * (5-1) =20 the postfix notation is 2 3 + 5 1 - *
2+(3*5) 1 = 16 the postfix notation is 2 3 5 * + 1 -
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started