|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 JingHe1997 于 2021-8-6 18:15 编辑
看过答案以后依旧不太理解void ungetch(int)的作用,为什么没有 ungetch 函数,当遇到像 1 2 3-4++ 这种操作符之间不留空的式子就会出现计算错误。
附上源码(ungetch部分已标红):
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAXVAL 100 // 栈val的最大深度
void push(double); // 将f压入到值栈中
double pop(void); // 弹出并返回栈顶的值
int getch(void); // 从输入缓冲区取出一个字符
void ungetch(int) // 将字符送回缓冲区中
int getop(char s[]);
int sp = 0; // 栈指针,指向下一个空闲位置
double val[MAXVAL]; // 值栈
void push(double f)
{
if (sp < MAXVAL)
val[sp++] = f;
else
printf("错误:栈已满!\n", f);
}
double pop(void)
{
if (sp > 0)
return val[--sp];
else
{
printf("错误:栈已空!\n");
return 0.0;
}
}
#define BUFSIZE 100 // 缓冲区的最大尺寸
#define NUMBER '0'
char buf[BUFSIZE]; // 缓冲区
int bufp = 0; // 缓冲区指针,指向下一个空闲位置
int getch(void)
{
// 从从标准输入流中获取一个字符
// 如果buf缓冲区中有存在字符,先从buf中获取
return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c)
{
if (bufp >= BUFSIZE)
printf("错误:缓冲区已满!\n");
else
buf[bufp++] = c;
}
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()))
;
// 收集小数部分
if (c == '.')
while (isdigit(s[++i] = c = getch()))
;
s[i] = '\0';
if (c != EOF)
ungetch(c);
return NUMBER;
}
#define MAXOP 100
int main(void)
{
int type;
double op2;
char s[MAXOP];
while ((type = getop(s)) != EOF)
{
switch (type)
{
case NUMBER:
push(atof(s));
break;
case '+':
push(pop() + pop());
break;
case '*':
push(pop() * pop());
break;
case '-':
op2 = pop();
push(pop() - op2);
break;
case '/':
op2 = pop();
if (op2 != 0.0)
push(pop() / op2);
else
printf("error: zero divisor\n");
break;
case '\n':
printf("\t%.8g\n", pop());
break;
default:
printf("error: unknown comand %s\n", s);
break;
}
}
return 0;
}
有朋友能够帮忙解释一下吗,谢谢
- while (isdigit(s[++i] = c = getch()))
复制代码
循环终止时 :
getchar取了一个不是非数字字符(如操作符)
但其实是有用的,你得退回缓冲区留下一次使用
|
|