Question
IN JAVA: For this lab we will be building an expression translator to convert infix notation to postfix notation . Typical expressions to be evaluated
IN JAVA:
For this lab we will be building an expression translator to convert infix notation to postfix notation. Typical expressions to be evaluated are as follows:
infix expression | postfix expression |
---|---|
a + b - c | a b + c - |
a - b + c * d | a b - c d * + |
a + c - h / b * r | a c + h b / r * - |
a + ( c - h ) / ( b * r ) | a c h - b r * / + |
An algorithm to accomplish the transformation is:
R1: Initially the operatorStack is empty
R2: For each operator of the infix string loop until the operator has been pushed onto the operatorStack:
If the operatorStack is empty or the operator has a higher precedence than the operator on the top of the operatorStack then push the operator onto the operatorStack
else pop the operatorStack and append that popped operator to the postfix string
R3: Once the end ot the input string is encountered
loop until the operatorStack is empty: Pop the operatorStack and append that popped operator to the postfix string
infix | operatorStack | postfix |
---|---|---|
a | empty | a |
+ | + | a |
c | + | a c |
- | - | a c + |
h | - | a c + h |
/ | -/ | a c + h |
b | -/ | a c + h b |
* | -* | a c + h b / |
r | -* | a c + h b / r |
- | a c + h b / r * | |
empty | a c + h b / r * - |
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