Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
那位高手帮看看,不知为什么总是wa#include<stdio.h> #include<stdlib.h> #include<string.h> struct aaa{ int var; int stat; }stack[29]; char op[10000]; char str[10000]; int var[26]; bool flag[26]; bool ff[10000]; int evaluator() { int i=0,j,sum; int count=0; int len,len1,len2; while(str[i]!='\0') { if(str[i]==' ') { i++; continue; } str[count++]=str[i++]; } str[count]='\0'; len=count; count=0; i=0; while(str[i]!='\0') { if(str[i]>='a' && str[i]<='z') { flag[str[i]-'a']=true; if(i-1 >=0 && str[i-1]=='-' && i-2 >=0 && str[i-2]=='-') { stack[count].var=str[i]-'a'; var[stack[count].var]--; stack[count].stat=0; ff[i]=true; ff[i-1]=true; ff[i-2]=true; } else if(i+1 <len && str[i+1]=='-' && i+2 <len && str[i+2]=='-') { stack[count].var=str[i]-'a'; stack[count].stat=1; ff[i]=true; ff[i+1]=true; ff[i+2]=true; } else if(i-1 >=0 && str[i-1]=='+' && i-2 >=0 && str[i-2]=='+') { stack[count].var=str[i]-'a'; stack[count].stat=3; var[stack[count].var]++; ff[i]=true; ff[i-1]=true; ff[i-2]=true; } else if(i+1 <len && str[i+1]=='+' && i+2 <len && str[i+2]=='+') { stack[count].var=str[i]-'a'; stack[count].stat=4; ff[i]=true; ff[i+1]=true; ff[i+2]=true; } else { stack[count].var=str[i]-'a'; stack[count].stat=2; ff[i]=true; } count++; } i++; } len1=count; i=0; count=0; while(str[i]!='\0') { if(!ff[i]) op[count++]=str[i]; i++; } len2=count; j=0; sum=var[stack[0].var]; if(len2==len1) { if(op[0]=='-') sum=-sum; j=1; } if(stack[0].stat==1) var[stack[0].var]--; else if(stack[0].stat==4) var[stack[0].var]++; for(i=1;i<len1;i++,j++) { if(op[j]=='+') sum+=var[stack[i].var]; else sum-=var[stack[i].var]; if(stack[i].stat==1) var[stack[i].var]--; else if(stack[i].stat==4) var[stack[i].var]++; } return sum; } int main() { int test,i; char ch; scanf("%d",&test); ch=getchar(); while(test>0) { for(i=0;i<26;i++) var[i]=i+1; memset(flag,false,sizeof(flag)); ch=getchar(); i=0; while(ch!='\n') { str[i++]=ch; ch=getchar(); } printf("Expression: %s\nvalue = ",str); printf("%d\n",evaluator()); for(i=0;i<26;i++) if(flag[i]) printf("%c = %d\n",i+'a',var[i]); test--; } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator