计算1+1之类的东西的~~ 不支持小数~~ #include <stdio.h> #include <math.h> #include <string.h> int num_stack[55]; char ope_stack[55], num_temp[10]; int To_num(char x[], int y) { int i, z; z=0; for(i=0; i<=y; i++) z = z + (x[i]-'0') * pow(10, y-i); return z; } int Ope(char ch) { if(ch== '*' || ch=='/') return 4; else if(ch=='+' || ch=='-') return 3; else if(ch=='(') return 5; else if(ch==')') return 2; else if(ch=='/n') return 1; else return 0; } int Flag(int x) { int y; y=Ope( ope_stack[x] ); return y; } int Cal(int x, int y) { int z; if(ope_stack[y]=='*') z=num_stack[x-1]*num_stack[x]; else if(ope_stack[y]=='/') z=num_stack[x-1]/num_stack[x]; else if(ope_stack[y]=='+') z=num_stack[x-1]+num_stack[x]; else if(ope_stack[y]=='-') z=num_stack[x-1]-num_stack[x]; return z; } int main(void) { char ch; int i, j, k, sign1, sign2, sign3; int top1, top2, top3; while(1) { top1=top2=top3=-1;//top2为运算符栈指针 ope_stack[++top2]='/n'; num_stack[++top1]='0'; sign3=1;//sign3标示是否继续输入字符 while(scanf("%c", &ch)) { if(ch>='0' && ch<= '9') { num_temp[++top3]=ch;//top3临时数字(数组 )指针 } else { sign1=Ope(ch);//sign1标示输入是否为有效操作符 if(sign1) { if(top3!=0) { num_stack[++top1]=To_num(num_temp, top3); top3=-1; } sign2=1;//sign2标示是否继续前搜 while(sign2) { if(sign1 == 2 && Flag(top2) == 2) { top2--; break; } else if(sign1==1 && Flag(top2)==1) { sign3=0; break; } else if(sign1 <= Flag(top2) ) { num_stack[top1-1]=Cal(top1, top2); top1--; top2--; } else if(sign1 > Flag(top2) ) { if(ch=='(') ch=')'; ope_stack[++top2]=ch; sign2=0; } } }//输入非数字判定 结束; } if(!sign3) { break; } } printf("The answer is %d/n/n", num_stack[1]); } } return 0; }