算术表达试求值的程序,不知道哪里错了
#include<stdio.h>#include<malloc.h>
#include<stdlib.h>
typedef struct
{
char *base;
char *top;
int stacksize;
}CharStack;
typedef struct
{
int *base;
int *top;
int stacksize;
}IntStack;
void InitCharStack(CharStack *s)//构造符号栈
{
s->base=(char*)malloc(100*sizeof(char));
s->top=s->base;
s->stacksize=100;
}
void InitIntStack(IntStack *s)//构造数字栈
{
s->base=(int*)malloc(100*sizeof(int));
s->top=s->base;
s->stacksize=100;
}
char GetTopChar(CharStack s,char e)//取符号栈顶元素
{
if(s.top!=s.base)
{
e=*(s.top-1);
return e;
}
return '0';
}
int GetTopInt(IntStack s,int e)//取栈顶元素
{
if(s.top!=s.base)
{
e=*(s.top-1);
return e;
}
return 0;
}
PushChar(CharStack *s,char e)//添加符号元素
{
if(s->top-s->base>=s->stacksize)
{
int a;
a=s->stacksize;
s->base=(char*)malloc((a+10)*sizeof(char));
s->top=s->base+a;
s->stacksize=a+10;
*s->top++=e;
}
else
{
*s->top++=e;
}
}
PushInt(IntStack *s,int e)//添加数字元素
{
if(s->top-s->base>=s->stacksize)
{
int a;
a=s->stacksize;
s->base=(int*)malloc((a+10)*sizeof(int));
s->top=s->base+a;
s->stacksize=a+10;
*s->top++=e;
}
else
{
*s->top++=e;
}
}
void PopChar(CharStack *s,char e)//删除符号栈顶元素
{
if(s->top==s->base)
{
return;
}
s->top--;
e=*s->top;
}
void PopInt(IntStack *s,int e)//删除数字栈顶元素
{
if(s->top==s->base)
{
return;
}
s->top--;
e=*s->top;
}
int IsChar(char c)//判断是否为运算符
{
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')')
return 1;
else
return 0;
}
int detect(char temp)//搜索矩阵位置
{
int i=0;
char oper[7]={'+','-','*','/','(',')','#'};
for(i=0;i<7;i++)
{
if(temp==oper[i])
{
return i;
}
}
return -1;
}
char Priority(char temp,char optr)//判断优先级
{
/**//*
+ - * / ( ) #
1 2 3 4 5 6 7
+ 1 < < < < > > >
- 2 < < < < > > >
* 3 > > < < > > >
/ 4 > > < < > > >
( 5 > > > > > = 0
) 6 < < < < = 0 >
# 7 < < < < > 0 =
*/
int row ,col;
char priority[7][7]={/**//* + - * / ( ) # */
{'<','<','<','<','>','>','>'},
{'<','<','<','<','>','>','>'},
{'>','>','<','<','>','>','>'},
{'>','>','<','<','>','>','>'},
{'>','>','>','>','>','=','0'},
{'<','<','<','<','=','0','>'},
{'<','<','<','<','>','0','='},
};
row = detect(temp);//找出对应的矩阵下标;
col = detect(optr);
// printf("%d,%d",row,col);
//优先级存储在一个7x7的矩阵中,对应关系上图;
return priority[row][col];
}
int Evaluate(int a,int b,char oper)
{
int c;
switch(oper)
{
case '+': c=a+b;break;
case '-': c=a-b;break;
case '*': c=a*b;break;
case '/': c=a/b;break;
default : exit(-1);break;
}
return c;
}
int EvaluateExpression()
{
int inputInt,topInt;
char inputChar,topChar,str,x;//str='<' or'>' or'='
CharStack OPTR;
IntStack OPND;
int a,b;
char theta;
topChar=' ';
str='0';//initiate str
InitCharStack(&OPTR);
PushChar(&OPTR,'#');
InitIntStack(&OPND);
scanf("%d",&inputInt);
PushInt(&OPND,inputInt);
inputChar=getchar();
while(inputChar!='#'||GetTopChar(OPTR,topChar)!='#')
{
// if(inputChar=='('||inputChar==')')
// {
// PushChar(&OPTR,inputChar);
// scanf("%d",inputInt);
// PushInt(&OPND,inputInt);
// inputChar=getchar();
// }
if(inputChar=='+'||inputChar=='-'||inputChar=='*'||inputChar=='/')
{
str=Priority(GetTopChar(OPTR,topChar),inputChar);
switch(str)
{
case '<':
PushChar(&OPTR,inputChar);
scanf("%d",inputInt);
PushInt(&OPND,inputInt);
inputChar=getchar();
break;
case '=':
PopChar(&OPTR,x);
inputChar=getchar();
break;
case '>':
PopChar(&OPTR,theta);
PopInt(&OPND,b);
PopInt(&OPND,a);
PushInt(&OPND,Evaluate(a,b,GetTopChar(OPTR,topChar)));
break;
}//switch
}//else if
else
{
exit(-1);
}
}//while
return GetTopInt(OPND,topInt);
}
main()
{
int result=0;
printf("请输入运算式\n");
result=EvaluateExpression();
printf("%d",result);
}