uva 327 - Evaluating Simple C Expressions
2014-08-15 11:13
393 查看
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, 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:
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 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.
Sample Input
a + b b - z a+b--+c++ c+f--+--a f-- + c-- + d-++e
Sample Output
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 <stack> #include <cstring> #include <cstdio> #include <string> #include <algorithm> #include <queue> #include <set> #include <map> #include <fstream> #include <stack> #include <list> #include <sstream> #include <cmath> using namespace std; #define ms(arr, val) memset(arr, val, sizeof(arr)) #define mc(dest, src) memcpy(dest, src, sizeof(src)) #define N 200 #define INF 0x3fffffff #define vint vector<int> #define setint set<int> #define mint map<int, int> #define lint list<int> #define sch stack<char> #define qch queue<char> #define sint stack<int> #define qint queue<int> char res , ori ; int val , tag , exist , ans;//tag存放--|++在后面的,--为-1,++为1 int skip()//清除空格 { mc(res, ori); ms(exist, 0); int n = 0; for (int i = 0; res[i]; i++) { if (res[i] != ' ') { res[n++] = res[i]; } if (islower(res[i])) { exist[res[i]] = 1; } } res = '\0'; for (int i = 'a', j = 1; i <= 'z'; i++, j++) { tag[i] = 0; val[i] = j; } return n; } bool judge(int i, int j) { return (res[i] == res[j] && (res[i] == '-' || res[i] == '+')); } void step1()//清除--|++在前的 { int n = 0; for (int i = 0; res[i]; i++) { if (islower(res[i]) && i > 1 && judge(i - 1, i -2)) { if (res[i - 1] == '-') { val[res[i]]--; } else { val[res[i]]++; } n -= 2; } res[n++] = res[i]; } res = '\0'; } void step2()//清除--|++在后的 { int n = 0; for (int i = 0; res[i]; i++) { res[n++] = res[i]; if (islower(res[i]) && res[i + 1] && res[i + 2] && judge(i + 1, i + 2)) { if (res[i + 1] == '-') { tag[res[i]] = -1; } else { tag[res[i]] = 1; } i += 2; } } res = '\0'; } void step3() { step1(); step2(); ans = val[res[0]]; for (int i = 1; res[i]; i += 2) { if (res[i] == '-') { ans -= val[res[i + 1]]; } else { ans += val[res[i + 1]]; } } } void input() { printf("Expression: %s\n", ori); printf(" value = %d\n", ans); for (int i = 'a'; i <= 'z'; i++) { if (exist[i]) { printf(" %c = %d\n", i, val[i] + tag[i]); } } } int main() { while (gets(ori)) { if(skip()) { step3(); input(); } } return 0; }
相关文章推荐
- uva_327-Evaluating Simple C Expressions
- UVA 327 -Evaluating Simple C Expressions(栈)
- uva 327 Evaluating Simple C Expressions
- uva 327 - Evaluating Simple C Expressions
- UVA 327 Evaluating Simple C Expressions
- UVA 327 - Evaluating Simple C Expressions(枚举就OK)
- UVa 327 Evaluating Simple C Expressions (模拟)
- uva 327 Evaluating Simple C Expressions(遍历)
- UVa 327 - Evaluating Simple C Expressions
- UVa 327 - Evaluating Simple C Expressions
- UVA 327 Evaluating Simple C Expressions
- 数据结构 uva-327-Evaluating Simple C Expressions
- UVA_327_Evaluating Simple C Expressions
- UVa 327 - Evaluating Simple C Expressions
- UVa 327 - Evaluating Simple C Expressions
- uva 327 - Evaluating Simple C Expressions
- UVA - 327 Evaluating Simple C Expressions
- uva 327--Evaluating Simple C Expressions
- uva 327 - Evaluating Simple C Expressions
- uva_327_Evaluating Simple C Expressions