#C1776. Validated Mathematical Expression Evaluator

    ID: 45018 Type: Default 1000ms 256MiB

Validated Mathematical Expression Evaluator

Validated Mathematical Expression Evaluator

You are given a string representing a mathematical expression that consists of single-digit non-negative integers (0-9) and the operators +, -, *, and /. Parentheses ( ) can be used to override the default operator precedence.

Your task is to validate the expression and then evaluate it. The expression is valid only if:

  • All characters belong to the allowed set (digits, operators, parentheses, and no other extraneous characters).
  • The parentheses are balanced.
  • The expression does not start or end with an operator and does not contain consecutive operators.

If the expression is valid, output its evaluated result. Otherwise, output Invalid Expression.

The evaluation follows the standard operator precedence rules and supports grouping with parentheses.

Note: Division is assumed to produce an integer result when valid.

For example, the expression \(3+(2\times2)-1/1\) evaluates to 6 and the expression \((8+2)/(3-1)\) evaluates to 5.

inputFormat

The input consists of a single line from stdin containing the expression string. The expression may include digits, operators (+, -, *, /) and parentheses. Leading and trailing whitespace should be ignored.

outputFormat

Output the evaluated result as per standard arithmetic computation if the expression is valid. Otherwise, output Invalid Expression to stdout.

## sample
3+(2*2)-1/1
6