How to use the Polish calculator from kr2 section 4.3, page 74.

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

Ben Bacarisse

matt said:
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.
I thought maybe I had totally misunderstood how the program is
intended to work so:

You've misunderstood but "totally" seems a bit harsh on yourself:

$ gcc -o wikicalc wikicalc.c
$ ./wikicalc
2 2+
4

<snip>
 
K

Kenny McCormack

Ben Bacarisse said:
You've misunderstood but "totally" seems a bit harsh on yourself:

$ gcc -o wikicalc wikicalc.c
$ ./wikicalc
2 2+
4

So, it seems it has to be all on one line. Acceptable, I suppose, but a
little odd. I wonder why it has that limitation.

Anyway, I got the code and compiled it - but don't have time (read:
sufficient interest) to dig into the code and fix it.

--
"I heard somebody say, 'Where's Nelson Mandela?' Well,
Mandela's dead. Because Saddam killed all the Mandelas."

George W. Bush, on the former South African president who
is still very much alive, Sept. 20, 2007
 
M

matt

You've misunderstood but "totally" seems a bit harsh on yourself:

$ gcc -o wikicalc wikicalc.c
$ ./wikicalc
2 2+
4

<snip>
Thanks. Looking more closely at getop() I see how this works now. If I'd
paid more attention to the whitespace in the line on page 74 under "is
entered as" ... Oh well.

Interesting that the characters after the first '2' are still "there" to
be gotchar'ed on the subsequent trips through getop(). But where is
"there". Doesn't seem to be in our program. Almost like there is
something like:

{'2', ' ', '2', '+', '\n'}

which is somehow persistent. I guess this a system thing not a C thing
though so offtopic.

Like Steve Summit says in his notes this one is worth geting your head
around.

Matt.
 
B

Barry Schwarz

Thanks. Looking more closely at getop() I see how this works now. If I'd
paid more attention to the whitespace in the line on page 74 under "is
entered as" ... Oh well.

Interesting that the characters after the first '2' are still "there" to
be gotchar'ed on the subsequent trips through getop(). But where is
"there". Doesn't seem to be in our program. Almost like there is
something like:

The "there" is the buffer associated with stdin. getop() calls
getch(). getch() extracts the next character from the buffer. On
most desktop systems, data you type is not made available to getch()
and its cousins until you press Enter. The net result is the buffer
contains exactly what you posted below (less the braces).
{'2', ' ', '2', '+', '\n'}

After the first call to getch(), s[0] and c both contain '2' and the
buffer now contains ' ', '2', '+', '\n'. After the next call, s[1]
and c both contain ' ' and the buffer contains '2', '+', '\n'. At
that point the code sets s[1] to '\0' (thus making s a string), call
ungetch() to restore the ' ' to the beginning of the buffer, and
returns NUMBER.

After processing the contents of s, main will again call getop().
getop() will call getch() twice, once to skip the ' ' and again to get
the '2'. The subsequent processing is identical to the above except
the character in s[1] and c is '+'. When getop returns to main, the
buffer contains '+' and '\n'.

You should be able to follow the subsequent processing with these
hints.
 
M

matt

On 11/12/13 15:35, Barry Schwarz wrote:

You should be able to follow the subsequent processing with these
hints.

Thanks for your time. C.l.c is really helping me learn.

Matt.
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top