#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct{
int *base;
int *top;
int stacksize;
}SqStack;
void InitStack(SqStack *s)
{
s->base=(int *)malloc(STACK_INIT_SIZE *sizeof(int));
if(!s->base) exit(1);
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
}
void GetTop(SqStack *s,int *e)
{
if(s->top==s->base)
{
printf("ERROR!\n");
exit(1);
}
*e=*(s->top-1);
}
void Push(SqStack *s,int e)
{
if(s->top-s->base>=s->stacksize)
{
s->base=(int *)realloc(s->base,(s->stacksize+STACKINCREMENT*sizeof(int)));
if(!s->base)
{
printf("overflow!");
exit(1);
}
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*s->top++=e;
}
void Pop(SqStack *s,int *e)
{
if(s->top==s->base)
exit(1);
*e=*--s->top;
}
int precede(int Top_char,int s1_char)
{
int i,pre[2],op[2];
op[0]=Top_char;
op[1]=s1_char;
for(i=0;i<2;i++)
switch(op[i])
{
case'(': case')': pre[i]=0;break;
case'+': case'-': pre[i]=1;break;
case'*': case'/': pre[i]=2;break;
case'^': pre[i]=3;break;
}
- 1
- 2
- 3
- 4
前往页