C 语言 逆波兰计算机

coolite 2009-02-22 03:56:47
为什么不管我怎么输入都是提示错误啊 是程序错了么?

#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;
}
}
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 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 != '.')
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;
}



...全文
126 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
coolite 2009-02-24
  • 打赏
  • 举报
回复
我要说什么好呢? 我真的没话说了
我只能说句!!!!!!!我真的超级无敌宇宙感谢你
因为这个问题 我一问 别人都让我看树的遍历
我树的遍历看过了
程序我捣鼓了半天设置断点也不会找毛病
真的很感谢您!!!
你让我释放了,我苦了三天,没人甩我
没知识真的很让人瞧不起,我会多多在这论坛上好好像你们学习!谢谢了
ltc_mouse 2009-02-23
  • 打赏
  • 举报
回复
1. getch是不回显的,即你无法看到输入的内容,不利于调试,可换成getche
2. error:unkonwn commad的原因在于得到了不识别的字符了,如Ctrl+Z,我在VS2003下测试,发现回车也这样.因为'\r','\n'的问题
case '\n':
printf("\t%.8g\n", pop());
break;
可以修改成:
case '\r':
case '\n':
printf("\n%.8g\n", pop()); ///输出前先换行下,否则会将表达式覆盖了
break;

3. 输入的运算式采用逆波兰式
coolite 2009-02-23
  • 打赏
  • 举报
回复
高手赶紧帮帮我吧 没方向。了
coolite 2009-02-23
  • 打赏
  • 举报
回复
高手求救啊
我是这么理解的
比如说我运行这个程序
我想算(1-2)*(4-5)
我就输入 1 这个字符将被存入s[0] 然后我按CRTL+Z输入结束符 结果DOS里面就显示: error:unkonwn commad
这时候1被压入到栈中 val[0]=1.0
然后我再将2压入
再输入+可是不出结果啊
这样每次都是提示错误啊

我想知道如何输入,这样我能更容易理解点,能帮我解决下么?到底该如何使用这个程序。
coolite 2009-02-23
  • 打赏
  • 举报
回复
高手求救啊
我是这么理解的
比如说我运行这个程序
我想算(1-2)*(4-5)
我就输入 1 这个字符将被存入s[0] 然后我按CRTL+Z输入结束符 结果DOS里面就显示: error:unkonwn commad
这时候1被压入到栈中 val[0]=1.0
然后我再将2压入
再输入+可是不出结果啊
这样每次都是提示错误啊

我想知道如何输入,这样我能更容易理解点,能帮我解决下么?到底该如何使用这个程序。
waizqfor 2009-02-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 coolite 的回复:]
我在网上找了个C的逆波兰计算器源码
但是不会用
比如我想算(1-2)*(4-5)吧
该如何输入呢
不管是是输入1 2 - 45 -*回车
还是(1-2)*(4-5)回车都提示错误
这个程序到底怎么用呢?
我到底怎么输入才能使用这个程序啊
应该怎么个输入方法 ~~我想要使用方法。。。谢谢了 大家帮帮我吧
我3天了 想了半天 也没想明白
[/Quote]
http://www.linuxdiyf.com/blog/?111420/action_viewspace_itemid_3513.htmlLZ配合程序看看原理
coolite 2009-02-23
  • 打赏
  • 举报
回复
我在网上找了个C的逆波兰计算器源码
但是不会用
比如我想算(1-2)*(4-5)吧
该如何输入呢
不管是是输入1 2 - 45 -*回车
还是(1-2)*(4-5)回车都提示错误
这个程序到底怎么用呢?
我到底怎么输入才能使用这个程序啊
应该怎么个输入方法 ~~我想要使用方法。。。谢谢了 大家帮帮我吧
我3天了 想了半天 也没想明白
  • 打赏
  • 举报
回复
现在没编译器,先mark下。
waizqfor 2009-02-22
  • 打赏
  • 举报
回复
给LZ一个参考一下

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAXOP 100
#define NUMBER '0'
int getop (char [] );
void push (double);
double pop(void);

int getch(void);
void ungetch(int);

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 BUFSIZE 100
char buf[BUFSIZE];
int bufp=0;
int getch(void)
{
return(bufp>0) ? buf[--bufp]:getchar();
}
void ungetch(int c)
{
if (bufp>=BUFSIZE)
printf("ungetch:too many characters\n");
else
buf[bufp++]=c;
}
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;*/
case 0x0a:
printf("\t%.8g\n",pop());
break;

default:
printf("error:unknown command %s\n",s);
break;
}
}
return 0;
}


#define MAXVAL 100

int sp=0;
double val[MAXVAL];

void push(double f)
{
if(sp<MAXVAL)
val[sp++]=f;
else
printf("error:strack full,can't push %g\n",f);
}
double pop(void)
{
if (sp>0)
return val[--sp];
else {
printf("error:stack empty\n");
return 0.0;
}
}

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧