#include<string.h> #include<stdio.h> #include<malloc.h> #include<conio.h> #define N 100 #define M 10
typedef char SElemType;
typedef struct{ SElemType *base; SElemType *top; int stacksize; }SqStack1; typedef struct{ int *base; int *top; int stacksize; }SqStack2; void InitStack1(SqStack1 *s) { s->base=(SElemType *)malloc(N*sizeof(SElemType)); if(!s->base)exit(1); s->top=s->base; s->stacksize=N; }
void InitStack2(SqStack2 *s) { s->base=(int *)malloc(N*sizeof(int)); if(!s->base)exit(1); s->top=s->base; s->stacksize=N; } void Push1(SqStack1 *s,int e) { if(s->top-s->base>=s->stacksize) { s->base=(SElemType*)realloc(s->base,(s->stacksize+=M)*sizeof(SElemType)); if(!s->base)exit(1); s->top=s->base+s->stacksize; s->stacksize+=M; }
*s->top++=e;
} void Push2(SqStack2 *s,int e) {
if(s->top-s->base>=s->stacksize) { s->base=(int*)realloc(s->base,(s->stacksize+=M)*sizeof(int)); if(!s->base)exit(1); s->top=s->base+s->stacksize; s->stacksize+=M; }
*s->top++=e; }
int GetTop1(SqStack1 *s) {int e; if(s->top==s->base) return 1; e=*(s->top-1); return e; } int GetTop2(SqStack2 *s) {int e; if(s->top==s->base) return 1; e=*(s->top-1); return e; } void Pop1(SqStack1 *s,int e) {
e=*--s->top;
} void Pop2(SqStack2 *s,int e) {
e=*--s->top;
}
char OP[10]={'+','-','*','/','(',')','#'}; int precede[7][7]={ 1,1,2,2,2,1,1, 1,1,2,2,2,1,1, 1,1,1,1,2,1,1, 1,1,1,1,2,1,1, 2,2,2,2,2,3,0, 1,1,1,1,0,1,1, 2,2,2,2,2,0,3};
int In(char c) { int i=0; while(i<7) {if(c==OP[i++]) return 1; return 0;} }
char Precede(char op,char c) { int pos_op; int pos_c; int i; for(i=0;i<7;i++) { if(op==OP[i]) pos_op=i; if(c==OP[i]) pos_c=i; } switch(precede[pos_op][pos_c]) { case 1: return '>'; case 2: return '<'; case 3: return '='; } }
int Operate(int a,char theta,int b) { switch(theta) { case '+':return a+b; case '-':return a-b+'0'; case '*':return (a-'0')*(b-'0')+'0'; case '/':return (a-'0')/(b-'0')+'0'; } }
char EvaluateExpression() { SqStack2 *OPND;SqStack1 *OPTR; char c,x,theta; char a,b; InitStack1(OPTR); Push1(OPTR,'#'); InitStack2(OPND); c=getchar(); while(c!='#'||GetTop1(OPTR)!='#') { if(!In(c)) {Push2(OPND,c);c=getchar();} else switch(Precede(GetTop1(OPTR),c)) { case '<': Push1(OPTR,c); c=getchar(); break; case '=':
Pop1(OPTR,x); c=getchar(); break; case '>': Pop1(OPTR,theta); Pop2(OPND,b); Pop2(OPND,a); Push2(OPND,Operate(a,theta,b)); break; } } c=GetTop2(OPND);
return c; }
main() { char i; printf("\n\n\n\nOnly within 0..9 evaluation,input a expression end with symbol #:\n"); i=EvaluateExpression(); printf("\nThis expression's result is: "); printf("%d\n\n\n\n",i); getch(); }
----------------解决方案--------------------------------------------------------
这么长。看得头晕。等后面的大虾来吧!!不好意思!!
----------------解决方案--------------------------------------------------------
难道没人救命吗???先谢一下楼上的兄弟
----------------解决方案--------------------------------------------------------
s->top-s->base>=s->stacksize
先把这里的=改成==再说
----------------解决方案--------------------------------------------------------
级别太低,看了头晕.帮不了你,非常抱歉.
----------------解决方案--------------------------------------------------------