请教c程序设计语言书上一个例子

来源:百度知道 编辑:UC知道 时间:2024/05/26 04:31:10
int getop(char s[])
{
int i,c;

while((s[0] = c = getch()) == ' ' || c == '\t')
;
s[1]='\0';
if(!isdigit(c) && c != '.')
return c;
i=0;
if(isdigit(c))
while(isdigit(s[++i] = c = getch()))
;
s[i] = '\0';
if(c != EOF)
ungetch(c);
return number;
}

/*getch & getop*/

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

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

getch 和 ungetch 有什么用呢?书上说反读,为什么要反读,看不懂。

中译本68页,谢谢各位

ungetch主要是本次用不上的字符,再放回去,下次读取的时候接着使用。不然的话就会导致丢失了这个字符
if(c != EOF)
ungetch(c);
printf("\nNext char is %c",getch(););