#C1114. Evaluating Mathematical Expressions
Evaluating Mathematical Expressions
Evaluating Mathematical Expressions
You are given a string S
which represents a mathematical expression. The expression contains only positive integers, addition (+) and subtraction (-) operators. Whitespaces may appear anywhere in the string and should be ignored.
Your task is to compute the value of the expression.
In mathematical terms, if the expression is processed as:
\( total = \sum_{i=1}^{n} (sign_i \times number_i) \)
where sign is determined by the preceding operator (default is positive), then total should be computed accordingly.
If the input string is empty or contains only whitespace, the result is 0
.
inputFormat
The input consists of a single line from stdin
which is the expression string S
. This string may include spaces, digits, and the operators '+' and '-'.
outputFormat
Output a single integer which is the result of evaluating the expression. The output should be written to stdout
.
3+2-1
4