1 / 18

Infix to postfix conversion

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

Download Presentation

Infix to postfix conversion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 + –

More Related