POJ-3337 Expression Evaluator-表示式求值

kewlgrl發表於2015-08-22
Expression Evaluator
Time Limit: 2000MS   Memory Limit: 65536K
Total 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, though c 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

2
a+b
c+f--+--a

Sample Output

Expression: a+b
value = 3
a = 1
b = 2
Expression: c+f--+--a
value = 9
a = 0
c = 3
f = 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的心情真是難以言表。。。

相關文章