#C12486. Evaluate Mathematical Expressions

    ID: 41918 Type: Default 1000ms 256MiB

Evaluate Mathematical Expressions

Evaluate Mathematical Expressions

You are given n mathematical expressions. For each expression, you must evaluate its numerical value. If the expression is invalid or if a division by zero occurs, output Error.

The expressions may contain basic operators: addition (+), subtraction (-), multiplication (*), division (/) and parentheses ( ).

For example, consider the following expressions:

  • Input: 3 + 5 → Output: 8
  • Input: 8 / 2 → Output: 4.0
  • Input: 6 / 0 → Output: Error

The arithmetic follows the standard operator precedence and associativity rules. A valid expression is one that can be fully parsed according to the grammar below in \(\LaTeX\) format:

\(\text{Expression} \to \text{Term} \; ( ( + | - )\; \text{Term} )^*\)

\(\text{Term} \to \text{Factor} \; ( ( * | / )\; \text{Factor} )^*\)

\(\text{Factor} \to ( \text{Number} ) \; | \; ( \; \text{Expression} \;) \)

inputFormat

The first line of input contains an integer n, the number of expressions.

Each of the following n lines contains a non-empty string representing an arithmetic expression.

All input should be read from stdin.

outputFormat

For each expression, output the evaluated result on a new line. If the expression is invalid or a division by zero error occurs, output Error instead.

All outputs should be printed to stdout.

## sample
4
3 + 5
10 - 3 * 2
8 / 4 + 1
7 + (3 * 2)
8

4 3.0 13

</p>