表达试求值(可完成HDU4192)

  1 #include <iostream>

  2 #include <cstring>

  3 #include <cstdio>

  4 #include <map>

  5 #include <algorithm>

  6 using namespace std;

  7 const int Ni = 2000;

  8 class calcu

  9 {

 10 public:

 11     //传入表达试

 12     void init(char *str)

 13     {

 14         s=str;

 15         nc=0;len=strlen(s);

 16         build_tree(0,len);

 17     }

 18     //代入数值计算

 19     double calc(int *arr)

 20     {

 21         int i,j,k;

 22         //注意字母是小写

 23         for(k=i=0;i<len;i++) if(s[i]>='a'&&s[i]<='z')

 24             ch[k++]=s[i];

 25         sort(ch,ch+k);

 26         for(j=i=0;i<k;i++) if(ch[j]!=ch[i]) ch[++j]=ch[i];

 27         m.clear();

 28         for(i=0;i<=j;i++)

 29             m[ch[i]]=arr[i];

 30         top=-1;

 31         query(1);

 32         return shu[0];

 33     }

 34 private:

 35     map<char,double> m;

 36     int lch[Ni],rch[Ni];

 37     char op[Ni],*s,ch[Ni];

 38     int top,nc,len;

 39     double shu[Ni];

 40     int build_tree(int x,int y)

 41     {

 42         int i,u,c1=-1,c2=-1,p=0;

 43         if(y-x==1)

 44         {

 45             u=++nc;

 46             lch[u]=rch[u]=0;

 47             op[u]=s[x];

 48             return u;

 49         }

 50         for(i=x; i<y; i++)

 51         {

 52             switch(s[i])

 53             {

 54             case '(':p++;break;

 55             case ')':p--;break;

 56             case '+':

 57             case '-':if(!p) c1=i;

 58                 break;

 59             case '*':

 60             case '/':if(!p) c2=i;

 61                 break;

 62             }

 63         }

 64         if(c1<0) c1=c2;

 65         if(c1<0) return build_tree(x+1,y-1);

 66         u=++nc;

 67         lch[u]=build_tree(x,c1);

 68         rch[u]=build_tree(c1+1,y);

 69         op[u]=s[c1];

 70         return u;

 71     }

 72     void query(int i)

 73     {

 74         double ans;

 75         if(lch[i])

 76             query(lch[i]);

 77         if(rch[i])

 78             query(rch[i]);

 79         if(op[i]=='+')

 80         {

 81             ans=shu[top]+shu[top-1];

 82             top--;

 83             shu[top]=ans;

 84             return;

 85         }

 86         if(op[i]=='-')

 87         {

 88             ans=shu[top-1]-shu[top];

 89             top--;

 90             shu[top]=ans;

 91             return;

 92         }

 93         if(op[i]=='*')

 94         {

 95             ans=shu[top-1]*shu[top];

 96             top--;

 97             shu[top]=ans;

 98             return;

 99         }

100         if(op[i]=='/')

101         {

102             ans=shu[top-1]/shu[top];

103             top--;

104             shu[top]=ans;

105             return;

106         }

107         shu[++top]=m[op[i]];

108     }

109 }qr;

110 int arr[20];

111 char s[Ni];

112 int main()

113 {

114     int i,n;

115     while(scanf("%d",&n),n)

116     {

117         for(i=0;i<n;i++) scanf("%d",arr+i);

118         scanf("%s",s);

119         qr.init(s);

120         printf("%lf\n",qr.calc(arr));

121     }

122     return 0;

123 }

 

计算a=3,b=2,c=5时表达试a+b+(a*c+c-a)-c-b的值

输入样列:

3
3 2 5
a+b+(a*c+c-a)-c-b
输出:
15

你可能感兴趣的:(HDU)