#K66412. Valid Arithmetic Expression Checker
Valid Arithmetic Expression Checker
Valid Arithmetic Expression Checker
You are given an arithmetic expression as a string. Your task is to determine whether the expression is valid based on the following rules:
- The expression may only contain non‐negative integer digits (0-9), the operators +, -, *, /, and parentheses ( and ).
- The expression should not contain any spaces or invalid characters.
- Operators cannot appear consecutively, and the expression cannot start or end with an operator.
- Parentheses must be balanced and non-empty.
- Division by zero is not allowed (e.g. evaluating an expression that causes a division by zero makes it invalid).
For example, the expression 3+(4-5)*2
is valid, while 5++6
or 10/(5-5)
are invalid.
Your program should read the expression from standard input and print True
if it is valid, or False
otherwise.
inputFormat
The input consists of a single line containing the arithmetic expression to be validated. The expression does not contain any spaces.
outputFormat
Output a single line: True
if the expression is valid, and False
otherwise.
3+(4-5)*2
True
</p>