327-evaluating Simple C Expressions
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_ problem&problem=263
The task in this problem was to evaluate a sequence of simple C expressions, buy you need not know C to solve the proble M! Each of the expressions would appear on a line by itself and would contain no more than. The expressions to be evaluated would contain only simple integer variables and a limited set of operators; There would be no constants in the expressions. There are variables which may appear by our simple expressions, namely those with the Namesa through Z (lower-case lett ERs only). At the beginning of evaluation of expression, variables would have the integer values 1 through, Respectiv Ely (That is,a = 1, b = 2, ..., n = m, o =, ..., z = 26). Each variable would appear at most once in a expression, and many variables may is used at all.
The operators, appear, expressions include the binary (Two-operand) + and-, with the usual interpretation. Thus the expression A + c-d + B has the value 2 (computed AS1 + 3-4 + 2). The only other operators, appear in Expressions are++ and--. These are are unary (one-operand) operators, and may appear before or after any variable. When the++ operator appears before a variable, that variable ' s value are incremented (by one) before the variable ' s value I s 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. When the++ operator appears after the A variable, that variable was incremented (again, by one) over its value is used to Dete Rmine the value of the entire expression. Thus the value of the expressionc++-B is 1, but C was incremented after the complete expression is evaluated; Its value would 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 Variablesc and B has 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 which increments the value of each such variable, and remove the++ operator from before That's variable in the expression.
In a similar manner, identify all variable that has a + + operator after it. Write a simple assignment statement that increments's value of each of, and remove the++ operator from 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 into step 1, and written before those Tten 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 th At order.
Using This approach, evaluating the expression ++a + b++ are 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 is to read expressions, one per line, until the "end of" the file is reached. Display each expression exactly as it is read, then display the value of the entire expression, and on separate lines, th E value of each variable on the expression was evaluated. Do isn't display the value of variables that were isn't used in the expression. The samples shown below illustrate the desiredexact output format.
Blanks are to is ignored in evaluating expressions, and for are assured that ambiguous expressions (likea+++b) Cause it could be treated as a++ + B or a + ++b) won't is appear in the input. Likewise, + + or--operators'll never appear both before and after a single variable. Thus Expressions like++a++ is not being in the input data.
Sample Input
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/