Back to home page
  • Prefix expressions

    Challenge Description:

    You are given a prefix expression. Write a program which evaluates it.

    Input sample:

    Your program should accept a file as its first argument. The file contains one prefix expression per line.

    For example:

    * + 2 3 4

    Your program should read this file and insert it into any data structure you like. Traverse this data structure and evaluate the prefix expression. Each token is delimited by a whitespace. You may assume that sum ‘+’, multiplication ‘*’ and division ‘/’ are the only valid operators appearing in the test data.

    Output sample:

    Print to stdout the output of the prefix expression, one per line.

    For example:



    • The evaluation result will always be an integer ≥ 0.
    • The number of the test cases is ≤ 40.
Login to submit solution