UVa 327 - Evaluating Simple C Expressions

327 - Evaluating Simple C Expressions

 * http://acm.uva.es/p/v3/327.html

Summary
Evaluate a C expression consisting only of addition, subtraction, ++, and --.

Optimizations

 * Keep two copies of the expression. One (which must be pristine) for the output and one that you can play with.
 * Try prepending a + to the expression.
 * Keep the lax constraints in mind: each variable appears only once, no ambiguity, etc.

Input
a + b b - z a+b--+c++ c+f--+--a f-- + c-- + d-++e

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