求后缀表达式 急急急

avt_405 2012-10-31 09:11:43
#include<stdio.h>
#include<stdlib.h>
#define StackMaxSize 30

typedef int ElemType;
struct Stack
{
ElemType elem[StackMaxSize];
int top;
}s;

void Push(ElemType x)
{
if(s.top ==StackMaxSize - 1) //判断错误
printf("stack is full!\n");
else
s.elem[s.top++] = x; //
}

ElemType Pop()
{
ElemType e;
if(s.top == -1)
printf("stack is empty!\n");
else
e = s.elem[s.top--];
return e;
}

void ExprCompute(char *str)
{

char ch;
int i = 0, x;
ch = str[i++];
while(ch != '@')
{
switch(ch)
{
case '+':
x = Pop() + Pop();
break;
case '-':
x = Pop();
x = Pop() - x;
break;
case '*':
x = Pop() * Pop();
break;
case '/':
x = Pop();
if(x != 0)
x = Pop() / x;
else
{
printf("Divided by 0 !\n");
exit(1);
}
break;
case ' ':
ch=str[i++];
continue;
default:
x = ch - 48; //
break;
while(str[i] >= '0' && str[i] <= '9')
{
x = x * 10 + str[i] - 48; //
i++;
}
}
Push(x);
ch = str[i++];
}

if(s.top > -1)
{
x = Pop();
printf("%d",x);
if(s.top == -1)
return ;
else
{
printf("expression error !\n");
exit(1);
}
}
else
{
printf("Stack is empty!\n");
exit(1);
}
}
int
main()
{
s.top = -1;
char str[20];
printf("please input a string ended with a @:");
gets(str);

ExprCompute(str);

}
输入的时候数字和符号想用空格隔开 但是实现的有点问题 哪位大神给指点一下
...全文
113 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
JiMoKuangXiangQu 2012-11-01
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>

#define StackMaxSize 30

typedef int ElemType;
struct Stack
{
ElemType elem[StackMaxSize];
int top;
}s;

void Push(ElemType x)
{
if (s.top == -1) s.top = 0;

if(s.top ==StackMaxSize - 1) //判断错误
printf("stack is full!\n");
else
s.elem[s.top++] = x; //
}

ElemType Pop()
{
ElemType e;
if(s.top == -1)
printf("stack is empty!\n");
else
e = s.elem[--s.top];
return e;
}

void ExprCompute(char *str)
{
char ch;
int i = 0, x;
ch = str[i++];
while(ch != '@')
{
switch(ch)
{
case '+':
x = Pop() + Pop();
break;
case '-':
x = Pop();
x = Pop() - x;
break;
case '*':
x = Pop() * Pop();
break;
case '/':
x = Pop();
if(x != 0)
x = Pop() / x;
else
{
printf("Divided by 0 !\n");
exit(1);
}
break;
case ' ':
ch=str[i++];
continue;
default:
x = ch - 48; //
break;
while(str[i] >= '0' && str[i] <= '9')
{
x = x * 10 + str[i] - 48; //
i++;
}
}
Push(x);
ch = str[i++];
}

if(s.top > -1)
{
x = Pop();
printf("%d",x);
if(s.top == -1)
return ;
else
{
printf("expression error !\n");
exit(1);
}
}
else
{
printf("Stack is empty!\n");
exit(1);
}
}

int
main()
{
s.top = -1;
char str[20];
printf("please input a string ended with a @:");
gets(str);

ExprCompute(str);

return 0;
}


s.top在第一次压栈时要正确初始化,出栈的时候应该用--s.top操作.
另外至少+操作有问题,应该再向后取一个操作数,而不是同时从栈上的出栈2个操作数.我没有修正这个问题.
其他的操作符应该也有问题.

学习调试是不可避免的,对自己有好处.
石乐志123333 2012-11-01
  • 打赏
  • 举报
回复
哇!!40分啊!!!
我在你另一个帖子里回答了,希望给分啊
我也是新人
avt_405 2012-10-31
  • 打赏
  • 举报
回复
跟没说不一样吗?混分不用这样吧,真的很着急
恨天低 2012-10-31
  • 打赏
  • 举报
回复
调试。

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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