The task in this problem is to evaluate a sequence of simple C expressions, buy you need not know C to solve the problem! Each of the expressions will appear on a line by itself and will contain no more than 110 characters. 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 which may appear in our simple expressions, namely those with the names a through z (lower-case letters only). At the beginning of evaluation of each expression, these 26 variables will have the integer values 1 through 26, respectively (that is, a = 1, b = 2, ..., n = 14, o = 15, ..., z = 26). Each variable will appear at most once in an expression, and many variables may not be used at all.
The operators that may appear in expressions include the binary (two-operand) + and -, with the usual interpretation. Thus the expressiona + c - d + b has the value 2 (computed as 1 + 3 - 4 + 2). The only other operators that may appear in expressions are ++ and--. These are unary (one-operand) operators, and may appear before or after any variable. When the ++ operator appears before a variable, that variable's value is incremented (by one) before the variable's value is used in determining the value of the entire expression. Thus the value of the expression ++c - b is 2, with c being incremented to 4 prior to evaluating the entire expression. When the ++operator appears after a variable, that variable is incremented (again, by one) after its value is used to determine the value of the entire expression. Thus the value of the expression c++ - b is 1, but c is incremented after the complete expression is evaluated; its value will still be 4. The -- operator can also be used before or after a variable to decrement (by one) the variable; its placement before or after the variable has the same significance as for the ++ operator. Thus the expression --c + b-- has the value 4, with variables c and b having the values 2 and 1 following the evaluation of the expression.
Here's another, more algorithmic, approach to explaining the ++ and -- operators. We'll consider only the ++ operator, for brevity:
Using this approach, evaluating the expression ++a + b++ is equivalent to computing
where expression would receive the value of the complete expression.
Your program is to read expressions, one per line, until the end of the file is reached. Display each expression exactly as it was read, then display the value of the entire expression, and on separate lines, the value of each variable after the expression was evaluated. Do not display the value of variables that were not used in the expression. The samples shown below illustrate the desired exact output format.
Blanks are to be ignored in evaluating expressions, and you are assured that ambiguous expressions like a+++b (ambiguous because it could be treated as a++ + b or a + ++b) will not appear in the input. Likewise, ++ or -- operators will never appear both before and after a single variable. Thus expressions like ++a++ will not be in the input data.
a + b b - z a+b--+c++ c+f--+--a f-- + c-- + d-++e
Expression: a + b value = 3 a = 1 b = 2 Expression: b - z value = -24 b = 2 z = 26 Expression: a+b--+c++ value = 6 a = 1 b = 1 c = 4 Expression: c+f--+--a value = 9 a = 0 c = 3 f = 5 Expression: f-- + c-- + d-++e value = 7 c = 2 d = 4 e = 6 f = 5
这个题不用解释,看样例就懂。处理比较操蛋……
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<sstream> #include<vector> #include<stack> using namespace std; int main() { string line; int table[200]; while(getline(cin,line)) { cout<<"Expression: "<<line<<endl; bool used[200]={0}; istringstream in(line); string s,exp; while(in>>s)exp+=s; for(int i='a',j=1; i<='z'; i++) table[i]=j++; for(int i=2;i<exp.size();i++) if(islower(exp[i])&&exp[i-1]==exp[i-2]) { if(exp[i-1]=='-')table[exp[i]]--; else table[exp[i]]++; exp[i-1]=exp[i-2]=0; } string ts; for(int i=0;i<exp.size();i++) if(exp[i])ts+=exp[i]; exp=ts; string plus; string need_plus; string need_sub; for(int i=0;i<exp.size();i++) if(islower(exp[i])&&exp[i+1]==exp[i+2]) { if(exp[i+1]=='+')need_plus+=exp[i]; else need_sub+=exp[i]; exp[i+1]=exp[i+2]=0; plus+=exp[i]; } ts.clear(); for(int i=0;i<exp.size();i++) if(exp[i])ts+=exp[i]; exp=ts; int ans=table[exp[0]]; string alph; alph+=exp[0]; used[exp[0]]=1; for(int i=1;i<exp.size();i++) if(exp[i]=='-') ans-=table[exp[i+1]]; else if(exp[i]=='+') ans+=table[exp[i+1]]; else if(!used[exp[i]])alph+=exp[i],used[exp[i]]=1; cout<<" value = "<<ans<<endl; for(int i=0;i<need_plus.size();i++) table[need_plus[i]]++; for(int i=0;i<need_sub.size();i++) table[need_sub[i]]--; sort(alph.begin(),alph.end()); for(int i=0;i<alph.size();i++) cout<<" "<<alph[i]<<" = "<<table[alph[i]]<<endl; } return 0; }