C++ Program to Convert Infix Expressions to Postfix (Algorithm)

10-07-2016

AIM:

Write a program to convert infix expressions to postfix.

ALGORITHM:

1. Push left parenthesis onto STACK and add right parenthesis at the end of Q.

2. Scan Q from left to right and repeat step 3 to 6 for each element of Q until the STACK is empty.

3. If an operand is encountered add it to P.

4. If a left parenthesis is encountered push it onto the STACK.

5. If an operator is encountered, then
(a) Repeatedly pop from STACK and add to P each operator
which has same precedence as or higher precedence than the operator
encountered.
(b) Push the encountered operator onto the STACK.

6. If a right parenthesis is encountered, then
(a) Repeatedly pop from the STACK and add to P each operator
until a left parenthesis is encountered.
(b) Remove the left parenthesis; do not add it to P.

7. Exit

Tagged in: ,