K&R, Sec. 4.3: error in calculator's source?

Discussion in 'C Programming' started by Sebastian Lipp, Oct 16, 2011.

  1. Hey There!

    I'm trying to get the reverse Polish calculator of the 2nd edition of
    The C Programming Language (German translation) working. But something
    doesn't work.

    The first and second try were a type-out directly from the book. The
    third try was fetching the code from [1]. It compiles fine but no matter
    what the program complains about an empty stack when calculating the first
    result and returns the last given operand:

    4*5
    error: stack empty
    5
    9/3
    3
    6/3
    3
    100-20
    20
    2+3+5
    5

    What's wrong here? Is there a error in the book or a change in the standard
    library since 1990 that's relevant here?

    [1]: http://users.powernet.co.uk/eton/kandr2/krx400.html

    --
    basti
     
    Sebastian Lipp, Oct 16, 2011
    #1
    1. Advertising

  2. Sebastian Lipp <> wrote:
    > I'm trying to get the reverse Polish calculator of the 2nd
    > edition of The C Programming Language (German translation)
    > working. But something doesn't work.

    ....
    >    4*5
    >   error: stack empty


    RPN for 4 * 5 is 4 5 *

    <http://en.wikipedia.org/wiki/Reverse_Polish_notation>

    --
    Peter
     
    Peter Nilsson, Oct 16, 2011
    #2
    1. Advertising

  3. Sebastian Lipp

    rudolf Guest

    In article <j7fi1i$eh7$>,
    Sebastian Lipp <> wrote:

    > Hey There!
    >
    > I'm trying to get the reverse Polish calculator of the 2nd edition of
    > The C Programming Language (German translation) working. But something
    > doesn't work.
    >
    > The first and second try were a type-out directly from the book. The
    > third try was fetching the code from [1]. It compiles fine but no matter
    > what the program complains about an empty stack when calculating the first
    > result and returns the last given operand:
    >
    > 4*5
    > error: stack empty
    > 5
    > 9/3
    > 3
    > 6/3
    > 3
    > 100-20
    > 20
    > 2+3+5
    > 5
    >
    > What's wrong here? Is there a error in the book or a change in the standard
    > library since 1990 that's relevant here?
    >
    > [1]: http://users.powernet.co.uk/eton/kandr2/krx400.html


    I don't know if there are any bugs in the source, but you are not
    entering the equations in RPN format.

    To multiply 4 by 5, you would enter 45* not 4*5

    http://en.wikipedia.org/wiki/Reverse_Polish_notation
     
    rudolf, Oct 16, 2011
    #3
  4. Sebastian Lipp <> wrote:
    > Hey There!


    > I'm trying to get the reverse Polish calculator of the 2nd edition of
    > The C Programming Language (German translation) working. But something
    > doesn't work.


    > The first and second try were a type-out directly from the book. The
    > third try was fetching the code from [1]. It compiles fine but no matter
    > what the program complains about an empty stack when calculating the first
    > result and returns the last given operand:


    > 4*5
    > error: stack empty
    > 5
    > 9/3
    > 3
    > 6/3
    > 3
    > 100-20
    > 20
    > 2+3+5
    > 5


    > What's wrong here? Is there a error in the book or a change in the standard
    > library since 1990 that's relevant here?


    > [1]: http://users.powernet.co.uk/eton/kandr2/krx400.html


    You seem to misunderstand what "Reverse Polish Notation" is
    (especially the "reverse" part;-) If you want to multiply 4
    by 5 use

    4 5 *

    That pushes 4 on the stack, then 5, and then multiplies the two
    items at the top of the stack, leaving the result at the top ele-
    ment on the stack. And for 9 divided by 3 use

    9 3 /

    For 100 minus 20 use

    100 20 -

    and for 2+3+5 do either

    2 3 + 5 + (adds 2 and 3 and then 5 to the result)

    or

    2 3 5 + + (adds 3 and 5 and then the the result to 2)

    etc. If you try

    4 * 5

    the the program will try to multiply 4 with whatever already
    is on the stack, then pushing 5. This, of course won't work
    if there's nothing on the stack, thus the error message you
    get about an empty stack.

    So the program is probably fine, you're just feeding it input
    that isn't what it requires.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Oct 16, 2011
    #4
  5. Sebastian Lipp

    Lew Pitcher Guest

    On October 16, 2011 17:24, in comp.lang.c, wrote:

    > Hey There!
    >
    > I'm trying to get the reverse Polish calculator of the 2nd edition of
    > The C Programming Language (German translation) working. But something
    > doesn't work.
    >
    > The first and second try were a type-out directly from the book. The
    > third try was fetching the code from [1]. It compiles fine but no matter
    > what the program complains about an empty stack when calculating the first
    > result and returns the last given operand:
    >
    > 4*5
    > error: stack empty

    Stack now has
    0.0 (4 * <empty stack> --> 0.0)
    5.0

    > 5

    Which is the top of the stack

    > 9/3

    Stack now has
    0.0 (4 * <empty stack> --> 0.0)
    0.5555555555555555556 (5.0 / 9.0 )
    3

    > 3

    Which is the top of the stack

    > 6/3

    Stack now has
    0.0 (4 * <empty stack> --> 0.0)
    0.5555555555555555556 (5.0 / 9.0 )
    0.5 (3.0 / 6.0 )
    3.0

    > 3

    Which is the top of the stack

    > 100-20

    Stack now has
    0.0 (4 * <empty stack> --> 0.0)
    0.5555555555555555556 (5.0 / 9.0 )
    0.5 (3.0 / 6.0 )
    -97.0 (3.0 - 100.0 )
    20

    > 20

    Which is the top of the stack

    > 2+3+5

    Stack now has
    0.0 (4 * <empty stack> --> 0.0)
    0.5555555555555555556 (5.0 / 9.0 )
    0.5 (3.0 / 6.0 )
    -97.0 (3.0 - 100.0 )
    25.0 (20.0 + 2.0 + 3.0)
    5.0

    > 5

    Which is the top of the stack

    >
    > What's wrong here?


    You don't understand how to use Reverse Polish Notation

    > Is there a error in the book or a change in the
    > standard library since 1990 that's relevant here?


    Neither.

    --
    Lew Pitcher
    Master Codewright & JOAT-in-training | Registered Linux User #112576
    Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
    ---------- Slackware - Because I know what I'm doing. ------
     
    Lew Pitcher, Oct 16, 2011
    #5
  6. Sebastian Lipp

    Geoff Guest

    On Sun, 16 Oct 2011 21:24:02 +0000 (UTC), Sebastian Lipp
    <> wrote:

    >Hey There!
    >
    >I'm trying to get the reverse Polish calculator of the 2nd edition of
    >The C Programming Language (German translation) working. But something
    >doesn't work.
    >
    >The first and second try were a type-out directly from the book. The
    >third try was fetching the code from [1]. It compiles fine but no matter
    >what the program complains about an empty stack when calculating the first
    >result and returns the last given operand:
    >
    > 4*5
    > error: stack empty
    > 5
    > 9/3
    > 3
    > 6/3
    > 3
    > 100-20
    > 20
    > 2+3+5
    > 5
    >
    >What's wrong here? Is there a error in the book or a change in the standard
    >library since 1990 that's relevant here?
    >
    >[1]: http://users.powernet.co.uk/eton/kandr2/krx400.html


    Your run result should be:

    4 5 *
    20
    9 3 /
    3
    6 3 /
    2
    100 20 -
    80
    2 3 5 + +
    10
     
    Geoff, Oct 17, 2011
    #6
  7. On 2011-10-16, rudolf <> wrote:
    > I don't know if there are any bugs in the source, but you are not
    > entering the equations in RPN format.


    WTF! How can I be this stupid?!

    Seems I forgot about that on some point between typing out and
    testing...

    --
    basti
     
    Sebastian Lipp, Oct 17, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. happyinst
    Replies:
    1
    Views:
    462
    happyinst
    Jul 10, 2003
  2. =?Utf-8?B?bWZlaW5nb2xk?=
    Replies:
    7
    Views:
    839
    =?Utf-8?B?bWZlaW5nb2xk?=
    Nov 8, 2004
  3. yurps
    Replies:
    0
    Views:
    461
    yurps
    Jun 2, 2005
  4. The DUDE
    Replies:
    3
    Views:
    583
    code learner
    Dec 24, 2010
  5. Robocop
    Replies:
    3
    Views:
    7,074
    Sean Berry
    Feb 26, 2004
Loading...

Share This Page