#include<stdio.h> #include<math.h> #include<string.h> int main(){ char a[100],b[100]; int c[100]; gets(a); int len=strlen(a); int top1=-1,top2=-1,i; for(i=0;i<len;i++){ if(a[i]=='('){ c[++top2]=-100; } else if(a[i]=='+'||a[i]=='-'||a[i]=='*'||a[i]=='/'){ b[++top1]=a[i]; } else if(a[i]>='0'&&a[i]<='9'){ c[++top2]=a[i]-'0'; } else if(a[i]==')'){ char ch=b[top1]; top1--; if(ch=='+'){ int s=0; while(c[top2]!=-100){ s+=c[top2]; top2--; } c[top2]=s; } else if(ch=='-'){ int sum=0,s=0; while(c[top2-1]!=-100){ sum+=c[top2--]; } s=c[top2]-sum; c[--top2]=s; } else if(ch=='*'){ int s=1; while(c[top2]!=-100){ s*=c[top2--]; } c[top2]=s; } else if(ch='/'){ int sum=1,s=0; while(c[top2-1]!=-100){ sum*=c[top2--]; } s=c[top2]/sum; c[--top2]=s; } } } printf("%d\n",c[top2]); } |
Double click to view unformatted code.