uva 327

#include<iostream>
#include<string>
#include<cctype>
#include<map>
#include<cstring>
#include<iterator>
using namespace std;
int main()
{
    string sa;
    while(getline(cin,sa))
    {
        cout<<"Expression: "<<sa<<endl;
        int k,i,j,flag=0,sum=0;
        string temp="";
        for(int p=0;p<sa.length();p++)
        {
            if(!isspace(sa[p]))
            {
                temp=temp+sa[p];
            }
        }
        temp.insert(temp.length(),"A");
        map<char,int>m;
        if(!isalpha(temp[0]))
        {
            for(k=0;k<temp.length();k++)
            {
                if(isalpha(temp[k]))
                {
                    m.insert(make_pair(temp[k],temp[k]-'a'+1));
                    if(temp[0]=='+')
                    {
                        sum++;
                        m[temp[k]]++;
                    }
                    else
                    {
                        sum--;
                        m[temp[k]]--;
                    }
                    flag=k;
                    break;
                }
            }
        }
        if(flag==0)
        m.insert(make_pair(temp[0],temp[0]-'a'+1));
        sum+=temp[flag]-'a'+1;
        for(i=flag;i<temp.length();i++)
        {
            if(isalpha(temp[i]))
            {
                for(j=i+1;j<temp.length();j++)
                {
                    if(isalpha(temp[j]))
                    {
                        m.insert(make_pair(temp[j],temp[j]-'a'+1));
                        string s=temp.substr(i+1,j-i-1);
                        if(s=="+")
                        {
                            sum+=temp[j]-'a'+1;
                        }
                        else if(s=="-")
                        {
                            sum-=temp[j]-'a'+1;
                        }
                        else if(s=="++")
                        {
                            m[temp[i]]++;
                        }
                        else if(s=="--")
                        {
                            m[temp[i]]--;
                        }
                        else if(s=="--+")
                        {
                            m[temp[i]]--;
                            sum+=temp[j]-'a'+1;
                        }
                        else if(s=="+--")
                        {
                            m[temp[j]]-=1;
                            sum+=(temp[j]-'a'+1)-1;
                        }
                        else if(s=="++-")
                        {
                            m[temp[i]]++;
                            sum-=temp[j]-'a'+1;
                        }
                        else if(s=="-++")
                        {
                            m[temp[j]]++;
                            sum-=temp[j]-'a'+1;
                            sum--;
                        }
                        else if(s=="++-++")
                        {
                            m[temp[i]]++;
                            m[temp[j]]++;
                            sum=sum-(temp[j]-'a'+2);
                        }
                        else if(s=="--+--")
                        {
                            m[temp[i]]--;
                            m[temp[j]]--;
                            sum=sum+(temp[j]-'a'+1);
                            sum--;
                        }
                        break;
                    }
                }
            }
        }
        cout<<"    value = "<<sum<<endl;
        map<char,int>::iterator it;
        for(it=m.begin();it!=m.end();it++)
        {
            if(it->first!='A')
            cout<<"    "<<it->first<<" = "<<it->second<<endl;
        }
        }
        return 0;
}

 

posted @ 2012-05-15 00:16  open your eyes  阅读(189)  评论(0)    收藏  举报