Calculator
Time limit1sMemory limit128 MB
Parse and evaluate fully parenthesized arithmetic expressions with big integers up to 90 digits, printing Error on overflow, negative results, or division by zero.
- Level
Medium4 of 10
- Topics
- String, Math, Recursion, Implementation
- Solved
- No attempts yet
Problem
A calculator works with numbers of at most digits long and computes expressions with the following syntax:
<expression> ::= <number> | (<expression><operator><expression>)
<operator> ::= + | - | * | /
<number> ::= an unsigned decimal integer number
The operators are: + (addition), - (subtraction), * (multiplication), and / (integer division, yielding the quotient of the division). The calculator aborts the evaluation in the following cases: 1) a division by zero; 2) the result of some operation is negative or exceeds digits; 3) an input number exceeds digits.
Write a program that simulates the calculator. For each expression read from the input, the program prints the value of the expression starting from the beginning of an output line. If the calculator aborts the evaluation of the expression, the word Error is printed. An expression can contain at most operators and is syntactically correct. White spaces are used freely in input that terminates with an end of file.