1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Problem with Conversion from Infix to Postfix Notation

  1. Mar 5, 2017 #1
    1. The problem statement, all variables and given/known data
    I am trying to convert following from infix to post fix:

    (4+8)*(6-5)/((3-2)*(2+2))

    I am facing problem at character 20 which is a ‘(‘ i.e. opening bracket.

    2. Relevant equations
    There is no eq but the algorihm is given below:
    I got following algorithm from internet.

    1. if (t is an operand) output t.

    2. else if (t is a right parentheses){ POP and output tokens until a left parentheses is popped(but do not output) }

    3. else { POP and output tokens until one of lower priority than t is encountered or a left parentheses is encountered. or the stack is empty. PUSH t. }



    3. The attempt at a solution
    I am facing problem at character 20 which is a ‘(‘ i.e. opening bracket. At character 19 i.e. at '*' i.e. (asterisk) I have following status:

    Character = *

    Stack = /(*

    Output = 4 8 + 6 5 - * 3 2 –

    At character 20 i.e. ( , as per above algorithm, I would come to step 3. so I am popping tokens & I have following status after character 20:

    Character = (

    Stack = /((

    Output = 4 8 + 6 5 - * 3 2 – *

    But the above is wrong. Some body please guide me what is my mistake?


    Zulfi.
     
    Last edited by a moderator: Mar 5, 2017
  2. jcsd
  3. Mar 6, 2017 #2

    jedishrfu

    Staff: Mentor

  4. Mar 7, 2017 #3
    Hi,
    I have got a problem with precedence. According to BODMAS rule, division has precedence over multiplication but the infix to posfix conversion algorithm is working opposite to this rule & giving precedence to division over multiplication. For instance in the expression:
    (4+8)*(6-5)/((3-2)*(2+2))

    At character# 12 which is /, we have following situation:
    Character: /
    Stack: *
    Output: 4 8 + 6 5 -
    I have seen that many solutions show:
    Stack: /
    Output: 4 8 + 6 5 - *

    I think this is not right because / has more precedence over *.

    Some body please guide me.
    Zulfi.
     
  5. Mar 7, 2017 #4

    berkeman

    User Avatar

    Staff: Mentor

    From Wikipedia:
    https://en.wikipedia.org/wiki/Order_of_operations
    I'm no expert on the Order of Precedence, but multiplication and division have the same precedence, I believe. So in your equation, without more brackets in the equation, the usual factor that decides whether * or / is done first is left-to-right. So in your equation, the * would be applied after seeing the / character and before the terms to the right of the / character are parsed.
     
  6. Mar 8, 2017 #5
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Problem with Conversion from Infix to Postfix Notation
  1. C++: Postfix to Infix (Replies: 1)

Loading...