infix to postifix

Upload: sathish-kumar

Post on 14-Apr-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 Infix to Postifix

    1/41

    Prefix, Postfix, Infix

    NotationUniversity College of

    Engineering, Kanchipuram

  • 7/27/2019 Infix to Postifix

    2/41

    Infix Notation

    To add A, B, we writeA+B

    To multiply A, B, we writeA*B

    The operators ('+' and '*') go inbetween the operands ('A' and 'B')This is "Infix"notation.

  • 7/27/2019 Infix to Postifix

    3/41

    Prefix Notation

    Instead of saying "A plus B", wecould say "add A,B " and write

    + A B"Multiply A,B" would be written

    * A BThis is Prefixnotation.

  • 7/27/2019 Infix to Postifix

    4/41

    Postfix Notation

    Another alternative is to put theoperators after the operands as in

    A B +and

    A B *This is Postfixnotation.

  • 7/27/2019 Infix to Postifix

    5/41

    The terms infix, prefix, and postfixtell us whether the operators gobetween, before, or after theoperands.

  • 7/27/2019 Infix to Postifix

    6/41

    Parentheses

    Evaluate 2+3*5.+ First:

    (2+3)*5 = 5*5 = 25* First:

    2+(3*5) = 2+15 = 17Infix notation requires Parentheses.

  • 7/27/2019 Infix to Postifix

    7/41

    What about Prefix Notation?

    + 2 * 3 5 == + 2 * 3 5= + 2 15 = 17

    * + 2 3 5 =

    = * + 2 3 5= * 5 5 = 25

    No parentheses needed!

  • 7/27/2019 Infix to Postifix

    8/41

    Postfix Notation

    2 3 5 * + == 2 3 5 * += 2 15 + = 17

    2 3 + 5 * =

    = 2 3 + 5 *= 5 5 * = 25

    No parentheses needed here either!

  • 7/27/2019 Infix to Postifix

    9/41

    Conclusion:

    Infix is the only notation thatrequires parentheses in order tochange the order in which theoperations are done.

  • 7/27/2019 Infix to Postifix

    10/41

  • 7/27/2019 Infix to Postifix

    11/41

    Infix to Prefix Conversion

    Move each operator to the left of itsoperands & remove the parentheses:

    ( ( A + B) * ( C + D ) )

  • 7/27/2019 Infix to Postifix

    12/41

    Infix to Prefix Conversion

    Move each operator to the left of itsoperands & remove the parentheses:

    ( + A B * ( C + D ) )

  • 7/27/2019 Infix to Postifix

    13/41

    Infix to Prefix Conversion

    Move each operator to the left of itsoperands & remove the parentheses:

    * + A B ( C + D )

  • 7/27/2019 Infix to Postifix

    14/41

    Infix to Prefix Conversion

    Move each operator to the left of itsoperands & remove the parentheses:

    * + A B + C D

    Order of operands does not change!

  • 7/27/2019 Infix to Postifix

    15/41

    Infix to Postfix

    ( ( ( A + B ) * C ) - ( ( D + E ) / F ) )

    A B + C * D E + F / -

    Operand order does not change!Operators are in order of evaluation!

  • 7/27/2019 Infix to Postifix

    16/41

    Computer AlgorithmFPE Infix To Postfix

    Assumptions:1. Space delimited list of tokens

    represents a FPE infix expression2. Operands are single characters.

    3. Operators +,-,*,/

  • 7/27/2019 Infix to Postifix

    17/41

    FPE Infix To Postfix

    Initialize a Stack for operators,

    output listSplit the input into a list of tokens.for each token (left to right):

    if it is operand: append to outputif it is '(': push onto Stackif it is ')': pop & append till '('

  • 7/27/2019 Infix to Postifix

    18/41

    FPE Infix to Postfix

    ( ( ( A + B ) * ( C - E ) ) / ( F + G ) )

    stack: output: []

  • 7/27/2019 Infix to Postifix

    19/41

    FPE Infix to Postfix

    ( ( A + B ) * ( C - E ) ) / ( F + G ) )

    stack: (output: []

  • 7/27/2019 Infix to Postifix

    20/41

    FPE Infix to Postfix

    ( A + B ) * ( C - E ) ) / ( F + G ) )

    stack: ( (output: []

  • 7/27/2019 Infix to Postifix

    21/41

    FPE Infix to Postfix

    A + B ) * ( C - E ) ) / ( F + G ) )

    stack: ( ( (output: []

  • 7/27/2019 Infix to Postifix

    22/41

    FPE Infix to Postfix

    + B ) * ( C - E ) ) / ( F + G ) )

    stack: ( ( (output: [A]

  • 7/27/2019 Infix to Postifix

    23/41

    FPE Infix to Postfix

    B ) * ( C - E ) ) / ( F + G ) )

    stack: ( ( ( +output: [A]

  • 7/27/2019 Infix to Postifix

    24/41

    FPE Infix to Postfix

    ) * ( C - E ) ) / ( F + G ) )

    stack: ( ( ( +output: [A B]

  • 7/27/2019 Infix to Postifix

    25/41

    FPE Infix to Postfix

    * ( C - E ) ) / ( F + G ) )

    stack: ( (output: [A B + ]

  • 7/27/2019 Infix to Postifix

    26/41

    FPE Infix to Postfix

    ( C - E ) ) / ( F + G ) )

    stack: ( ( *output: [A B + ]

  • 7/27/2019 Infix to Postifix

    27/41

    FPE Infix to Postfix

    C - E ) ) / ( F + G ) )

    stack: ( ( * (output: [A B + ]

  • 7/27/2019 Infix to Postifix

    28/41

    FPE Infix to Postfix

    - E ) ) / ( F + G ) )

    stack: ( ( * (output: [A B + C ]

  • 7/27/2019 Infix to Postifix

    29/41

    FPE Infix to Postfix

    E ) ) / ( F + G ) )

    stack: ( ( * ( -output: [A B + C ]

  • 7/27/2019 Infix to Postifix

    30/41

    FPE Infix to Postfix

    ) ) / ( F + G ) )

    stack: ( ( * ( -output: [A B + C E ]

  • 7/27/2019 Infix to Postifix

    31/41

    FPE Infix to Postfix

    ) / ( F + G ) )

    stack: ( ( *output: [A B + C E - ]

  • 7/27/2019 Infix to Postifix

    32/41

    FPE Infix to Postfix

    / ( F + G ) )

    stack: (output: [A B + C E - * ]

  • 7/27/2019 Infix to Postifix

    33/41

    FPE Infix to Postfix

    ( F + G ) )

    stack: ( /output: [A B + C E - * ]

  • 7/27/2019 Infix to Postifix

    34/41

    FPE Infix to Postfix

    F + G ) )

    stack: ( / (output: [A B + C E - * ]

  • 7/27/2019 Infix to Postifix

    35/41

    FPE Infix to Postfix

    + G ) )

    stack: ( / (output: [A B + C E - * F ]

  • 7/27/2019 Infix to Postifix

    36/41

    FPE Infix to Postfix

    G ) )

    stack: ( / ( +output: [A B + C E - * F ]

  • 7/27/2019 Infix to Postifix

    37/41

    FPE Infix to Postfix

    ) )

    stack: ( / ( +output: [A B + C E - * F G ]

  • 7/27/2019 Infix to Postifix

    38/41

    FPE Infix to Postfix

    )

    stack: ( /output: [A B + C E - * F G + ]

  • 7/27/2019 Infix to Postifix

    39/41

    FPE Infix to Postfix

    stack: output: [A B + C E - * F G + / ]

  • 7/27/2019 Infix to Postifix

    40/41

    Problem with FPE

    Too many parentheses.Establish precedence rules:

    My Dear Aunt SallyWe can alter the previous program to

    use the precedence rules.

  • 7/27/2019 Infix to Postifix

    41/41

    Infix to Postfix

    Initialize a Stack for operators, outputlistSplit the input into a list of tokens.for each token (left to right):

    if it is operand: append to outputif it is '(': push onto Stackif it is ')': pop & append till '('if it in '+-*/':

    while peek has precedence it:pop & append

    push onto Stackpop and append the rest of the Stack.