Prefix expression P59813


Statement
 

pdf   zip

html

Write a program that reads a prefix expression, and prints the result of evaluate it. The possible operators are sum, subtraction and multiplication. The operands are natural numbers between 0 and 9 (both included).

Input

Input is a prefix expression. It means that always appears the operator before its operands. For instance, the expression 4 + 3 would be:

+ 4 3

The expression 8 * (4 + 3) would be:

* 8 + 4 3

The expression (2 − 8) * (4 + 3) would be:

* - 2 8 + 4 3

Output

Your program must print an integer: the result of evaluate the expression.

Observation

Use recursion (not tables). Notice that an expression or is a digit, or is an operator followed by an expression followed by another expression.

Public test cases
  • Input

    + 4 3
    

    Output

    7
    
  • Input

    * 8 + 4 3
    

    Output

    56
    
  • Input

    * - 2 8 + 4 3
    

    Output

    -42
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Translator
    Carlos Molina
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C++
    User solutions
    C++