gpt4 book ai didi

c - 关于后缀计算器中第二个堆栈的谜题

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:37:12 26 4
gpt4 key购买 nike

最近在看《TCPL》,4.3节有一个经典案例“后缀计算器”。我有一个关于这个案子的问题:案例代码见(从书上抄的):

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

#define MAXOP 100
#define NUMBER '0'
#define MAXVAL 100 /* maxmium depth of val stack */
#define BUFFSIZE 100


int sp = 0;
double val[MAXVAL];

int bufp = 0; /* next free size in buf*/
char buf[BUFFSIZE]; /* buffer in ungetch */


int getch(void);
void ungetch(int);
int getop(char []);
void push(double);
double pop(void);

int main()
{
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;
default:
printf("\t%.8g\n", pop());
break;
}
}
return 0;
}


/* push: push value f onto value stack */
void push(double f)
{
if (sp < MAXVAL) {
val[sp++] = f;
}
else {
printf("error: stack full, can't push.\n");
}
}


/* pop: pop and return value on the top of the stack */
double pop(void)
{
if (sp > 0) {
return val[--sp];
}
else {
printf("error: stack empty.\n");
return 0.0;
}
}



/* getop: get next character or numeric operand */
int getop(char s[])
{
int i, c;

while ((s[0] = c = getch()) == ' ' || c == '\t') {
;
}
s[1] = '\0';
if (!isdigit(c) && c != '.' ) { /* operator */
return c;
}
i = 0;
if (isdigit(c)) { /* collect integer part */
while (isdigit(s[++i] = c = getch())) {
;
}
}
if (c == '.') { /* collect fraction part */
while (isdigit(s[++i] = c = getch())) {
;
}
}
s[i] = '\0';
if (c != EOF) {
ungetch(c);
}
return NUMBER;
}


/* getch: get a (possibly pushed-back) character */
int getch(void)
{
return (bufp > 0)? buf[--bufp] : getchar();
}


/* ungetch: push character back on input */
void ungetch(int c)
{
if (bufp >= BUFFSIZE) {
printf("ungetch: too many characters in buufer.\n");
}
else {
buf[bufp++] = c;
}
}

有一个全局的字符数组buf,它就像一个栈,最后两个函数是在buf上'pop'和'push'。

我的问题是在什么情况下堆栈 buf 包含多个元素?

调用 ungetch() 的唯一机会是在函数 getop() 中,但是在 getop() 中函数 getch() 至少被调用一次。

最佳答案

My question is in which case does the stack buf contains more than one element?

从来没有,buf可以只是一个字符,它只是用来记住紧跟在数字后面的字符,以免丢失

代码可以修改为不使用它:

/* getch:  get a (possibly pushed-back) character */
int getch(void)
{
return getchar();
}


/* ungetch: push character back on input */
void ungetch(int c)
{
ungetc(c, stdin);
}

编译和执行:

pi@raspberrypi:/tmp $ gcc pf.c
pi@raspberrypi:/tmp $ ./a.out
1 2 3 4 + + +
10

或者使用只能保存一个字符的事实:

int SavedChar = EOF;

/* getch: get a (possibly pushed-back) character */
int getch(void)
{
if (SavedChar == EOF)
return getchar();

int r = SavedChar;

SavedChar = EOF;

return r;
}

/* ungetch: push character back on input */
void ungetch(int c)
{
SavedChar = c;
}

编译和执行:

pi@raspberrypi:/tmp $ gcc pf.c
pi@raspberrypi:/tmp $ ./a.out
1 2 3 4 + + +
10

关于c - 关于后缀计算器中第二个堆栈的谜题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55879518/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com