用堆栈作的表达式求值 问题求解
我用Microsoft vistypedef c++有一个错误 找不到 还有我觉得我的主程序部分不够完整 求高手完善typedef char SElemType;
#include<string.h>
#include<malloc.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<process.h>
#define TRUE 1
#define FALSE 0
#define Stack_Size 50
typedef struct
{
SElemType elem[Stack_Size];
int top;
}SeqStack;
Status InitStack (SeqStack *S)
{
S->top=-1;
}
status IsEmpty(SeqStack *S)
{
return(S->top==-1?TRUE:FALSE);
}
status IsFull(SeqStack *S)
{
return(S->top==Stack_Size-1?TRUE:FALSE);
}
Status Push(SeqStack *S,SElemType x)
{
if(S->top==Stack_Size-1)return(FALSE);
S->top++;
S->elme[S->top]=x;
return(TRUE);
}
Status Pop(SeqStack *S,SElemType *X)
{
if(S->top==-1)
return(FALSE);
else
{
*x=S->elem[S->top];
S->top--;
return(TRUE);
}
}
Status GetTop(SeqStack S,SElemType *x)
{
if(S->top==-1)
return(FALSE);
else
{
*x==S->elem[S->top];
return(TRUE);
}
}
SElemType Precede(SElemType x,SElemType y)
{
SElemType z;
if(x=='#')
switch(y)
{
case'#': z='=';break;
case')':printf("error");break;
default: z='<';break;
}
if(x=='+'||x=='-')
switch(y)
{
case'+': z='>';break;
case'-': z='>';break;
case'*': z='<';break;
case'/': z='<';break;
case'(': z='<';break;
case')': z='>';break;
case'#': z='>';break;
}
if(x=='*'||x=='/')
switch(y)
{
case'+': z='>';break;
case'-': z='>';break;
case'*': z='>';break;
case'/': z='>';break;
case'(': z='<';break;
case')': z='>';break;
case'#': z='>';break;
}
if(x=='(')
switch(y)
{
case')': z='=';break;
case'#':printf("error");break;
default: z='<';break;
}
if(x==')')
switch(y)
{
case'(':printf("error");break;
default: z='>';break;
};
return z;
}
Status In(SElemType ch)
{
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='('||ch==')'||ch=='#')
return OK;
else return ERROR;
}
SElemType Operate(SElemType a,SElemType theta,SElemType b)
{
SElemType c;
a=a-48;
b=b-48;
switch(theta)
{
case'+':c=a+b+48;
break;
case'-':c=a-b+48;
break;
case'*':c=a*b+48;
break;
case'/':c=a/b+48;
}
return c;
}
SElemType EvaluateExpression()
{
SeqStack(&operand);
SeqStack(&operator);
Push(&OPTR,'#');
printf("\n\Please input an expression(Ending with #) :");
ch=getchar();
while(ch!='#'||GetTop(operator)!='#')
{
if(!In(ch,OPS))
{
int temp;
temp=ch-'0';
ch=getchar();
while(! In(ch,OPS))
{temp=temp*10+ch-'0'
ch=getchar();
}
Push(&operand,temp);
}
else
switch(Compare(GetTop(operator),ch)
{
case'<':Push(&operator,c);
c=getchar();
break;
case'=':Pop(&operator,&x);
c=getchar();
break;
case'>':Pop(&operator,&op);
Pop(&operand,&b);
Pop(&operand,&a);
v=Execute(a,op,b);
Push(&operand,v);
break;
}
v=GetTop(operand);
return(v);
}
void main()
{
printf("请输入表达式以"#"开始,以"#"结束(数字只能为0到9):\n");
getch();
printf("表达式结果为:%c\n",EvaluateExpression());
}