Ron Ford said:
What I did, Ben, was import the soln posted for 4-4 for the clc wiki. The
fella's name who gets the attribution there is Bob Wightman. I cut out
Getop and pasted in the Tondo and Gimpel version of Getop that mdh is
trying to implement.
I think I follow. I was confused in part by the fact that exercise
4-4 is an "extend..." exercise but Bob Wightman's solution changes
some of K&R's original code. For example he removes the test for
there being room to store the input. I wonder if Tondo & Gimpel do as
well.
Can you elaborate? I see 4 cases:
1) the posted solution for 4-4 is daft.
2) T&G's Getop is daft.
3) The inclusion of T&G's Getop in 4-4 is daft.
4) This portion of your criticism is daft.
I mean 2 with a bit of 3. Chapter 7 covers scanf and things like
ungetc. I don't know the exact contents and order since I don't have
K&R2, but the exercise is to: "re-write the postfix calculator of
Chapter 4 to use scanf and/or sscanf to do the input and number
conversion".
To do this by ignoring scanf's ability to read and covert numbers
seems daft. The posted code (which I was having trouble believing was
from a published book of solutions until this was confirmed) simply
re-writes getop to read characters using scanf rather than using
getchar (or K&R's invented getch). Now it is likely that it looks
particularly daft when T&G's getop is pasted into a solution of 4-4
that uses atof to convert a numeric string, because the result fails
to use scanf to do the number conversion as asked.
I am guessing, but surely T&G's version uses sscanf in main rather
than atof so that it meets the requirements of the exercise? Even so,
I don't think single character input is a good use of scanf -- It is
not what scanf is good at.
These will probably end up as spare parts.
Example solutions should be free from such left-over parts.
I ask about the author because this version is a step backwards.
K&R's getop passes in the buffer length. Now you can get by with
"global" (#define) size but that is not such a good design. Worse,
though, is that this code does not even check that there is room. It
is, in effect, a giant gets.
int Getop(char line[]) is a gemisch of a few things. K&R and T&G use getop
with a small g, which, according to gcc, differs from large G Getop.
Not only gcc. Case matters in C's identifiers.
mdh
used line[] where T&G used s[], but the substitution was consistent
throughout. I would prefer using s[], so as to stay closer to known
solutions.
The T&G soln for this only shows getop, and the first sentence of the
explanation is:
The function getop (page 78 K&R) is the only routine modified.
Curious. This means they *don't* use sscanf in main to do the
conversion. If so, in what sense is it even a solution to the
exercise?
This clears up one thing. If only getop is modified, it must have the
length parameter that the K&R version had. Is it simply ignored in
the body of the function?