postfix expressions

This is a discussion on postfix expressions within the C Programming forums, part of the General Programming Boards category; how do i evaluate the following two expressions?? 9 8 2 - 3 / + I come up with 9 ...

  1. #1
    Unregistered
    Guest

    postfix expressions

    how do i evaluate the following two expressions??

    9 8 2 - 3 / +

    I come up with
    9 - 8 = 1 / 2 = .5 + 3 = 3.5

    3 4 5 2 * - +

    I come up with
    3 * 4 = 12 - 5 = 7 + 2 = 9

    Am I doing this right when talking of binary tree postfix expressions???

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    9 8 2 - 3 / +

    Actually, I believe to be totally postfix, it would be:

    9 8 2 3 / + -

    Which would be:

    9 - 8 = 1
    1 / 2 = .5
    .5 - 3 = -2.5

    But I could be wrong. I've personally never had use for postfix notations.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    The best way to do this is just look at it one operator at a time...
    First we just have some numbers...
    9 8 2
    then we get a '-', which operates on the last 2 values...
    9 (8, 2)-
    now, (8, 2)- is a value, that is, it represents 6. We could just replace (8, 2)- with 6 at this point, but I don't for the purpose of showing how postfix is changed to other forms.
    another number....
    9 (8, 2)- 3
    and an operator that will work on the last 2 values
    9 ((8, 2)-, 3)/
    and another operator that will work on the last 2 values...
    (9, ((8, 2)-, 3)/)+

    Now we just break it down into infix using the parenthesis...
    (9 + ((8 - 2) / 3))
    And that's how you analyze it.


    Or, just replacing the values, it would analyze like this...
    9 8 2 - == 9 6
    9 6 3 / == 9 2
    9 2 + == 11
    11 nothing left, so 11's the answer.
    Callou collei we'll code the way
    Of prime numbers and pings!

  4. #4
    Unregistered
    Guest
    so with your logic explained would the second expression i have

    3 4 5 2 * - +

    be.......


    3, 4 (5,2) *
    3,(4,(5,2)*)-
    (3,(4,(5,2)*)-)+
    (3 + (4 - (5*2))) = -3

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Exactly.
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expression: Convert infix notation to postfix notation.
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 02-27-2010, 07:44 AM
  2. evaluate postfix expressions read from infile
    By killmequick in forum C Programming
    Replies: 5
    Last Post: 10-02-2008, 02:19 PM
  3. Stack that evaluates postfix expressions
    By killmequick in forum C Programming
    Replies: 7
    Last Post: 10-01-2008, 07:23 PM
  4. Replies: 4
    Last Post: 03-12-2006, 02:17 PM
  5. Infix to Postfix
    By dat in forum C Programming
    Replies: 6
    Last Post: 06-16-2003, 09:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21