getch(), ungetch(c), and EOF

getch(), ungetch(c), and EOF

Mate de Vita
Kelli

2008 Oct 4 • 2453
159 ₧
C code
#define NUMBER '0'
#define TOOBIG '9'

getop (s, lim)
char s[];
int lim;
{
int i, c;

while ((c = getch()) == ' ' || c == '\t' || c == '\n');
if (c != '.' && (c < '0' || c > '9')) return (c);

s[0] = c;

for (i = 1; (c = getchar()) >= '0' && c <= '9'; i++)
if (i < lim) s = c;

if (c == '.'){ //collect fraction
if (i < lim) s = c;
for (i++; (c = getchar()) >= '0' && c <= '9'; i++)
if (i < lim) s = c;
}

if (i < lim){ //number is ok
ungetch (c);
s = '\0';
return NUMBER;
}
else { //it's too big, skip the rest of the line
while (c != '\n' && c != EOF) c = getchar();
s[lim-1] = '\0';
return TOOBIG;
}
}


C code
#define BUFSIZE 100

char buf[BUFSIZE]; //buffer for ungetch
int bufp = 0; //pointer to the next free position in buf[]

int getch()
{
return ((bufp > 0) ? buf[--bufp] : getchar());
}

ungetch (c)
int c;
{
if (bufp > BUFSIZE) printf ("ungetch error: too many characters\n");
else buf[bufp++] = c;
}


Exercise 4-6: This getch and ungetch do not handle a pushed-back EOF in a portable way. Why?
Decide what their properties ought to be if an EOF is pushed back, then implement your design.

My problem is the first part of this exercise. Why is a pushed-back EOF not handled properly?
OK, if EOF is a negative constant, you probably have to change the char array to an int array, but other than that.

P.S. Those s = c; are supposed to be s[ i ] = c; (without the spaces)
For some reason they don't appear as such :/
...and that's the bottom line because Mate de Vita said so.
 
 
 
2010 Nov 1 at 04:30 PDT — Ed. 2010 Nov 1 at 04:33 PDT
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
Wait, what are you trying to do? It's been a while since I properly looked at C (not that that stopped me from trying to write some today and failing, of course).
 
 
 
2010 Nov 1 at 08:46 PDT
Mate de Vita
Kelli

2008 Oct 4 • 2453
159 ₧
This is taken directly from the book by which I'm learning C. It's a part of a Reverse Polish Notation Calculator.

The getop() gets the next operand - it transforms a string representing a number into a number and returns it, or (if the operand is not numerical, such as +, -, a, b, etc.) returns the operand.

getch() and ungetch(c) are two functions that are used because getop() can't know it's read enough until it's read one (or possibly more) character too many. That's why the last read character (or more if it's possible) has to be pushed back to the input, so it can be read again.

Something like that.
...and that's the bottom line because Mate de Vita said so.
 
 
 
2010 Nov 1 at 09:52 PDT — Ed. 2010 Nov 1 at 09:52 PDT
sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
Mate de Vita said:
It's a part of a Reverse Polish Notation Calculator.

That's right, those Polishess's people always think they're smart. Inventing fancy computin' machines.

wtf!?!?! Quote's fucked up.... :S
 
 
 
2010 Nov 1 at 10:08 PDT — Ed. 2010 Nov 1 at 10:09 PDT
phoenix_r

2009 May 13 • 905
17 ₧
While I don't have an answer at current, assuming this is K&R it's actually exercise 4-9, not 4-6. I'm not quite that far in but I'll post again once I get there if I have anything useful to add.
Edit: While, as far as I can tell this is no longer maintained and it does not have a solution for 4-9, you should check http://users.powernet.co.uk/eton/kandr2/ out if you haven't already, it's got solutions for 79 out of the 97 problems in the book. I recommend reading each solution *after* you've solved the problem to see how others have done it. Of course, if you're stuck, you can always take a look *while* solving for inspiration but I'd say hold out as long as you can and really try to solve the problem before you peek.
BOO
 
 
 
2010 Nov 3 at 14:54 PDT — Ed. 2010 Nov 3 at 15:01 PDT
Page [1]