Title: Infix to postfix conversion
1Infix to postfix conversion
- Process the tokens from a vector infixVect of
tokens (strings) of an infix expression one by
one - When the token is an operand
- Add it to the end of the vector postfixVect of
token (strings) that is used to store the
corresponding postfix expression - When the token is a left or right parenthesis or
an operator - If the token x is (
- Push the token x onto the stack
- if the token x is )
- Repeatedly pop a token y from the stack and
push_back that token y to postfixVect until (
is encountered in the end of the stack. Pop (
from the stack. - If the stack is already empty before finding a
(, this expression is not a valid infix
expression. - if the token x is a regular operator
- Step 1 Check the token y currently on the top of
the stack. - Step 2
- If (i) the stack is not empty, (ii) y is not
( and (iii) y is an operator of higher or
equal precedence than that of x, - then pop the token y from the stack and
push_back the token y to postfixVect, and repeat
Step 1 again - else push the token x onto the stack.
- When all tokens in infixVect are processed as
described above, repeatedly pop a token y from
the stack and push_back that token y to
postfixVect until the stack is empty.
2Infix to postfix conversion
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
3Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
(
4Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1
(
5Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1
(
6Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1 2
(
7Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1 2
-
(
8Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1 2 3
-
(
9Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1 2 3 -
10Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1 2 3 -
11Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1 2 3 - 4
12Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1 2 3 4
-
13Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1 2 3 4
(
-
14Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1 2 3 4 5
(
-
15Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1 2 3 4 5
(
-
16Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6)
postfixVect
1 2 3 4 5 6
(
-
17Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1 2 3 4 5 6
-
18Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1 2 3 4 5 6