该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
#include
#include
#include
#define STACKSIZE 100
typedef struct{ int *top;
int *base;
int listsize;
}sqstack1;
typedef struct{char *top;
char *base;
int listsize;
}sqstack2;
void initstack_1(sqstack1 *l)
{l->listsize=STACKSIZE;
l->base=(int *)malloc(l->listsize*sizeof(int));
l->top=l->base;
}
void initstack_2(sqstack2 *l)
{l->listsize=STACKSIZE;
l->base=(char *)malloc(l->listsize);
l->top=l->base;
}
void pushstack_1(sqstack1 *l,char e)
{int i;
if(l->top-l->base==l->listsize)
{printf("there is error\n");
exit(0);}
i=e-48;
*(l->top++)=i;
}
void pushstack_2(sqstack2 *l,char e)
{ if(l->top-l->base==l->listsize)
{printf("there is error\n");
exit(0);}
*(l->top++)=e;}
int popstack_1(sqstack1 *l)
{int e;
if(l->top==l->base)
{printf("there is error\n");
exit(0);}
e=*(--l->top);return (e);
}
char popstack_2(sqstack2 *l)
{char e;
if(l->top==l->base)
{printf("there is error\n");
exit(0);
}
e=*(--l->top);return(e);
}
int in(char e)
{if(e>='0'&&e<='9')
return(0);
else
return(1);
}
char gettop_2(sqstack2 l)
{ char e;
if(l.top==l.base)
{printf("there is error\n");
exit(0);}
e=*(l.top-1);
return(e);
}
int gettop_1(sqstack1 l)
{int e;
if(l.top==l.base)
{printf("there is error\n");
exit(0);
}
e=*(l.top-1);
return(e);
}
char judge(char m,char n)
{int i,j;
char a[7]={'+','-','*','/','(',')','#'};
char b[7][7]={{'>','>','','>'},
{'>','>','','>'},
{'>','>','>','>','','>'},
{'>','>','>','>','','>'},
{'
{'>','>','>','>',' ','>','>'},
{'
};
for(i=0;i<7;i++)
if(a[i]==m)
break;
for(j=0;j<7;j++)
if(a[j]==n)
break;
return(b[i][j]);
}
int operate(int a,char theta,int b)
{int n;
if(theta=='+') n=a+b;
if(theta=='-') n=a-b;
if(theta=='*') n=a*b;
if(theta=='/') n=a/b;
return (n);
}
main()
{char c,theta;
int a,b,t;
sqstack1 *la;
sqstack2 *lb;
clrscr();
initstack_1(la);
initstack_2(lb);
pushstack_2(lb,'#');
printf("please input the expression\n");
c=getchar();
while(c!='#'||gettop_2(*lb)!='#')
{if(!in©){pushstack_1(la,c);c=getchar();}
else
switch(judge(gettop_2(*lb),c))
{ case'
break;
case'=': popstack_2(lb);popstack_2(lb);c=getchar();
break;
case'>': theta=popstack_2(lb);b=popstack_1(la);
a=popstack_1(la);pushstack_1(la,operate(a,theta,b));
break;
}
}
t=gettop_1(*la);
printf("the result is:\n");
printf("%d",t);
getch();
}