#C12723. Evaluate Mathematical Expressions

    ID: 42182 Type: Default 1000ms 256MiB

Evaluate Mathematical Expressions

Evaluate Mathematical Expressions

You are given several mathematical expressions in string format. Your task is to evaluate each expression and output the result. The expressions are simple arithmetic expressions that involve two operands and one operator (either addition +, subtraction -, multiplication *, or division /). If an expression is invalid or involves division by zero, output None for that expression.

The result should be formatted as follows: if the result is a whole number, print it as an integer. Otherwise, print the floating-point result (e.g. 4.0). Input will be given via standard input and the answers should be printed on standard output, one result per line.

Note: An expression is valid only if it exactly consists of three tokens (operand, operator, operand). Extra tokens or malformed expressions should be treated as invalid.

For example, given the expressions:
3 + 2
10 / 0
5 * 6
a + b

The output should be:
5
None
30
None

In addition, if the expression contains extra whitespace, it should not affect the result.

inputFormat

The first line of input contains an integer n, representing the number of expressions to evaluate. The following n lines each contain one arithmetic expression.

Example Input:

4
3 + 2
5 * 6
10 - 3
8 / 2

outputFormat

For each arithmetic expression provided in the input, output its evaluated result on a new line. If the expression is invalid or leads to a division by zero, print None instead.

Example Output:

5
30
7
4.0
## sample
4
3 + 2
5 * 6
10 - 3
8 / 2
5

30 7 4.0

</p>