谁来帮我看这个程序呀,计算表达式值的程序,编译通过,就是没有正确结果,其中的字符输入函数总是跳过去不执行。 #include <stdio.h> #define maxsize 100 typedef char elementype; typedef struct { char data[maxsize]; int top; }stacktype;
void initstack(stacktype *s) { s->top=-1; }
void push(stacktype *s,char x) { if (s->top==maxsize) printf("the stack is full"); else { s->top++; s->data[s->top]=x; } }
char pop(stacktype *s) { elementype x; if (s->top==-1) printf("the stack is empty"); else { x=s->data[s->top]; s->top--; return(x); } }
int empty(stacktype *s) { if (s->top==-1) return(1); else return(0); }
int stackfull(stacktype *s) { if (s->top==maxsize) return(1); else return(0); }
char top(stacktype *s) { char x; if (s->top==-1) return(0); else x=s->data[s->top]; return(x); }
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,char *op) { int i=0; while(i<7) if(c==op[i++]) return 1; else return 0; }
char Preced(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 '='; } }
char Operate(int a,char theta,int b) { switch(theta) { case '+':return a+b-'0'; case '-':return a-b+'0'; case '*':return (a-'0')*(b-'0')+'0'; case '/':return (a-'0')/(b-'0')+'0'; } }
char EvaluateExpression() { stacktype s2,s1; char c,x,theta; char a,b; initstack(&s2); push(&s2,'#'); initstack(&s1); c=getchar(); while(c!='#'||top(&s2)!='#') { if(!In(c,OP)) { push(&s1,c); c=getchar();} else switch(Preced(top(&s2),c)) { case '<': push(&s2,c); c=getchar(); break; case '=': pop(&s2); c=getchar(); break; case '>': theta=pop(&s2); b=pop(&s1); a=pop(&s1); push(&s1,Operate(a,theta,b)); break; } } c=top(&s1);
return c; }
main() { char i; i=EvaluateExpression(); printf("\nThis expression's result is: "); printf("%d\n\n\n\n",i-'0');
}