表达式求值 栈

 
#include<stdio.h>
#include<string.h>
int count(int x,int y,char z)
   { if(z=='d')
     return x+y;
     if(z=='a')
     return x>y?x:y;
     if(z=='i')
     return x<y?x:y;
  
   }
char a[330],b[330];
int  c[330],btop,ctop,sum;
int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
     int i,m;
     btop=ctop=1;
     memset(a,0,sizeof(a));
     memset(b,0,sizeof(b));
     memset(c,0,sizeof(c));
     scanf("%s",a);
     m=strlen(a);
     for(i=0;i<m;)
       {
         if(a[i]>='a'&&a[i]<='z')
         {
           if(a[i+1]=='d')
              b[btop++]='d';
           else if(a[i+1]=='a')
              b[btop++]='a';
           else if(a[i+1]=='i')
              b[btop++]='i';
           i+=2;
         }
         else if(a[i]=='('||a[i]==',')
              i++;
         else if(a[i]>='0'&&a[i]<='9')
            { sum=0;
              while(a[i]>='0'&&a[i]<='9')
              { sum=sum*10+a[i]-'0';
                i++;
              }
              c[ctop++]=sum;
            }
         else if(a[i]==')')
          {
              sum=count(c[--ctop],c[--ctop],b[--btop]);
              c[ctop++]=sum;
              i++;
          }
         
       }
      printf("%d\n",c[1]);
     }
    return 0;           
}
       

posted @ 2013-08-12 21:56  hpu张亚飞  阅读(211)  评论(0编辑  收藏  举报