请问如何用static来修改getop函数,使getop函数不使用ungetch函数呢?

来源:百度知道 编辑:UC知道 时间:2024/06/14 22:08:48
int getch(void);
void ungetch(int);

//getop函数,获取下一个运算符或数值操作数
int getop(char s[])
{
int i,c;

while((s[0]=c=getch())==' '|| c=='\t ')
; // 忽略空格和TAB.
s[1]=' \0';

if(!isdigit(c) && c != '.')
return c;

i=0;
if(isdigit(c))
while(isdigit(s[++i]=c=getch()))
;
if(c=='.') //收集小数部分
while(isdigit(s[++i]=c=getch()))
;
s[i]='\0';
if(c!=EOF)
ungetch(c); /*将c存入缓冲区中*/
return NUMBER;
}

大虾们帮帮忙, 请问如何用 static类型 来修改getop函数,使getop函数不使用 ungetch 函数呢?谢谢!!

要使getop函数不使用 ungetch 函数,可先声明一个常量:
static int *c1=&EOF;
然后在getop函数倒数三行的
if(c!=EOF)
前加上
&c=&c1;
就可以了.
这样,c的内容永远是EOF;则if(c!=EOF)的参数永远是false,那么里面的
ungetch(c); /*将c存入缓冲区中*/
将无法得到执行.