zoukankan      html  css  js  c++  java
  • nyoj305 表达式求值

     1 #include<stdio.h>
     2 char s[301],cstack[120];
     3 int dstack[80];
     4 int i,ctop,dtop;
     5 int Operate(int a,char theta,int b)
     6 {
     7     switch(theta){
     8         case 'd': return a+b;
     9         case 'n': return a>b?b:a;
    10         case 'x': return a>b?a:b;
    11     }
    12 }
    13 int EvaluateExpression()
    14 {
    15     char theta;
    16     int a,b,k;
    17     do{    
    18         if(s[i]>'9'||s[i]<'0'){
    19             if(s[i]=='a'||s[i]=='m') i+=2;
    20             if(s[i]==')'){
    21                 --ctop;
    22                 theta=cstack[--ctop];
    23                 a=dstack[--dtop];
    24                 b=dstack[--dtop];
    25                 dstack[dtop++]=Operate(a,theta,b);
    26             }else if(s[i]!=',') cstack[ctop++]=s[i];
    27             ++i;
    28         }
    29         while(s[i]<='9'&&s[i]>='0'){
    30             for(k=0;s[i]!=','&&s[i]!=')'&&s[i]<='9'&&s[i]>='0';++i)
    31                 k=10*k+s[i]-'0';
    32             dstack[dtop++]=k;
    33             if(s[i]==',') ++i;
    34         }
    35     }while(ctop);
    36     return dstack[0];
    37 }
    38 int main()
    39 {
    40     int N;
    41     scanf("%d",&N);
    42     while(N--){
    43         i=ctop=dtop=0;
    44         scanf("%s",s);
    45         printf("%d\n",EvaluateExpression());
    46     }
    47     return 0;
    48 }
    49         
    50         

    纠结了半天,还好最终AC了,但是还有一点不明白,就是30行的限制数字的那个条件,我感觉不要也行,但提交总是WA,加上之后就AC了,为什么啊?求大神指点!!!

  • 相关阅读:
    IIS7最大上传附件大小配置
    数据表的identity
    服务器×××上的MSDTC不可用解决办法——Orchard(转)
    IOS项目中使用全局变量
    九宫格解锁的实现
    UINavigationControlle类详解
    jar仓库
    django安装
    django入门
    值得阅读的pyhon文章
  • 原文地址:https://www.cnblogs.com/shihuajie/p/2621412.html
Copyright © 2011-2022 走看看