13522 - Infinitely Big Integer: Darray Implementation   

Description

The problem "Infinitely Big Integer" is splitted into two subproblems:

  1. Darray Implementation (3/10)
  2. Big Integer Implementation (7/10)

You should solve this subproblem first.

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. 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.
  5. int length(void): return the current size.
  6. void resize(void): double the capacity and copy the data.
  7. ~Darray(): destructor

Note that main.cpp acts like a functional tester for your Darray. There's no need to understand it. You should test your Darray by yourself. Furthermore, a new function void popback(void) is 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);
    private:
        void resize(void); // double the capacity
        int *data;
        int capacity;
        int size;
};
// usage
Darray arr;
for (int i = 0; i < 5; i++) arr.pushback(i*i);
arr[2] += 100 + arr[3];

for (int i = 0; i < arr.length(); i++)
    cout << arr[2] << ' ';             // 
Print: 0 1 113 9 16
cout << endl << arr.length() << endl;  // Print: 5
arr.clear();

cout << arr.length() << endl;          // Print: 0
arr.pushback(9487); 
arr.pushback(9487);
arr.popback();

cout << arr.length() << ' ' << arr[0] << endl;  // Print: 1 9487

 

std::vector is prohibited (both subproblems 1 and 2) and will result in 0 score if you use it. 

Input

This is handled by the main function.

1/3 of the test cases will not test popback(). If you have problems with it, try writing an empty popback function so that it can be compiled successfully.

// in function.cpp
void popback() {
}

Output

This is handled by the main function.

Sample Input  Download

Sample Output  Download

Partial Judge Code

13522.cpp

Partial Judge Header

13522.h

Tags




Discuss