我不知道在哪里发布这个,但我想我在 K&R 的波兰计算器程序中发现了一个相当大的错误。基本上,当您执行操作时,会弹出两个值,而只推送结果。问题是结果没有被推到栈顶!这是一个插图:
教科书提供的波兰语计算器的完整代码如下所示:
#include <stdio.h>
#include <stdlib.h> /* for atof() */
#define MAXOP 100 /* max size of operand or operator */
#define NUMBER '0' /* signal that a number was found */
int getop(char []);
void push(double);
double pop(void);
/* reverse Polish calculator */
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;
case '\n':
printf("\t%.8g\n", pop());
break;
default:
printf("error: unknown command %s\n", s);
break;
}
}
system("Pause");
return 0;
}
#define MAXVAL 100 /* maximum depth of val stack */
int sp = 0; /* next free stack position */
double val[MAXVAL]; /* value stack */
/* push: push f onto value stack */
void push(double f)
{
if ( sp < MAXVAL)
val[sp++] = f;
else
printf("error: stack full. can't push %g\n", f);
}
/* pop: pop and return top value from stack */
double pop(void)
{
if (sp > 0)
return val[--sp];
else {
printf("error: stack empty\n");
return 0.0;
}
}
#include <ctype.h>
int getch(void);
void ungetch(int);
/* getop: get next operator or numeric operand */
int getop(char s[])
{
int i, c;
while ((s[0] = c = getch()) == ' ' || c == '\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 fraction part*/
while (isdigit(s[++i] = c = getch()))
;
s[i] = '\0';
if (c != EOF)
ungetch(c);
return NUMBER;
}
#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;
}
如果您想自己检查,我所做的只是添加
static int pass = 0;
int i, check;
i = check = 0;
在 main() 的 while 循环内和
if(!check) {
printf("pass #%d\n",pass++);
while(val[i] != '\0') {
printf("val[%d]: %.2f\n",i,val[i]);
++i;
}
}
在 while 循环结束时,就在 switch 语句之后。我也把check = 1;
这个案子放进去'\n'
。
作为一种可能的解决方法,我重新编写了 pop 函数,以便在访问弹出值时立即从 val 数组中删除它们。所以而不是
double pop(void)
{
if (sp > 0)
return val[--sp];
else {
printf("error: stack empty\n");
return 0.0;
}
}
你会有类似的东西
double pop(void)
{
if (sp > 0) {
double temp = val[--sp];
val[sp] = '\0';
return temp;
}
else {
printf("error: stack empty\n");
return 0.0;
}
}
我还重新编写了 push 函数,以确保始终将值推送到 val 数组的末尾。所以而不是
void push(double f)
{
if ( sp < MAXVAL)
val[sp++] = f;
else
printf("error: stack full. can't push %g\n", f);
}
你会有
void push(double f)
{
if ( sp < MAXVAL) {
while (val[sp] != '\0')
++sp;
val[sp++] = f;
}
else
printf("error: stack full. can't push %g\n", f);
}
即使有这些变化,我仍然不得不重新编写
case '\n':
printf("\t%.8g\n", pop());
break;
检索堆栈顶部的值而不弹出它,这需要用printf
一个简单的函数替换语句,如
void print_top(void)
{
int i = 0;
while( val[i] != '\0' )
++i;
--i;
printf("\t%.8g\n",val[i]);
}
只有这样,波兰语计算器才能按预期运行,至少就堆栈在幕后所做的事情而言。您可以使用修改后的代码自己尝试一下:
#include <stdio.h>
#include <stdlib.h> /* for atof() */
#include <ctype.h>
#define MAXOP 100 /* max size of operand or operator */
#define NUMBER '0' /* signal that a number was found */
#define MAXVAL 100 /* maximum depth of val stack */
int getop(char []);
void push(double);
double pop(void);
void print_top(void);
int sp = 0; /* next free stack position */
double val[MAXVAL]; /* value stack */
/* reverse Polish calculator */
main()
{
int type;
double op2;
char s[MAXOP];
while ((type= getop(s)) != EOF) {
static int pass = 0;
int i, check;
i = check = 0;
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':
print_top();
check = 1;
break;
default:
printf("error: unknown command %s\n", s);
break;
}
if(!check) {
printf("pass #%d\n",pass++);
while(val[i] != '\0') {
printf("val[%d]: %.2f\n",i,val[i]);
++i;
}
}
}
system("Pause");
return 0;
}
/* push: push f onto value stack */
void push(double f)
{
if ( sp < MAXVAL) {
while (val[sp] != '\0')
++sp;
val[sp++] = f;
}
else
printf("error: stack full. can't push %g\n", f);
}
/* pop: pop and return top value from stack */
double pop(void)
{
if (sp > 0) {
double temp = val[--sp];
val[sp] = '\0';
return temp;
}
else {
printf("error: stack empty\n");
return 0.0;
}
}
int getch(void);
void ungetch(int);
/* getop: get next operator or numeric operand */
int getop(char s[])
{
int i, c;
while ((s[0] = c = getch()) == ' ' || c == '\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 fraction part*/
while (isdigit(s[++i] = c = getch()))
;
s[i] = '\0';
if (c != EOF)
ungetch(c);
return NUMBER;
}
#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;
}
void print_top(void)
{
int i = 0;
while( val[i] != '\0' )
++i;
--i;
printf("\t%.8g\n",val[i]);
}
请注意,我必须将大部分#define
语句和原型声明移到开头,以printf
适应main()
.
*编辑了我的一些大胆的主张:P