Calculator P40155


Statement
 

pdf   zip

html

Your little sister has lost her pocket calculator just before an important exam. She asks you to lend her your portable computer and to write her a program to evaluate single expressions. Your fraternal love is so big that you immediately agree on both things.

The calculator that your sister wishes is very simple: expressions can only contain one-digit numbers and the four basic arithmetic operations (where divisions should be understood as integer divisions). Parentheses are also allowed. The calculator must honor the usual priority rules. All operators have left-to-right associativity (just like in C++). All expressions strictly adhere to the following syntax:

     
    expression  →  expression  +  term  |  expression  -  term  |  term          
term  →  term  *  factor  |  term  /  factor  |  factor          
factor  →  number  |  (  expression  )           
number  →  0   |   1   |   2   |   3   |   4   |   5   |   6   |   7   |   8   |   9            

Input

Input consists of several expressions following the grammar above, with no spaces at all. No expression has more than 2000 characters. You have the guarantee that no division by 0 will ever occur, and that no final nor intermediate result will overflow in a 32-bit int.

Output

For every expression, print the result of its evaluation.

Public test cases
  • Input

    4
    4+4
    4-2-2
    (8+7+9-1-2)*(1-2)*((5-1)+2)+1
    3+3*2
    9/3/3
    5/2
    ((8))
    (1-9)-(3-4)
    

    Output

    4
    8
    0
    -125
    9
    1
    2
    8
    -7
    
  • Information
    Author
    Jordi Petit
    Language
    English
    Official solutions
    C++
    User solutions
    C++ Rust