PKU 3337 Expression Evaluator

PKU 3337 Expression Evaluator

这个题目A的很傻,一次接入三个字符,然后自己不停的来处理各种情况的效果,呵呵,各种格式是应该注意的,空格算是小陷阱?呵呵,所以一般复制那个sample再改就好了,freopen("a.in","r",stdin);freopen("a.out","w",stdout);这种文件读入读出的方式对观察自己的输出结果很有好处,推荐使用防止PE,呵呵,也许是北大的数据比较弱,这个同样的题目在天大的OJ就过不了~WA的很郁闷。。。
  1 Source Code
  2
  3 Problem:  3337   User: hongtaozhy 
  4 Memory: 296K  Time: 0MS 
  5 Language: G ++   Result: Accepted 
  6
  7 Source Code 
  8 #include < stdio.h >
  9 #include < string .h >
 10 char  res[ 10000 ];
 11 char  fes[ 10000 ];
 12 char  zd[ 26 ] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26} ;
 13 bool  mark[ 26 ];
 14 int  main() {
 15    int n;
 16    int sum;
 17    int key ;
 18    //freopen("g.in","r",stdin);
 19    //freopen("gg.in","w",stdout);  
 20    scanf("%d\n",&n);
 21    int len ;    
 22    while(n--){
 23        key = 0 ;
 24    memset(mark,0,sizeof(mark));       
 25     for(int i = 0 ;i < 26 ; i++ )
 26     zd[i]=i+1;
 27               
 28    sum = 0 ;
 29    gets(fes);
 30    printf("Expression: %s\n",fes);
 31    
 32    
 33    len =  strlen ( fes );
 34    int t = 0 ;
 35    for(int i = 0 ;i < len ; i++ ){
 36    if(fes[i]!=' ') res[t++]=fes[i];
 37    if(fes[i]=='\0')               { res[t++]=0;res[t++]=0;}
 38    }

 39  //   printf("%s\n",res);
 40    len = strlen ( res );
 41    forint i = 0 ; i < len ; i++ ){
 42         if(res[i]<='z'&&res[i]>='a'&&res[i+1]==res[i+2]){
 43             if(key == 0)
 44             sum += zd[res[i]-'a'];
 45             else 
 46             sum -= zd[res[i]-'a'];
 47             if(res[i+1]=='-')
 48             zd[res[i]-'a']--;
 49             else if(res[i+1]=='+')
 50             zd[res[i]-'a']++;
 51             mark[res[i]-'a']=1;
 52           
 53             i+=2;
 54             continue;
 55         }

 56         else if(res[i+2]<='z'&&res[i+2]>='a'&&res[i+1]==res[i]){           
 57             if(res[i+1]=='-')
 58             zd[res[i+2]-'a']--;
 59             else if(res[i+1]=='+')
 60             zd[res[i+2]-'a']++;
 61             if(key == 0)
 62             sum += zd[res[i+2]-'a'];
 63             else 
 64             sum -= zd[res[i+2]-'a'];
 65             mark[res[i+2]-'a']=1;
 66          
 67              i+=2;
 68             continue;
 69         }

 70         else  if(res[i]<='z'&&res[i]>='a'){
 71       
 72             if(key == 0)
 73             sum += zd[res[i]-'a'];
 74             else 
 75             sum -= zd[res[i]-'a']; 
 76             mark[res[i]-'a']=1;
 77             if(res[i+1]=='+')                 key=0;
 78             else key=1;
 79         }

 80         else if(res[i]=='-'&&res[i+1]=='-'&&res[i+2]=='+')
 81         key=0;
 82         else if(res[i]=='+'&&res[i+1]=='+'&&res[i+2]=='-')
 83         key=1;
 84         else if(res[i]=='-'&&res[i+1]=='+'&&res[i+2]=='+')
 85         key=1;
 86         else if(res[i]=='+'&&res[i+1]=='-'&&res[i+2]=='-')
 87         key=0;
 88         else if(res[i]=='+'&&res[i+1]<='z'&&res[i+1]>='a')
 89         key=0;
 90         else if(res[i]=='-'&&res[i+1]<='z'&&res[i+1]>='a')
 91         key=1;
 92         else if(res[i]=='+'&&res[i+1]=='+'&&res[i+2]=='+')
 93         key=0;
 94         else if(res[i]=='-'&&res[i+1]=='-'&&res[i+2]=='-')
 95         key=1;
 96    }

 97        
 98        
 99   printf("value = %d\n",sum);
100   for(int i = 0 ; i < 26 ; i ++ ){
101    if(mark[i]!=0)
102    printf("%c = %d\n",i+'a',zd[i]);
103    }

104    }

105 //   while(1);
106return 0 ; 
107}

108
109

你可能感兴趣的:(PKU 3337 Expression Evaluator)