eri*_*sse 0 c stack calculator polish
我不知道在哪里发布这个,但我想我在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;
}
Run Code Online (Sandbox Code Playgroud)
如果你想自己检查,我所做的就是添加
static int pass = 0;
int i, check;
i = check = 0;
Run Code Online (Sandbox Code Playgroud)
在main()和的while循环中
if(!check) {
printf("pass #%d\n",pass++);
while(val[i] != '\0') {
printf("val[%d]: %.2f\n",i,val[i]);
++i;
}
}
Run Code Online (Sandbox Code Playgroud)
在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;
}
}
Run Code Online (Sandbox Code Playgroud)
你有类似的东西
double pop(void)
{
if (sp > 0) {
double temp = val[--sp];
val[sp] = '\0';
return temp;
}
else {
printf("error: stack empty\n");
return 0.0;
}
}
Run Code Online (Sandbox Code Playgroud)
我还重写了push函数,以确保值始终被推送到val数组的末尾.而不是
void push(double f)
{
if ( sp < MAXVAL)
val[sp++] = f;
else
printf("error: stack full. can't push %g\n", f);
}
Run Code Online (Sandbox Code Playgroud)
你有
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);
}
Run Code Online (Sandbox Code Playgroud)
即使有了这些变化,我仍然需要重新编写
case '\n':
printf("\t%.8g\n", pop());
break;
Run Code Online (Sandbox Code Playgroud)
检索堆栈顶部的值而不弹出它,这需要用printf一个简单的函数替换语句
void print_top(void)
{
int i = 0;
while( val[i] != '\0' )
++i;
--i;
printf("\t%.8g\n",val[i]);
}
Run Code Online (Sandbox Code Playgroud)
只有这样,抛光计算器似乎按预期运行,至少就堆栈在幕后的作用而言.您可以使用修改后的代码自行尝试:
#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]);
}
Run Code Online (Sandbox Code Playgroud)
请注意,#define为了适应最后的调试printf语句,我必须将大部分语句和原型声明移到开头main().
*编辑了我的一些大胆的主张:P
您正在考虑向后堆栈 - 堆栈顶部是最高有效索引,而不是val[0].当你看到操作数的推动时,这种行为是显而易见的.你的输出:
3 4 +
pass #0
val[0]: 3.00
pass #1
val[0]: 3.00
val[1]: 4.00
Run Code Online (Sandbox Code Playgroud)
首先,3推送 - 进入(先前为空)堆栈的顶部 - 它位于插槽0中.接下来4被推送.正如您所看到的,它进入了val[1],清楚地表明val[0]在这种情况下它不是堆栈的顶部.
您错误地打印了堆栈,这让您感到困惑.将您的打印循环更改为:
while (i < sp) {
printf("val[%d]: %.2f\n",i,val[i]);
++i;
}
Run Code Online (Sandbox Code Playgroud)
也就是说,只打印堆栈中的有效条目,您将看到错误.
您当前的比较是0在堆栈上查找条目,而不是程序识别空闲条目的方式.这就是sp变量的用途.除了寻找错误的东西,它还是以一种奇怪的方式进行 - val是一个浮点数的数组 - 你为什么要比较一个字符文字\0?
这是完整的更正输出:
3 4 +
pass #0
val[0]: 3.00
pass #1
val[0]: 3.00
val[1]: 4.00
pass #2
val[0]: 7.00
7
Run Code Online (Sandbox Code Playgroud)
现在,您可以看到正确的输出-无论是3.00和4.00被弹出,并且7.00被推回压入堆栈.它现在是唯一有效的条目.