利用栈表达式求值没有结果?
#include<stdlib.h>#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
#define int_size 100
#define incre_size 10
#define MAX 25
typedef int Status;
typedef char SElemType;
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
//定义全局变量
Status InitStack(SqStack *S)
{
S->base=(SElemType *)malloc(sizeof(SElemType));
if(!S->base)
exit(ERROR);
S->top=S->base;
S->stacksize=int_size;
return OK;
}
Status Push(SqStack *S,SElemType e)
{
if(S->top-S->base>=int_size)
{
S->base=(SElemType *)realloc(S->base,(S->stacksize+incre_size)*sizeof(SElemType));
if(!S->base)
exit(ERROR);
S->top=S->base+S->stacksize;
S->stacksize+=incre_size;
}
*S->top++=e;
return OK;
}
/*void print(SElemType c)
{
printf("%c",c);
}
void StackTravese(SqStack *S,void (*SElemType)())
{
SElemType *p=S->base;
while(S->top>p)
visit(*p++);
printf("\n");
}*/
Status Pop(SqStack *S,SElemType &e)
{
if(S->top==S->base)
exit(ERROR);
e=*--S->top;
return OK;
}
void ClearStack(SqStack *S)
{
S->top=S->base;
}
void DestroyStack(SqStack *S)
{
free(S->base);
S->top=S->base=NULL;
S->stacksize=0;
}
Status StackEmpty(SqStack *S)
{
if(S->top==S->base)
return OK;
else
return ERROR;
}
Status GetTop(SqStack *S,SElemType &e)
{
if(S->top>S->base)
{
e=*--S->top;
return OK;
}
else
return ERROR;
}
SElemType Precede(SElemType c1,SElemType c2)
{
int i=0,j=0;
static char array[49]={
'>', '>', '<', '<', '<', '>', '>',
'>', '>', '<', '<', '<', '>', '>',
'>', '>', '>', '>', '<', '>', '>',
'>', '>', '>', '>', '<', '>', '>',
'<', '<', '<', '<', '<', '=', '!',
'>', '>', '>', '>', '!', '>', '>',
'<', '<', '<', '<', '<', '!', '='};
switch(c1)
{
/* i为下面array的横标 */
case '+' : i=0;break;
case '-' : i=1;break;
case '*' : i=2;break;
case '/' : i=3;break;
case '(' : i=4;break;
case ')' : i=5;break;
case '#' : i=6;break;
}
switch(c2)
{
/* j为下面array的纵标 */
case '+' : j=0;break;
case '-' : j=1;break;
case '*' : j=2;break;
case '/' : j=3;break;
case '(' : j=4;break;
case ')' : j=5;break;
case '#' : j=6;break;
}
return (array[7*i+j]); /* 返回运算符 */
}
SElemType Operate(SElemType a,SElemType theta,SElemType b)
{
switch(theta)
{
case '+':return a+b;
case '-':return a-b;
case '*':return a*b;
}
return a/b;
}
Status In(SElemType c)
{
switch(c)
{
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
case '#': return OK;
default: return ERROR;
}
}
SElemType EvaluateExpression()
{
SqStack OPTR,OPND;//OPTR为运算符,OPND为运算数
SElemType a,b,c,x;
InitStack(&OPTR);
InitStack(&OPND);
Push(&OPTR,'#');
c=getchar();
GetTop(&OPTR,x);
while(c!='#' || x!='#')
{
if(In(c))
switch(Precede(x,c))
{
case '<':Push(&OPTR,c);
c=getchar();
break;
case '=':Pop(&OPTR,x);
c=getchar();
break;
case '>':Pop(&OPTR,x);
Pop(&OPND,b);
Pop(&OPND,a);
Push(&OPND,Operate(a,x,b));
}
else if(c>='0' && c<='9')
{
Push(&OPND,c-48);
c=getchar();
}
else
{
printf("出现非法字符\n");
exit(ERROR);
}
GetTop(&OPTR,x);
}
Pop(&OPND,x);
if(!StackEmpty(&OPND))
{
printf("表达式不正确\n");
exit(ERROR);
}
return x;
}
int main()
{
int rel;
printf("请输入算术表达式(输入的值在0-9之间,中间运算值和输出的结果在-128-129之间:\n");
rel=EvaluateExpression();
printf("%d\n",rel);
return 0;
}