Question
Postfix Evaluation PartII- Write a generic C++ function that evaluates a valid postfix expression such as 6 2 + 5 * 8 4 / -
Postfix Evaluation PartII-
Write a generic C++ function that evaluates a valid postfix expression such as
6 2 + 5 * 8 4 / -
The function should read a postfix expression consisting of digits and operators into a string.
Using an implementation modified of the stack functions implemented for partI,
the program should scan the expression and evaluate it.
The algorithm is as follows:
1. While you have not reached the end of the string, read the expression from left to right.
If the current character is a digit,
Push its integer value onto the stack (the integer value of a digit character is its value in the computers character set minus the value of '0' in the computers character set).
Otherwise, if the current character is an operator, Pop the two top elements of the stack into variables x and y. Calculate y operator x. Push the result of the calculation onto the stack.
2. When you reach the end of the string, pop the top value of the stack. This is the result of the postfix expression.
[Note: In Step 2 above, if the operator is '/', the top of the stack is 2 and the next element in the stack is 8, then pop 2 into x, pop 8 into y, evaluate 8 / 2 and push the result, 4, back onto the stack. This note also applies to operator ''.] The arithmetic operations allowed in an expression are
+ addition
subtraction
* multiplication
/ division
^ exponentiation
% remainder
[Note: We assume left-to-right associativity for all operators for the purpose of this exercise.] The stack should be maintained with stack nodes that contain an int data member and a pointer to the next stack node.
You may want to provide the following functional capabilities:
1. function evaluatePostfixExpression that evaluates the postfix expression
2. function calculate that evaluates the expression op1 operator op2
3. function push that pushes a value onto the stack
4. function pop that pops a value off the stack
5. function isEmpty that determines if the stack is empty
6. function printStack that prints the stack
You are to write your Template stack header and implementation, two functions for parts I & II.
Then test your code in main with a menu to ask the user to select from
IMPORTANT!!!
A- infix-to-postfix
B- Postfix-to-infix
C- Exit
Whenever there is a user entry, there is a validation (10 Points)
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