#C14112. Evaluate Mathematical Expression

    ID: 43726 Type: Default 1000ms 256MiB

Evaluate Mathematical Expression

Evaluate Mathematical Expression

You are given a mathematical expression in string form. The expression contains positive integers and the operators +, -, *, and /. Your task is to evaluate the expression while respecting the operator precedence. Note that the operator precedence is defined as follows: multiplication and division have higher precedence than addition and subtraction. All operations are performed from left to right considering their precedence.

If the expression contains any invalid character (i.e. any character other than digits, +, -, *, /, or whitespace), the program should output the error message Invalid character detected. Also, if a division by zero is attempted, the program should output Division by zero.

The formulas used are given in \( \LaTeX \) format as follows:

  • Operator Precedence: \( +, -: 1 \) and \( *, /: 2 \).
  • Evaluation: For two operands \(a\) and \(b\) and operator \(op\), the operation is defined by \(a\, op\, b\).

Read the expression from the standard input and write the result to the standard output. If the result is a whole number, output it as an integer; otherwise output the floating-point number.

inputFormat

Input is provided via standard input (stdin) as a single line string representing the mathematical expression. The expression may include whitespace.

outputFormat

Output the evaluated result to standard output (stdout). If the expression is invalid or attempts division by zero, output the respective error message.## sample

3+2
5