13897 - Darray stack for postfix calculation (6/10)   

Description

The problem “Darray stack” is splitted into 2 subproblems:

  1. Darray & stack implementation (4/10)
  2. Postfix expression calculation using stack (6/10)

You should solve the former subproblem first.


In this subproblem, you are asked to implement a class, Postfix_calculator, which calculates a postfix expression using the Darray_stack class you have implemented in "13896".

Here is the structure of Postfix_calculator:

class Postfix_calculator{
    public:
        Postfix_calculator(){}
        Postfix_calculator(string s): postfix(s){}
        void calculate();
        int answer(){
            int ans;
            st >> ans;
            return ans;
        }
    private:
        string postfix;
        Darray_stack st;
};

Postfix_calculator contains 2 data members:

  1. string postfix: the input postfix expression
  2. Darray_stack st: the stack you are going to use during the whole calculation process.

You only have to implement the member function void calculate(), which calculates the postfix expression using Darray_stack st and stores the final result on the top of Darray_stack st.


To compile your code:

  • Your function.cpp should include function.h and contain the implementation of all Darray (copied from your code in "13873 - Dynamic Array 2"), Darray_stack (copied from your code in "13896"), and Postfix_calculator. Our main.cpp would run the function ‘verify_code’ to test whether your implementation is correct.
  • Please use C++17.

Input

The first line is a string which only contains digits, representing the input of the verify function.

The second line is a postfix expression. The numbers and the operators are separated by spaces.

Constraints:

All numbers in the expression are nonnegative and are less than or equal to 100.

The length of the expression is no more than 250.

The length of the verify input code is no more than 10000.

The operators would only be '+', '-' or '*'.

It is guaranteed the numbers won't exceed 10during the whole calculation process.

Output

Output contains 2 lines.

The first line contains the verify code.

The second line contains an integer, representing the result of the postfix expression.

Sample Input  Download

Sample Output  Download

Partial Judge Code

13897.cpp

Partial Judge Header

13897.h

Tags




Discuss