#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define STACK_INIT_SIZE 20
#define STACKINCREMENT 10
#define MAXBUFFER 10
typedef char ElemType;
typedef struct
{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;
void InitStack(SqStack *s)
{
s->base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!s->base)
exit(0);
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
}//组建
void Push(SqStack *s,ElemType e)
{
if(s->top-s->base>=s->stacksize)
{
s->base=(ElemType *)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(ElemType));
if(!s->base)
exit(0);
s->top=s->base+s->stacksize;
s->stacksize=s->stacksize+STACKINCREMENT;
}
*(s->top)=e;
s->top++;
}//入栈操作
void Pop(SqStack *s,ElemType *e)
{
if(s->top==s->base)
{
return ;
}
*e=*--(s->top);
}
int Stacklen(SqStack s)
{
return (s.top-s.base);
}
int main()
{
SqStack s;
char c;
double d,e;
char str[MAXBUFFER];
int i=0;
InitStack(&s);//初始化
printf("请按照逆波兰表达式输入待计算数据,数据与运算符之间空格隔开已#结束\n");
scanf("%c",&c);
while(c!='#')
{
while(isdigit(c)||c=='.')//用于过滤数字
{
str[i++]=c;
str[i]='\0';
if(i>=10)
{
printf("输入的单个数据过大\n");
return -1;
}
scanf("%c",&c);
if(c==' ')
{
d=atof(str);
Push(&s,d);
i=0;
break;
}
}////头文件ctype
switch(c)
{
case '+':
Pop(&s,&e);
Pop(&s,&d);
Push(&s,d+e);
break;
case '-':
Pop(&s,&e);
Pop(&s,&d);
push(&s,d-e);
break;
case '*':
Pop(&s,&e);
Pop(&s,&d);
push(&s,d*e);
break;
case '/':
Pop(&s,&e);
Pop(&s,&d);
if(e!=0)
{
push(&s,d/e);
}
else
{
printf("\n出错除数为0");
return -1;
}
}
scanf("%c",&c);
}
Pop(&s,&d);
printf("最终的计算结果为%d",d);
return 0;
}
不知道怎么改 看不懂 谁会呢?帮我该该吧!