327 – Evaluating Simple C ExpressionsTime limit: 3.000 seconds |
Evaluating Simple C Expressions |
The operators that may appear in expressions include the binary (two-operand) + and -, with the usual interpretation. Thus the expression a + 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, withc 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:
- Identify each variable that has a ++ operator before it. Write a simple assignment statement that increments the value of each such variable, and remove the ++ operator from before that variable in the expression.
- In a similar manner, identify each variable that has a ++ operator after it. Write a simple assignment statement that increments the value of each of these, and remove the ++ operator from after that variable in the expression.
- Now the expression has no ++ operators before or after any variables. Write the statement that evaluates the remaining expression after those statements written in step 1, and before those written in step 2.
- Execute the statements generated in step 1, then those generated in step 3, and finally the one generated in step 2, in that order.
Using this approach, evaluating the expression ++a + b++ is equivalent to computing
- a = a + 1 (from step 1 of the algorithm)
- expression = a + b (from step 3)
- b = b + 1 (from step 2)
where expression would receive the value of the complete expression.
Input and Output
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 desiredexact 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.
Sample Input
1 2 3 4 5 |
a + b b - z a+b--+c++ c+f--+--a f-- + c-- + d-++e |
Sample Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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 |
这道题目大概就是模拟一下编译器里面的加减自加自减吧,不过只有26个数,每个数也只出现一次,并且某些容易引起语意矛盾的表达式(比如a+++b)也不会在输入数据里面出现,还有也不会出现–a++这种东西。
首先自然就是去掉输入表达式里面的空格,然后在式子前面加一个「+」然后再对每个数进行计算,比如计算 a++-b++-c–的时候就可以转换成 +a++-b++-c– 然后就可以划分成:[+a++][-b++][-c–]三个部分,这样就比较有利于分析了,最后要注意的是输入空行的时候也必须有输出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
/******************************************************** * File Name: uvaoj327.cpp * Author: razrLeLe * Mail: razrlele@outlook.com * Homepage: https://yueyu.io * Created Time: Sun 08 Feb 2015 11:02:57 AM CST ******************************************************/ #include "vector" #include "set" #include "deque" #include "queue" #include "algorithm" #include "functional" #include "iostream" #include "cstdio" #include "cmath" #include "cstdlib" #include "string" #include "cstring" #include "string.h" #include "map" #include "cctype" #include "list" #include "stack" using namespace std; #define INF 0x3f3f3f3f #define LOCAL bool used[200]; int num[200]; char exprTemp[300]; char expr[300]; void init() { for(int i = (int)'a'; i <= (int)'z'; i++) { num[i] = i-'a'+1; used[i] = false; memset(expr, 0, sizeof(expr)); } return ; } int main() { #ifdef LOCAL freopen("/home/razrlele/build/data.txt", "r", stdin); freopen("/home/razrlele/build/out.txt", "w", stdout); #endif while(cin.getline(exprTemp, 300)) { init(); int j = 0; expr[j++] = '+'; int exprValue = 0; int exprLength = strlen(exprTemp); for(int i = 0; i < exprLength; i++) { if(exprTemp[i] != ' ') expr[j++] = exprTemp[i]; }//drop all space exprLength = strlen(expr); if(exprLength > 1) for(int i = 0; i < exprLength; i++) { if(!isalpha(expr[i+1])) { if(expr[i+1] == '+' && expr[i+2] == '+') { num[expr[i+3]]++; } else if(expr[i+1] == '-' && expr[i+2] == '-') { num[expr[i+3]]--; } if(expr[i] == '+') exprValue += num[expr[i+3]]; else exprValue -= num[expr[i+3]]; used[expr[i+3]] = true; i+=3; } else { if(expr[i] == '+') exprValue += num[expr[i+1]]; else exprValue -= num[expr[i+1]]; used[expr[i+1]] = true; i+=1; } if(expr[i+1] == '+' && expr[i+2] == '+') { num[expr[i]]++; i+=2; } else if(expr[i+1] == '-' && expr[i+2] == '-') { num[expr[i]]--; i+=2; } } printf("Expression: %s\n value = %d\n", exprTemp, exprValue); for(int i = (int)'a'; i <= (int)'z'; i++) { if(used[i]) printf(" %c = %d\n", i, num[i]); } } return 0; } |