这题初看起来被吓到了,以为要写成运算符重载,后来发现其实很水,呵呵(但是某人虽然在poj过了,但是却在tojWA了5次,实在不解,呵呵)
思路:接入字符串,去掉空格,从头到尾扫,遇到字母就检测它的前后两位有没有++(--)如果有进行处理,然后看它的后(前)面第3为是+(-)就加(减)到和sum里;用一个三维数组v[26][3]记录状态和值。代码写的比较繁琐,呵呵,不想改了,易懂,嘿嘿。
  1#include<iostream>
  2using namespace std;
  3char a[500];
  4char b[500];
  5int v[26][3];
  6int l;
  7bool inside(int a, int b)
  8{
  9    if(a>=0 && a<&& b>=0 && b<l)
 10        return true;
 11    else
 12        return false;
 13}

 14bool jia(int c, int b)
 15{
 16    if(a[c]=='+' && a[b]=='+')
 17        return true;
 18    else
 19        return false;
 20}

 21bool jian(int c, int b)
 22{
 23    if(a[c]=='-' && a[b]=='-')
 24        return true;
 25    else
 26        return false;
 27}

 28int main()
 29{
 30    int n,i,j,k3,k4,k1,k2;
 31    char c;
 32    scanf("%d",&n);
 33    scanf("\n");
 34    while(n--){
 35        
 36        int sum=0;
 37        for(i=0; i<26; i++)
 38            v[i][0]=i+1,v[i][1]=i+1,v[i][2]=0;
 39
 40        memset(a, 0sizeof(a));
 41        memset(b, 0sizeof(b));
 42        i=0;j=0;
 43        while(scanf("%c",&c) && c!='\n'){
 44            b[j]=c;
 45            j++;
 46            if(c!=' ')
 47                a[i]=c,i++;
 48        }

 49
 50        
 51        
 52        l=i;int count=0;
 53        for(i=0; i<l; i++)
 54            if(a[i]<='z' && a[i]>='a'){
 55                count++;
 56                v[a[i]-'a'][2]=1;
 57                k1=i+1;
 58                k2=i+2;
 59                k3=i-1;
 60                k4=i-2;
 61                if(count==1){
 62                if(inside(k1,k2) && jia(k1,k2))
 63                    v[a[i]-'a'][0]++;
 64                else if(inside(k3,k4) && jia(k3,k4))
 65                    v[a[i]-'a'][0]++,v[a[i]-'a'][1]++;
 66                else if(inside(k1,k2) && jian(k1,k2))
 67                    v[a[i]-'a'][0]--;
 68                else if(inside(k3,k4) && jian(k3,k4))
 69                    v[a[i]-'a'][0]--,v[a[i]-'a'][1]--;
 70
 71                sum+=v[a[i]-'a'][1];}

 72                else{
 73                    if(inside(k1,k2) && jia(k1,k2)){
 74                        v[a[i]-'a'][0]++;
 75                        if(a[i-1]=='+')
 76                            sum+=v[a[i]-'a'][1];
 77                        else
 78                            sum-=v[a[i]-'a'][1];
 79                    }

 80                    else if(inside(k3,k4) && jia(k3,k4)){
 81                        v[a[i]-'a'][0]++,v[a[i]-'a'][1]++;
 82                        if(a[i-3]=='+')
 83                            sum+=v[a[i]-'a'][1];
 84                        else
 85                            sum-=v[a[i]-'a'][1];
 86                    }

 87                    else if(inside(k1,k2) && jian(k1,k2)){
 88                        v[a[i]-'a'][0]--;
 89                        if(a[i-1]=='+')
 90                            sum+=v[a[i]-'a'][1];
 91                        else
 92                            sum-=v[a[i]-'a'][1];
 93                    }

 94                    else if(inside(k3,k4) && jian(k3,k4)){
 95                        v[a[i]-'a'][0]--,v[a[i]-'a'][1]--;
 96                        if(a[i-3]=='+')
 97                            sum+=v[a[i]-'a'][1];
 98                        else
 99                            sum-=v[a[i]-'a'][1];
100                    }

101                    else
102                    {
103                    if(a[i-1]=='+')
104                        sum+=v[a[i]-'a'][1];
105                    else
106                        sum-=v[a[i]-'a'][1];
107                    }

108                }

109            }

110        printf("Expression: %s\n",b);;
111        printf("value = %d\n",sum);
112        for(i=0; i<26; i++)
113            if(v[i][2]==1)
114                printf("%c = %d\n",'a'+i,v[i][0]);
115    
116    }

117
118return 0;
119}