Infix to postfix conversion - PowerPoint PPT Presentation

About This Presentation
Title:

Infix to postfix conversion

Description:

Title: CSC2100B Author: CSE Last modified by: root Created Date: 1/22/2003 11:02:22 AM Document presentation format: On-screen Show Company: cuhk Other titles – PowerPoint PPT presentation

Number of Views:127
Avg rating:3.0/5.0
Slides: 19
Provided by: CSE138
Learn more at: http://csci.biola.edu
Category:

less

Transcript and Presenter's Notes

Title: Infix to postfix conversion


1
Infix 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.

2
Infix to postfix conversion
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
3
Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
(
4
Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1
(
5
Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1

(
6
Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1 2

(
7
Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1 2
-
(
8
Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1 2 3
-
(
9
Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1 2 3 -
10
Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1 2 3 -

11
Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1 2 3 - 4

12
Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1 2 3 4
-
13
Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1 2 3 4
(
-
14
Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1 2 3 4 5
(
-
15
Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1 2 3 4 5

(
-
16
Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6)
postfixVect
1 2 3 4 5 6

(
-
17
Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1 2 3 4 5 6
-
18
Infix to postfix conversion
stack
infixVect
( 1 2 3 ) 4 ( 5 6 )
postfixVect
1 2 3 4 5 6
Write a Comment
User Comments (0)
About PowerShow.com