## 下面是一个中缀法计算的算式，运行会出现错误，但没报错，求讲解

```#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(char)
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
struct stack
{
char *base;
char *top;
int stacksize;
};
struct stack * Initstack(struct stack *s)
{
s->base=(char *) malloc(STACK_INIT_SIZE * LEN);
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return s;
}
void Push(struct stack *s,char e)
{
if (s->top-s->base>=s->stacksize)
{
s->base=(char *) realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(LEN));
if (!s->base)
{
return;
}
s->top=s->base+s->stacksize;
(s->stacksize)+=STACKINCREMENT;
}
*s->top=e;
s->top++;
}
char * pop(struct stack *s,char *e)
{
e=NULL;
if (s->top==s->base) return 0;
*e= * --s->top;
return e;
}
char *GetTop(struct stack *s)
{
char *e=NULL;
if (s->top==s->base) return 0;
*e = *(s->top -1);
return e ;
}
char Precede(char a,char b)
{
if (a==b)
return '>';
if (a=='('&&b!='#')
return '<';
if (a=='('&&b==')')
return '=';
if (a==')'&&b!='(')
return '>';
if ((a=='*'||a=='/')&&b!='(')
return '>';
if (a=='+'&&(b=='-'||b==')'))
return '>';
else return '<';
if (a=='-'&&(b=='+'||b==')'))
return '>';
else return '<';
if (a=='#'&&b!=')'&&b!='#')
return '<';
if (a=='#'&&b=='#')
return '=';
}
char operate(char *a,char *theta,char *b)
{
int x,y;
char str;
x=*a-48;
y=*b-48;
switch (*theta)
{
case '*':x=x*y; break;
case '-':x=x-y; break;
case '+':x=x+y; break;
case '/':if (y!=0)
{
x=x/y;
}
else {printf("error!");}break;
}
str=x+'0';
return str;
}
int main()
{
char *x=NULL,*theta=NULL,*b=NULL,*a=NULL;
struct stack *OPTR=NULL,*OPND=NULL;
char c;
Initstack(OPTR);
Push(OPTR,'#');
Initstack(OPND);
c=getchar();
while(c!='#'||(*GetTop(OPTR))!='#')
{
if (c>47&&c<58)
{
Push(OPTR,c);
c=getchar();
}
else{
switch(Precede(*GetTop(OPTR),c))
{
case '<': Push (OPTR,c);
c=getchar();break;
case '=': pop(OPTR,x);
c=getchar();break;
case '>': pop(OPTR,theta);
*pop(OPND,b);*pop(OPND,a);
Push(OPND,operate(a,theta,b));
break;
}
}
}
printf("%c/n",GetTop(OPND));
return 0;
} ```

0

0