Let Q be an arithmetic expression written in infix notation. This algorithm transforms the infix expression into postfix expressions.
POLISH(Q, P)
Support Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression P.
Push “(” Onto STACK, and add “)” to the end of Q.
Scan Q from left to right and repeat step 3 to 6 for each element of Q until the stack is empty:
If an operand is encountered, add it to P.
If left parenthesis is encountered, push it onto STACK.
If an operator is encountered, then:
Repeatedly pop from stack and add to P each operator (on the top of the STACK) which has the same precedence or higher precedence than the operator
Add operator to STACK
[End of If structure]
If a right parenthesis encounter, then :
Repeatedly pop from the stack and add it to P each operator (on the top of the stack) until a left parenthesis is encountered.
Remove the left [Do not add the left parenthesis to P].
[End of if structure]
[End of step 2 loop]
Exit
No comments:
Post a Comment