M
matt
I don't understand how to use the Polish calculator from kr2 section
4.3, page 74. Using the code from here:
http://clc-wiki.net/wiki/K&R2_solutions:Ancillary:Polish_calculator
pasted to the file wikicalc.c, and using the test case of adding 2 + 2.
mds@waitaki:~$ gcc --version
gcc (Debian 4.7.2-5) 4.7.2
mds@waitaki:~$ gcc wikicalc.c
mds@waitaki:~$
mds@waitaki:~$ ./a.out
2
2
2
2
+
error: stack empty
error: stack empty
0
I thought maybe I had totally misunderstood how the program is intended
to work so:
mds@waitaki:~$ ./a.out
22+
error: stack empty
22
Last night I typed the program myself in 4 source files as per the
diagram on page 82 and built it with GNU make. When I couldn't get it to
work I switched to using the code from the wiki to see if maybe I had
made an error of some sort but I have the same problem.
I spent some time last night trying to see what was going on here and at
one point I was able to see that 4 was getting pushed on to the stack
but I was never able to have it put to stdout.
To my way of thinking the program as implemented doesn't quite match the
pseudocode at the top of page 75. For example where it says
if (number)
push it
well what you get imo is:
if (number)
push it
call pop()
because to have execution proceed after you type 2 you have to type
enter. This triggers case '\n' in the switch statement in main(), which
includes the call to pop() as a parameter(arg?) to printf() so for every
push operation you get an immediate pop which seems to run counter to
how the program needs to work.
I must be missing something basic here. I'm prepared for total
humiliation - hopefully I'll learn something.
Matt.
4.3, page 74. Using the code from here:
http://clc-wiki.net/wiki/K&R2_solutions:Ancillary:Polish_calculator
pasted to the file wikicalc.c, and using the test case of adding 2 + 2.
--cut here--<
mds@waitaki:~$ gcc --version
gcc (Debian 4.7.2-5) 4.7.2
mds@waitaki:~$ gcc wikicalc.c
mds@waitaki:~$
mds@waitaki:~$ ./a.out
2
2
2
2
+
error: stack empty
error: stack empty
0
--cut here--<
I thought maybe I had totally misunderstood how the program is intended
to work so:
--cut here--<
mds@waitaki:~$ ./a.out
22+
error: stack empty
22
--cut here--<
Last night I typed the program myself in 4 source files as per the
diagram on page 82 and built it with GNU make. When I couldn't get it to
work I switched to using the code from the wiki to see if maybe I had
made an error of some sort but I have the same problem.
I spent some time last night trying to see what was going on here and at
one point I was able to see that 4 was getting pushed on to the stack
but I was never able to have it put to stdout.
To my way of thinking the program as implemented doesn't quite match the
pseudocode at the top of page 75. For example where it says
if (number)
push it
well what you get imo is:
if (number)
push it
call pop()
because to have execution proceed after you type 2 you have to type
enter. This triggers case '\n' in the switch statement in main(), which
includes the call to pop() as a parameter(arg?) to printf() so for every
push operation you get an immediate pop which seems to run counter to
how the program needs to work.
I must be missing something basic here. I'm prepared for total
humiliation - hopefully I'll learn something.
Matt.