我是这样写的
#include<stdio.h>
#include<conio.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define TRUE 1
#define OK 1
#define FALSE 0
#define OVERFLOW -2
#define ERROR 0
typedef int Status;
typedef char SElemType1;
typedef int SElemType2;
char OP[7]={'+','-','*','/','(',')','#'};
typedef struct
{
SElemType1 *base;
SElemType1 *top;
int stacksize;
}CharStack;
typedef struct
{
SElemType2 *base;
SElemType2 *top;
int stacksize;
}IntStack;
Status initIntStack(IntStack *I)
{
(*I).base=(SElemType2 *)malloc(STACK_INIT_SIZE*sizeof(SElemType2));
if(!(*I).base) exit(OVERFLOW);
(*I).top=(*I).base;
(*I).stacksize=STACK_INIT_SIZE;
return OK;
}
Status initCharStack(CharStack *C)
{
(*C).base=(SElemType1 *)malloc(STACK_INIT_SIZE*sizeof(SElemType1));
if(!(*C).base) exit(OVERFLOW);
(*C).top=(*C).base;
(*C).stacksize=STACK_INIT_SIZE;
return OK;
}
Status desintStack(IntStack *I)
{
(*I).top=NULL;
free((*I).base);
(*I).base=NULL;
return OK;
}
Status descharStack(CharStack *C)
{
(*C).top=NULL;
free((*C).base);
(*C).base=NULL;
return OK;
}
int getinttop(IntStack I)
{
SElemType2 e;
if(I.top==I.base) return ERROR;
e=*(I.top-1);
return e;
}
char getchartop(CharStack C)
{
SElemType1 e;
if(C.top==C.base) return ERROR;
e=*(C.top-1);
return e;
}
Status pushchar(CharStack *C,SElemType1 e)
{
if((*C).top-(*C).base>=(*C).stacksize)
{
(*C).base=(SElemType1 *)realloc((*C).base,((*C).stacksize+STACKINCREMENT)*sizeof(SElemType1));
if(!(*C).base) exit(OVERFLOW);
(*C).top=(*C).base+(*C).stacksize;
(*C).stacksize+=STACKINCREMENT;
}
*(*C).top++=e;
return OK;
}
Status pushint(IntStack *I,SElemType2 e)
{
if((*I).top-(*I).base>=(*I).stacksize)
{
(*I).base=(SElemType2 *)realloc((*I).base,((*I).stacksize+STACKINCREMENT)*sizeof(SElemType2));
if(!(*I).base) exit(OVERFLOW);
(*I).top=(*I).base+(*I).stacksize;
(*I).stacksize+=STACKINCREMENT;
}
*(*I).top++=e;
return OK;
}
Status popchar(CharStack *C,SElemType1 *e)
{
if((*C).top==(*C).base) return ERROR;
*e=*(--(*C).top);
return OK;
}
Status popint(IntStack *I,SElemType2 *e)
{
if((*I).top==(*I).base) return ERROR;
*e=*(--(*I).top);
return OK;
}
Status In(char c,char OP[])
{
int i;
for(i=0;i<=6;i++)
if(c==OP[i]) return TRUE;
return FALSE;
}
int Transfer(char c)
{
int k;
switch(c)
{
case '+':k=0;break;
case '-':k=1;break;
case '*':k=2;break;
case '/':k=3;break;
case '(':k=4;break;
case ')':k=5;break;
case '#':k=6;break;
}
return k;
}
char Precede(char c1,char c2)
{
int i,j;
char a[7][7]={{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=',''},
{'>','>','>','>','','>','>'},
{'<','<','<','<','<','','='}};
i=Transfer(c1);
j=Transfer(c2);
return(a[i][j]);
}
int Operate(int a,char c,int b)
{
int z;
switch(c)
{
case '+':z=b+a;break;
case '-':z=b-a;break;
case '*':z=b*a;break;
case '/':z=b/a;break;
}
return z;
}
int EvaluateExpression()
{
int flag=0,num,a,b,answer;
char c,x;
IntStack NUM;
CharStack OPTR;
initCharStack(&OPTR);
pushchar(&OPTR,'#');
initIntStack(&NUM);
c=getchar();
while(c!='#'||getchartop(OPTR)!='#')
{
if(!In(c,OP))
{
if(flag==1)
{
popint(&NUM,&num);
num=num*10+(c-'0');
pushint(&NUM,num);
}
else
{
pushint(&NUM,c-'0');
flag=1;
}
c=getchar();
}
else
{
switch(Precede(getchartop(OPTR),c))
{
case '<':pushchar(&OPTR,c);c=getchar();break;
case '=':popchar(&OPTR,&x);c=getchar();break;
case '>':popchar(&OPTR,&x);
popint(&NUM,&a);
popint(&NUM,&b);
pushint(&NUM,Operate(a,x,b));
break;
}/*switch*/
flag=0;
}
}/*while*/
answer=getinttop(NUM);
desintStack(&NUM);
descharStack(&OPTR);
return answer;
}
main()
{
int loop=1,answer;
while(loop)
{
clrscr();
printf("\n\n\n\t\tPlease input your biaodashi:(end with '#')");
answer=EvaluateExpression();
printf("\n\t\tThe answer is : %d",answer);
printf("\n\t\tOnce again?(1/0)");
scanf("%d%*c",&loop);
}
printf("\n\n\n\t\tThank you for your useing!");
getch();
}
#include<stdio.h>
#include<conio.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define TRUE 1
#define OK 1
#define FALSE 0
#define OVERFLOW -2
#define ERROR 0
typedef int Status;
typedef char SElemType1;
typedef int SElemType2;
char OP[7]={'+','-','*','/','(',')','#'};
typedef struct
{
SElemType1 *base;
SElemType1 *top;
int stacksize;
}CharStack;
typedef struct
{
SElemType2 *base;
SElemType2 *top;
int stacksize;
}IntStack;
Status initIntStack(IntStack *I)
{
(*I).base=(SElemType2 *)malloc(STACK_INIT_SIZE*sizeof(SElemType2));
if(!(*I).base) exit(OVERFLOW);
(*I).top=(*I).base;
(*I).stacksize=STACK_INIT_SIZE;
return OK;
}
Status initCharStack(CharStack *C)
{
(*C).base=(SElemType1 *)malloc(STACK_INIT_SIZE*sizeof(SElemType1));
if(!(*C).base) exit(OVERFLOW);
(*C).top=(*C).base;
(*C).stacksize=STACK_INIT_SIZE;
return OK;
}
Status desintStack(IntStack *I)
{
(*I).top=NULL;
free((*I).base);
(*I).base=NULL;
return OK;
}
Status descharStack(CharStack *C)
{
(*C).top=NULL;
free((*C).base);
(*C).base=NULL;
return OK;
}
int getinttop(IntStack I)
{
SElemType2 e;
if(I.top==I.base) return ERROR;
e=*(I.top-1);
return e;
}
char getchartop(CharStack C)
{
SElemType1 e;
if(C.top==C.base) return ERROR;
e=*(C.top-1);
return e;
}
Status pushchar(CharStack *C,SElemType1 e)
{
if((*C).top-(*C).base>=(*C).stacksize)
{
(*C).base=(SElemType1 *)realloc((*C).base,((*C).stacksize+STACKINCREMENT)*sizeof(SElemType1));
if(!(*C).base) exit(OVERFLOW);
(*C).top=(*C).base+(*C).stacksize;
(*C).stacksize+=STACKINCREMENT;
}
*(*C).top++=e;
return OK;
}
Status pushint(IntStack *I,SElemType2 e)
{
if((*I).top-(*I).base>=(*I).stacksize)
{
(*I).base=(SElemType2 *)realloc((*I).base,((*I).stacksize+STACKINCREMENT)*sizeof(SElemType2));
if(!(*I).base) exit(OVERFLOW);
(*I).top=(*I).base+(*I).stacksize;
(*I).stacksize+=STACKINCREMENT;
}
*(*I).top++=e;
return OK;
}
Status popchar(CharStack *C,SElemType1 *e)
{
if((*C).top==(*C).base) return ERROR;
*e=*(--(*C).top);
return OK;
}
Status popint(IntStack *I,SElemType2 *e)
{
if((*I).top==(*I).base) return ERROR;
*e=*(--(*I).top);
return OK;
}
Status In(char c,char OP[])
{
int i;
for(i=0;i<=6;i++)
if(c==OP[i]) return TRUE;
return FALSE;
}
int Transfer(char c)
{
int k;
switch(c)
{
case '+':k=0;break;
case '-':k=1;break;
case '*':k=2;break;
case '/':k=3;break;
case '(':k=4;break;
case ')':k=5;break;
case '#':k=6;break;
}
return k;
}
char Precede(char c1,char c2)
{
int i,j;
char a[7][7]={{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=',''},
{'>','>','>','>','','>','>'},
{'<','<','<','<','<','','='}};
i=Transfer(c1);
j=Transfer(c2);
return(a[i][j]);
}
int Operate(int a,char c,int b)
{
int z;
switch(c)
{
case '+':z=b+a;break;
case '-':z=b-a;break;
case '*':z=b*a;break;
case '/':z=b/a;break;
}
return z;
}
int EvaluateExpression()
{
int flag=0,num,a,b,answer;
char c,x;
IntStack NUM;
CharStack OPTR;
initCharStack(&OPTR);
pushchar(&OPTR,'#');
initIntStack(&NUM);
c=getchar();
while(c!='#'||getchartop(OPTR)!='#')
{
if(!In(c,OP))
{
if(flag==1)
{
popint(&NUM,&num);
num=num*10+(c-'0');
pushint(&NUM,num);
}
else
{
pushint(&NUM,c-'0');
flag=1;
}
c=getchar();
}
else
{
switch(Precede(getchartop(OPTR),c))
{
case '<':pushchar(&OPTR,c);c=getchar();break;
case '=':popchar(&OPTR,&x);c=getchar();break;
case '>':popchar(&OPTR,&x);
popint(&NUM,&a);
popint(&NUM,&b);
pushint(&NUM,Operate(a,x,b));
break;
}/*switch*/
flag=0;
}
}/*while*/
answer=getinttop(NUM);
desintStack(&NUM);
descharStack(&OPTR);
return answer;
}
main()
{
int loop=1,answer;
while(loop)
{
clrscr();
printf("\n\n\n\t\tPlease input your biaodashi:(end with '#')");
answer=EvaluateExpression();
printf("\n\t\tThe answer is : %d",answer);
printf("\n\t\tOnce again?(1/0)");
scanf("%d%*c",&loop);
}
printf("\n\n\n\t\tThank you for your useing!");
getch();
}