cho.sh
Notes
Loading...

Can You Do Arithmetic?

Time limit

2s

Memory limit

128 MB

Problem

You are given an arithmetic expression. If the expression can be evaluated, print its value. If the expression is invalid or any division by zero occurs during evaluation, treat it as impossible to evaluate.

In this problem, an expression is an <expr> defined by the following grammar. The grammar has no unary + or unary -.

<digit> = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'<number> = <digit> | <number> <digit><expr> = <number> | <expr> '+' <expr> | <expr> '-' <expr> | <expr> '*' <expr> | <expr> '/' <expr> | '(' <expr> ')'

Input

The first line contains the expression to evaluate. The expression consists only of characters from ()*/+-0123456789, and its length is at most 1,000.

Output

Print the value of the expression. If the expression is invalid and cannot be evaluated, print ROCK.

  • Evaluate the expression using the usual operator precedence: expressions inside parentheses first, then multiplication and division, then addition and subtraction.
  • Operators with the same precedence are evaluated from left to right.
  • If the given expression satisfies the grammar above, then every division either has an integer quotient or has divisor 0. If a division with divisor 0 occurs, the expression is considered invalid.
<digit> = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'<number> = <digit> | <number> <digit><expr> = <number> | <expr> '+' <expr> | <expr> '-' <expr> | <expr> '*' <expr> | <expr> '/' <expr> | '(' <expr> ')'