• Breaking News

    Funny Coder

    Funny coder is an open source web for interested programmer. It is a programming environment.It's a way where you can code with fun.

    Saturday, April 9, 2016

    Infix to Postfix Conversion with Algorithm

    Algorithm:


    1. Say Q is an infix expression and its postfix expression will be P. Now scan the infix expression from left to right.
    2. Initialize an empty stack. This is NULL.
    3. Repeat step 4 and 5 until all characters of Q are scanned.
    4. If the scanned character is an operator, add it to P.
    5. If the scanned character is an operand thenà
      1. If stack is empty then add it to P
      2. Otherwise repeatedly pop from the stack and add to P. Each operator which has the same or higher precedence.
      3. Push operator to the stack.
    6. If scanned character is “( then push it to the stack.
    7. If scanned character is “)” thenà
      1. Repeatedly pop from stack and add to P with each operator until “(“ is encountered.
      2. Remove “(“ from the stack.
    8. If scanned finish but stack will not be empty, then add stack to P.

    Finally we’ll get the postfix expression in “P” .

    Example:


    Q = 5 * (6 + 2) – 12/4
    Stack
    P
    5
    *
    (
    6
    +
    2
    )
    -
    12
    /
    4
    Finish

    *
    *(
    *(
    *(+
    *(+
    *
    -
    -
    -/
    -/
    5
    5
    5
    56
    56
    562
    562+
    562+*
    562+*12
    562+*12
    562+*124
    562+*124/-




    If you further face any problem to this you must comment below. Thanks.
    For more, Go to--> http://tech-world6.blogspot.com/2016/04/infix-to-postfix-conversion-algorithm.html



    No comments:

    Post a Comment

    Fashion

    Beauty

    Travel