我写的逆波兰式求值的程序,但是有错,求高手解决
逆波兰式求值的程序调试后就知道错在哪了
# include<stdio.h>
# include<stdlib.h>
#define OK 1
#define ERROR 0
#define STACK_ZJ_SIZE 10
#define STACK_CREAT_SIZE 100
typedef struct{
char *top,*base;
int STACKSIZE;
}STACK;
int creat_stack(STACK *S1,STACK *S2)//创建栈
{
S1->base=(char *)malloc(STACK_CREAT_SIZE*sizeof(char));//给存储运算符号的栈分配空间
S2->base=(char *)malloc(STACK_CREAT_SIZE*sizeof(char));//给存储运算数字的栈分配空间
if(!S1->base||!S2->base){
printf("内存分配失败。");
return ERROR;
}
S1->top=S1->base;
S2->top=S2->base;
S1->STACKSIZE=STACK_CREAT_SIZE;
S1->STACKSIZE=STACK_CREAT_SIZE;
return OK;
}
int insert_top(STACK *S,char x)//插入新的栈顶
{
if(S->top-S->base>=S->STACKSIZE){
S->base=(char *)realloc(S->base,(STACK_CREAT_SIZE+STACK_ZJ_SIZE)*sizeof(char));
if(!S->base) return ERROR;
S->top=S->base+S->STACKSIZE;
S->STACKSIZE=S->STACKSIZE+STACK_ZJ_SIZE;
}
S->top++;
S->top=&x;
return OK;
}
char put_top(STACK *S,char *e)//用e返回栈顶元素
{
if(S->top-S->base==0) return ERROR;
e=S->top;
return *e;
}
int delete_top(STACK *S)//删除栈顶元素
{
if(S->top-S->base==0) return ERROR;
S->top--;
return OK;
}
char panduan(char c1,char c2)//判断运算符号优先级
{
char DX;
if((c2=='+')&&(c1=='+'||c1=='-'||c1==')'||c1=='#')) DX='>';
else if((c2=='-')&&(c1=='+'||c1=='-'||c1==')'||c1=='#')) DX='>';
else if((c2=='*')&&(c1=='+'||c1=='-'||c1=='*'||c1=='/'||c1==')'||c1=='#')) DX='>';
else if((c2=='/')&&(c1=='+'||c1=='-'||c1=='*'||c1=='/'||c1==')'||c1=='#')) DX='>';
else if((c2==')')&&(c1=='+'||c1=='-'||c1=='*'||c1=='/'||c1==')'||c1=='#')) DX='>';
else if(c2=='('&&c1==')') DX='=';
else DX='<';
return DX;
}
char yunsuan(char m,char n,char k)//运算函数
{
int m1,n1,s;
char s1;
m1=m-48,n1=n-48;
if(k=='+') s=m1+n1;
if(k=='-') s=m1-n1;
if(k=='*') s=m1*n1;
if(k=='/') s=m1/n1;
s1=s+48;
return s1;
}
int main()//主函数
{
int i;
STACK shuzi,fuhao;
char SR[20],top1,a,b,fh,jieguo;
i=0;
creat_stack(&shuzi,&fuhao);
insert_top(&fuhao,'#');//给存储符号的栈插入新栈顶元素'#'
gets(SR);
while(1){
if(SR[i]>=48&&SR[i]<=57) insert_top(&shuzi,SR[i]),i++;//若是数字则将SR压入存储数字的栈
else if(SR[i]=='=') break;//输入=则结束while
else{
put_top(&fuhao,&top1);//用top1返回栈顶元素
switch(panduan(SR[i],top1)){
case'<':insert_top(&fuhao,SR[i]),i++;break;//若栈顶元素优先级<新输入元素,则将新输入的压入栈顶
case'=':delete_top(&fuhao);break;//若栈顶元素优先级=新输入元素,则删除栈顶,也就是去括号
case'>':
put_top(&shuzi,&a);delete_top(&shuzi);//用a返回数字栈的栈顶元素,并且删除
put_top(&shuzi,&b);delete_top(&shuzi);//用b再次返回数字栈的栈顶元素,并且删除
put_top(&fuhao,&fh);delete_top(&fuhao);//用fh返回运算符号栈的栈顶元素,并且删除
insert_top(&shuzi,yunsuan(a,b,fh));//进行运算,并且将运算结果压入数字栈的栈顶
break;
}
}
}
put_top(&shuzi,&jieguo);
printf("%c\n",jieguo);
return 0;
}
求高手帮忙