#K63592. Check Mathematical Expressions
Check Mathematical Expressions
Check Mathematical Expressions
You are given a series of simple arithmetic expressions. Each expression is composed only of non‐negative integers and the binary operators +
, -
, *
, and /
. There are no spaces in the expressions. An expression is considered valid if and only if:
- It contains only digits and the allowed operators.
- Operators do not appear consecutively.
- During evaluation, no division by zero occurs.
If the expression is valid, output Valid
. If an expression contains any illegal characters or if two operators appear consecutively, output Invalid
. If the expression triggers a division by zero during evaluation, output Division by zero
.
For example, consider the following expressions:
3+5
evaluates correctly →Valid
8/0
attempts a division by zero →Division by zero
5+*7
has consecutive operators →Invalid
Note: All evaluation is done using standard operator precedence. All expressions are assumed to be written without spaces.
inputFormat
The first line of input contains a single integer T (T ≥ 1) which indicates the number of expressions. The following T lines each contain a single arithmetic expression.
For example:
3 3+5 8/0 5+*7
outputFormat
For each expression, output the result on a new line. The result is one of the following strings:
Valid
if the expression is mathematically correct.Invalid
if the expression contains non-digit characters or consecutive operators or any format issues.Division by zero
if the expression attempts to divide by zero during evaluation.
For the sample input above, the output would be:
Valid Division by zero Invalid## sample
3
3+5
8/0
5+*7
Valid
Division by zero
Invalid
</p>