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

S

Sebastian Lipp

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
 
R

rudolf

Sebastian Lipp said:
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
 
J

Jens Thoms Toerring

Sebastian Lipp said:
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?

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
 
L

Lew Pitcher

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
Which is the top of the stack
Stack now has
0.0 (4 * <empty stack> --> 0.0)
0.5555555555555555556 (5.0 / 9.0 )
3
Which is the top of the stack
Stack now has
0.0 (4 * <empty stack> --> 0.0)
0.5555555555555555556 (5.0 / 9.0 )
0.5 (3.0 / 6.0 )
3.0
Which is the top of the stack
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
Which is the top of the stack
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
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.
 
G

Geoff

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
 
S

Sebastian Lipp

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...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top