请大家帮忙看一下这个程序吗,怎么修改一下,是算术表达式的
#include<stdio.h>#include<stdlib.h>
typedef char ElemType;
typedef struct
{
ElemType *base;
ElemType *top;
int StackSize;
}Stack;
int InitStack(Stack *s,int n)
{
s->base=(ElemType*)malloc(n*sizeof(ElemType));
if(s->base==NULL)return 0;
s->top=s->base;
s->StackSize=n;
return 1;
}
ElemType GetTop(Stack s)
{
if(s.top==s.base)return 0;
return *(s.top-1);
}
int Push(Stack *s,ElemType e)
{
if(s->top-s->base>=s->StackSize)
return 0;
*s->top++=e;
return 1;
}
int Pop(Stack *s,ElemType *e)
{
if(s->top==s->base)return 0;
*e=*--s->top;
return 1;
}
int Precede(char a)
{
if(a=='(')
return 0;
else if(a=='+'||a=='-')
return 1;
else if(a=='*'||a=='/')
return 2;
else if(a==')')
return 3;
}
float operate(char a,char b,char c)
{
float anwser;
switch(b)
{
case '+':
return anwser=((int)a-48)+((int)c-48);break;
case '-':
return anwser=((int)a-48)-((int)c-48);break;
case '*':
return anwser=((int)a-48)*((int)c-48);break;
case '/':
return anwser=((int)a-48)/((int)c-48);break;
}
}
char EvaluateExpressiom()
{
char c,a,b,theta;
Stack optr,opnd;
InitStack(&optr,100);
Push(&optr,'#');
InitStack(&opnd,100);
c=getchar();
while(c!='#'||GetTop(optr)!='#')
{
if(c!='+'||c!='-'||c!='*'||c!='/'||c!='('||c!=')')
{
Push(&opnd,c);c=getchar();
}
else if(Precede(GetTop(optr))<Precede(c))
{
Push(&optr,c);
c=getchar();
}
else if(Precede(GetTop(optr))==Precede(c))
{
Pop(&optr,&c);c=getchar();
}
else if(Precede(GetTop(optr))>Precede(c))
{
theta=GetTop(optr);
Pop(&optr,&theta);
a=GetTop(opnd);
Pop(&opnd,&a);
b=GetTop(opnd);
Pop(&opnd,&b);
Push(&opnd,(char)operate(a,theta,b));
}
}return GetTop(opnd);
}
int main()
{
char a;
printf("请输入算术表达式:");
a=EvaluateExpressiom();
printf("%c",a);
return 1;
}