#C12879. Evaluate Mathematical Expressions in Strings
Evaluate Mathematical Expressions in Strings
Evaluate Mathematical Expressions in Strings
You are given a string that contains one or more mathematical expressions enclosed in {{
and }}
. Your task is to evaluate each expression and replace it with its result. If an expression cannot be evaluated (for example, because of division by zero or a syntax error), replace that expression with the text Error
.
Note: The expression may include basic arithmetic operations (addition, subtraction, multiplication, and division) and parentheses. The result should be printed exactly as evaluated (for instance, 5.0
for a float result from 10 / 2
).
inputFormat
The input consists of a single line read from standard input. This line is a string that may contain one or more segments of the form {{ expression }}
, where expression is a valid arithmetic expression.
outputFormat
Output the resulting string on standard output after replacing every {{ expression }}
by its evaluated result. For expressions that cannot be evaluated, replace them with Error
.
The sum is {{1 + 1}}.
The sum is 2.