POJ-3337 Expression Evaluator-表达式求值

来源:互联网 发布:网络推广的岗位职责 编辑:程序博客网 时间:2024/06/05 02:33
Expression Evaluator
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 2027 Accepted: 616

Description

This problem is about evaluating some C-style expressions. The expressions to be evaluated will contain only simple integer variables and a limited set of operators; there will be no constants in the expressions. There are 26 variables in the program, named by lower case letters a through z. Before evaluation, the initial values of these variables area = 1,b = 2, ...,z = 26.

The operators allowed are addition and subtraction (binary + and -), with their known meaning. So, the expressiona +c -d + b has the value 2 (1 + 3 - 4 + 2). Additionally, ++ and –- operators are allowed in the input expression too, which are unary operators, and may come before or after variables. If the ++ operator comes before a variable, then that variable's value is increased (by one) before the variable's value is used in calculating the value of the whole expression. Thus the value of ++c -b is 2. When ++ comes after a variable, that variable is increased (by one) after its value is used to calculate the value of the whole expression. So, the value of thec ++ -b is 1, thoughc is incremented after the value for entire expression is computed; its value will be 4 too. The -- operator behaves the same way, except that it decreases the value of its operand.

More formally, an expression is evaluated in the following manner:

  • Identify every variable that are preceded by ++. Write an assignment statement for incrementing the value of each of them, and omit the ++ from before that variable in the expression.
  • Do similarly for the variables with ++ after them.
  • At this point, there is no ++ operator in the expression. Write a statement evaluating the remaining expression after the statements determined in step 1, and before those determined in step 2.
  • Execute the statements determined in step 1, then those written in step 3, and finally the one written in step 2.

This way, evaluating ++ a + b ++ is the same as computing a = a + 1, result = a + b, and b = b + 1.

Input

The first line of the input contains a single integer T which is the number of test cases, followed byT lines each containing the input expression for a test case. Ignore blanks in the input expression. Be sure that no ambiguity is in the input expressions (likea+++b). Similarly, ++ or -- operators do not appear both before and after one single variable (like ++a++). You may safely assume each variable appears only once in an expression.

Output

For each test case, write each expression as it appears in the input (exactly), then write the value of the complete expression. After this, on separate lines, write the value of each variable after evaluating the expression (write them in sorted order of the variable names). Write only the values of the variables that are used in the expressions. To find out about the output format, follow the style used in the sample output below.

Sample Input

2a+bc+f--+--a

Sample Output

Expression: a+bvalue = 3a = 1b = 2Expression: c+f--+--avalue = 9a = 0c = 3f = 5

Source

Tehran 2006 Preliminary

/*    *Copyright (c)2015,烟台大学计算机与控制工程学院    *All rights reserved.      *作    者:单昕昕    *完成日期:2015年8月22日    *版 本 号:v1.0        */ #include<iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>using namespace std;int main(){    int k,i,t,l,v[26],val,b,c,flag;    char s[1000];    cin>>t;//测试用例数    getchar();    while(t--)    {        bool occur[26];//标记字母是否用过        //以下作初始化        memset(occur,false,sizeof(occur));        for(i=0; i<26; ++i)            v[i]=i+1;        val=0;        b=1;        gets(s);        l=strlen(s);        cout<<"Expression: "<<s<<endl;//输出表达式        for(i=0; i<l; ++i)//去掉表达式中的空格!!!这个一定要有,一开始以为木有关系结果WA了11次!        {            if(s[i]==' ')            {                k=i;                for(; s[k]!='\0'; ++k)                    s[k]=s[k+1];                --i;            }        }        for(k=0; k<l; ++k)        {            //是运算符            if((s[k]=='+')||(s[k]=='-'))            {                if(((s[k]=='+'&&s[k+1]=='+')||(s[k]=='-'&&s[k+1]=='-'))&&(s[k+2]<='z'&&s[k+2]>='a'))//如果是++或--                {                    v[int(s[k+2]-'a')]+=(s[k]=='+'?1:-1);                    ++k;                    c=int(s[k+1]-'a');                    occur[c]=true;                }                else                    b=(s[k]=='+'?1:-1);//只是+或-            }            //是变量            else if(s[k]>='a'&&s[k]<='z')            {                flag=0;//标记                c=int(s[k]-'a');                val+=b*v[c];                if(!occur[c])//没用过                {                    flag=1;                    occur[c]=true;                }                if(flag==1&&((s[k+1]=='+'&&s[k+2]=='+')||(s[k+1]=='-'&&s[k+2]=='-')))//变量++或--                {                    v[c]+=(s[k+1]=='+'?1:-1);                    k+=2;                }            }        }        cout<<"value = "<<val<<endl;//输出各个值        for(i=0; i<26; ++i)            if(occur[i])                cout<<char('a'+i)<<" = "<<v[i]<<endl;    }    return 0;}

呵呵呵呵哒,WA11次的惨痛经历。
对着书上的提示看了半天写了半天,好不容易编译木有错误了。。结果一直WA的心情真是难以言表。。。
0 0
原创粉丝点击