表达式求值,不知道哪里错了,可以执行但没结果,求高手帮忙啊!
#include<stdio.h>#include<stdlib.h>
#include<malloc.h>
#define stackinitsize 100
struct sqstack
{ char *base;
char *top;
char stacksize;};
initstack(struct sqstack &s)
{ s.base=(char*)malloc(stackinitsize*sizeof(char));
s.top=s.base;
s.stacksize=stackinitsize;return 1;
}
push1(struct sqstack &s,char c)
{*s.top=c;
s.top++;
return c;}
push2(struct sqstack &s,int p)
{*s.top=p;
s.top++;
return p;}
gettop1(sqstack s,char c)
{if(s.top==s.base)return 0;
c=*(s.top-1);
return c;
}
gettop2(sqstack s,int p)
{if(s.top==s.base)return 0;
p=*(s.top-1);
return p;
}
pop1(struct sqstack &s,char c)
{if(s.base==s.top)return 0;
else c=*--s.top;
return c;}
pop2(struct sqstack &s,int p)
{if(s.base==s.top)return 0;
else p=*--s.top;
return p;}
char precede(char t1,char t2)
{ char f;
switch(t2)
{case'+':
if(t1=='('||t1=='#')
f='<';
else f='>';
case'-':
if(t1=='('||t1=='#')
f='<';
else f='>';
break;
case'*':
if(t1=='*'||t1=='/'||t1==')')
f='>';
else f='<';
case'/':
if(t1=='*'||t1=='/'||t1==')')
f='>';
else f='<';
case'(':
if(t1==')')
{printf("error\n");}
case')':
if(t1=='#')
{printf("error\n");};
if(t1=='(')f='=';
else f='>';
case'#':
if(t1=='(')printf("error\n");
else if(t1=='#')f='=';
else f='>';
}return f;
}
in(char c)
{
switch(c){
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
case '#':return 1;
}
}
int operate(int a,char theta,int b)
{int c;
switch(theta)
{case'+':c=a+b;break;
case'-':c=a-b;break;
case'*':c=a*b;break;
case'/':c=a/b;break;}
return c;}
main()
{char c;
sqstack optr,opnd;
int p;
int a;
int b;
char theta;
char t1;
initstack(optr);
initstack(opnd);
printf("input expressions end with '#'\n");
push1(optr,'#');
c=getchar();
while(c!='#'||gettop1(optr,c)!='#')
{if(!in(c)){
push2(opnd,p);
c=getchar();}
else
switch(precede(t1,c))
{case'<':push1(optr,c);
c=getchar();
break;
case'=':pop1(optr,c);
c=getchar();
break;
case'>':pop1(optr,theta);
pop2(opnd,b);
pop2(opnd,a);
push2(opnd,operate(a,theta,b));
break;}
}
gettop2(opnd,p);
printf("%d",p);
}