Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   K&R, Sec. 4.3: error in calculator's source? (http://www.velocityreviews.com/forums/t754952-k-and-r-sec-4-3-error-in-calculators-source.html)

Sebastian Lipp 10-16-2011 09:24 PM

K&R, Sec. 4.3: error in calculator's source?
 
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

Peter Nilsson 10-16-2011 09:33 PM

Re: K&R, Sec. 4.3: error in calculator's source?
 
Sebastian Lipp <ba...@riseup.net> 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

rudolf 10-16-2011 09:39 PM

Re: K&R, Sec. 4.3: error in calculator's source?
 
In article <j7fi1i$eh7$1@dont-email.me>,
Sebastian Lipp <bacuh@riseup.net> 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

Jens Thoms Toerring 10-16-2011 09:47 PM

Re: K&R, Sec. 4.3: error in calculator's source?
 
Sebastian Lipp <bacuh@riseup.net> 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 ___ jt@toerring.de
\__________________________ http://toerring.de

Lew Pitcher 10-16-2011 10:05 PM

Re: K&R, Sec. 4.3: error in calculator's source?
 
On October 16, 2011 17:24, in comp.lang.c, bacuh@riseup.net 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. ------



Geoff 10-17-2011 01:57 AM

Re: K&R, Sec. 4.3: error in calculator's source?
 
On Sun, 16 Oct 2011 21:24:02 +0000 (UTC), Sebastian Lipp
<bacuh@riseup.net> 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

Sebastian Lipp 10-17-2011 03:59 PM

Re: K&R, Sec. 4.3: error in calculator's source?
 
On 2011-10-16, rudolf <r.thered@gmail.com> 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


All times are GMT. The time now is 09:30 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.