Help with understanding K&R exercises [closed]
I am reading the book by kerninghan Ritchie Edition 2. It contains a code function on getch()
, ungetch()
, getop()
, etc. I am not able to understand the stuff and I feel that it is bit incomplete also. Therefore, I am posting the question here.
Not able to understand... what getch(), ungetch() functions are doing. Please give few examples to demonstrate these functions.
At the end. these getch() and ungetch(0 functions are used by getOp(). What does this function doing. Only examples willl do. No code explanations reqd.That i will try and manage myself. If i have some sample examples.
Please give few examples to make this code look pretty easier; if there is any correction in the programm, then please let me know.
Thanks
#include <stdio.h>
#include <ctype.h>
#define BUFSIZE 100
char buf[BUFSIZE]; /* buffer for ungetch */
int bufp = 0; /* next free position in buf */
int getch(void) /* get a (possibly pushed-back) character */
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c) /* push character back on input */
{
if(bufp >= BUFSIZE)
printf(" ungetch too many characters\n");
else
buf[bufp++] = c;
}
/* getop: get next character or numeric operand */
int getop(char s[])
{
int i, c;
while ((s[0] = c = getch()) == ' ' || c ==开发者_如何学JAVA '\t');
s[1] = '\0';
if(!isdigit(c) && c != '.')
return c; /* not a number */
i=0;
if(isdigit(c)) /* collect integer part */
while(isdigit(s[++i] = c = getch()));
if(c == '.') /* collect fractional part */
while(isdigit(s[++i] = c = getch()));
s[i] = '\0';
if (c != EOF)
ungetch(c);
}
You have a typo here:
while(isdigit([s++i] = c = getch()));
Change it to:
while(isdigit(s[++i] = c = getch()));
At first glance, I thought it was incomplete as well, but then I noticed the following key line:
return (bufp > 0) ? buf[--bufp] : getchar();
On the off chance you weren't aware, the a ? b : c syntax is a ternary statement meaning "if a is true, execute b, else execute c". In this case, what happens is that, if bufp
, the count of characters in the array buf
, is greater than 0, getch()
will decrement bufp
and return the last character in the buffer (meaning the buffer actually acts as a stack, since ungetch()
adds characters to the tail of the buffer rather than the head). If there are no characters in buf
(i.e. bufp
<= 0), the function calls getchar()
and returns the value obtained from that. Aside from that, I don't see where your confusion could be coming from, unless your problem really does stem from that typo that Paul R mentioned and everything before this sentence is pointless.
getchar() reads and eliminates from stdin. What you read is lost iff you are not keeping it. this is where the pop and push functions getch() and ungetch() com into play. in getop you have a small parser with a look-ahead of 1 (one character in advance).
getop()
- skip blanks and tabs
- if c is no digit and no dot, return this character
- read over numbers and dot and more numbers
- if read character is not a number, push it back and leave function
精彩评论