Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to evaluate this postfix evaluation?

 
Nadine Ernest
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


ive tried using the stack in evaluating this but it didnt work with this expression coz of the last - *


check it out:






the answer is 6!!


i just dont know how!!
please help make this simple to me....!!

thanks in advance
 
Campbell Ritchie
Sheriff
Pie
Posts: 48955
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is the sort of postfix expression where each character is a token in its own right. It is difficult to understand until you separate it to

2 3 4 5 + * 2 4 - *

Going from left to right it means
  • push 2
  • push 3
  • push 4
  • push 5
  • add (4 + 5 = 9) so the 4 and 5 come off the stack and 9 is pushed. Stack now 2 3 9
  • Multiply 3 * 9, taking 3 9 off the stack and pushing 27. Stack now 2 27
  • Push 2
  • push 4
  • subtract, taking 2 4 off the stack and pushing -2 back: stack = 2 27 -2
  • Multiply, taking 27 -2 off the stack and pushing -54


  • Stack is now 2 -54.

    In a minute or two I shall try it in Forth and show the Forth stack traces, so you can see it still produces -54
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 48955
    60
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    UoT Formal Methods and Programming Research Group
    Reversible Forth vsn 0.11 built on linux-pgix
    Wed Nov 5 11:50:21 GMT 2008
    ok
    2 3 4 5 .S stack=2 3 4 5
    ok....
    + .S stack=2 3 9
    ok...
    * .S stack=2 27
    ok..
    2 4 .S stack=2 27 2 4
    ok....
    - .S stack=2 27 -2
    ok...
    * .S stack=2 -54
    ok..
    Not 6. What did you expect?

    The dots... after ok show how many values there are on the stack, and .S prints the stack.

    Please check whether you have copied the expression exactly from its source. And please quote your source, if you have a link or reference.

    Another try:
    2 3 4 5 + * 2 4 - * .S stack=2 -54
    ok..
     
    Nadine Ernest
    Greenhorn
    Posts: 27
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No actually the right answer is 6!!

    so heres how you're supposed to do it:

    you push the 2 3 4 5 into the stack so now the stack looks like this:

    5
    4
    3
    2

    and so you + the first two wich is 5+4 = 9. and now the stack looks like this

    9
    3
    2

    you do this 9*3 which is 27
    and now the stack looks like this:

    27
    2

    and now you add 24 and the stack will look like this:

    24
    27
    2

    and now you do the - operation
    which will be 27-24=3

    and now the stack looks like this

    3
    2

    and now you perform the * operation and so it will give you 3*2 which is the 6 and now the stack looks like this

    6


    tell me campbell ritchie...do you agree with me?!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic