2794 - I2P(II)2023_Yang_lab4 Scoreboard

Time

2023/05/05 13:20:00 2023/05/05 15:20:00

Clarification

# Problem Asker Description Reply Replier Reply Time For all team

# Problem Pass Rate (passed user / total user)
13873 Dynamic Array 2
13896 Darray stack for postfix calculation (4/10)
13897 Darray stack for postfix calculation (6/10)

13873 - Dynamic Array 2   

Description

Based on the definition of Dynamic Array in Problem 13520 (nthu.edu.tw), you need to implement a dynamic array with the following functions:

  1. int& operator[](int): access data like using array. Users and main.cpp should/will not access the index which is greater or equal to size.
  2. void pushback(int x): append the element x
  3. void popback(void): pop the last element. Don't do anything if the array is empty.
  4. int back(void): return a copy of the last element. Return -1 if the array is empty.
  5. void clear(void): clear the array (set size to 0) so that the next pushbacks will place elements in data[0],data[1] and so on.
  6. int length(void): return the current size.
  7. void resize(void): double the capacity and copy the data.
  8. ~Darray(): destructor

Note that, two new functions void popback(void) and int back(void) are introduced in this problem.

// function.h

class Darray {
    public:
        Darray() {
            capacity = 100;
            size = 0;
            data = new int[capacity];
        };
        ~Darray();
        int& operator[](int);
        void pushback(int x);
        void popback(void);
        void clear(void);
        int length(void);
        int back(void);
        void print(void){
            if(this->size == 0){
                cout << endl;
            }
            else{
                for(int i=0; i<this->size; i++){
                    cout << this->data[i] << " ";
                }
                cout << endl;
            }
        };
    private:
        void resize(void); // double the capacity
        int *data;
        int capacity;
        int size;
};

Input

There are six kinds of commands:

  • “pushback integerA” represents adding an element with int value A at the end of the dynamic array.
  • “popback “ represents removing the element at the end of the dynamic array.
  • “clear” represents clearing the dynamic array.
  • “length” represents showing how many elements are in the dynamic array currently. 
  • “back” represents showing the element at the end of the dynamic array.
  • “print” represents showing the current content of the dynamic array.

Each command is followed by a new line character.

Output

The output should follow the instructions. 

("pushback", "popback" and "clear" will not print anything)

When the dynamic array is empty, you don’t need to print anything except a new line character.

Sample Input  Download

Sample Output  Download

Partial Judge Code

13873.cpp

Partial Judge Header

13873.h

Tags




Discuss




13896 - Darray stack for postfix calculation (4/10)   

Description

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

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

You should solve this subproblem first.


In this subproblem, you have to implement a stack class, Darray_stack, which uses the Darray class you have implemented in "13873 - Dynamic Array 2" as its internal representation.

Here is the structure of Darray_stack:

class Darray_stack{
    public:
        Darray_stack();
        void operator << (const int&);
        void operator >> (int&);
        int size(){
            return arr.length();
        }
    private:
        Darray arr;
};

You have to implement 3 public member functions for Darray_stack:

  1. Darray_stack(): the constructor
  2. void operator << (const int&): overloads << to push an element onto the stack
  3. void operator >> (int&): overloads >> to pop an element from the stack and store it at the right operand

To compile your code:

  • Your function.cpp should include function.h and contain the implementation of both Darray (copied from your code in "13873 - Dynamic Array 2") and Darray_stack. 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.

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 the verify code.

Sample Input  Download

Sample Output  Download

Partial Judge Code

13896.cpp

Partial Judge Header

13896.h

Tags




Discuss




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