Advertisement
Guest User

Untitled

a guest
May 25th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #pragma once
  2. #include <stdlib.h>
  3. #include <time.h>
  4. using namespace std;
  5.  
  6. template<class T>
  7. class MagicBox
  8. {
  9. private:
  10.     T* box;
  11.     int current;
  12.     int capacity;
  13.  
  14.     void grow();
  15. public:
  16.     MagicBox();
  17.     MagicBox(int cap);
  18.     MagicBox(const MagicBox & rhs);
  19.     MagicBox& operator=(const MagicBox& rhs);
  20.     ~MagicBox();
  21.  
  22.     T* getBox() const;
  23.     //void setCapacity(T cap);
  24.     const int getCapacity() const;
  25.     const int getCurr() const;
  26.  
  27.  
  28.     void insert(const T& item);
  29.     void pop();
  30.     void list() const;
  31.     char indlist(int a) const;
  32. };
  33.  
  34. template<class T>
  35. inline void MagicBox<T>::grow()
  36. {
  37.     int oldCap = capacity;
  38.     capacity++;
  39.     T* newArr = new T[capacity];
  40.     for (int i = 0; i < oldCap; i++)
  41.         newArr[i] = box[i];
  42.     delete[] box;
  43.     box = newArr;
  44. }
  45.  
  46. template<class T>
  47. inline MagicBox<T>::MagicBox()
  48. {
  49.     box = nullptr;
  50.     capacity = 0;
  51.     current = -1;
  52. }
  53.  
  54. template<class T>
  55. inline MagicBox<T>::MagicBox(int cap)
  56. {
  57.     capacity = cap;
  58.     current = -1;
  59.     box = new T[capacity];
  60. }
  61.  
  62. template<class T>
  63. inline MagicBox<T>::MagicBox(const MagicBox& rhs)
  64. {
  65.     delete[] box;
  66.     box = new T[rhs.getCapacity()];
  67.     for (int i = 0; i <= rhs.getCurr(); i++)
  68.     {
  69.         box[i] = rhs.indlist(i);
  70.     }
  71.     this->capacity = rhs.getCapacity();
  72.     this->current = rhs.getCurr();
  73. }
  74.  
  75. template <class T>
  76. inline MagicBox<T>& MagicBox<T>::operator=(const MagicBox<T>& rhs)
  77. {
  78.     if (this != &rhs)
  79.     {
  80.         delete[] box;
  81.         box = new T[rhs.getCapacity()];
  82.         for (int i = 0; i <= rhs.getCurr(); i++)
  83.         {
  84.             box[i] = rhs.indlist(i);
  85.         }
  86.         this->capacity = rhs.getCapacity();
  87.         this->current = rhs.getCurr();
  88.     }
  89.     return *this;
  90. }
  91.  
  92. template<class T>
  93. inline MagicBox<T>::~MagicBox()
  94. {
  95.     delete[] box;
  96. }
  97.  
  98. template<class T>
  99. inline void MagicBox<T>::insert(const T& item)
  100. {
  101.     if (current == capacity - 1)
  102.     {
  103.         grow();
  104.     }
  105.     box[++current] = item;
  106. }
  107.  
  108. template<class T>
  109. inline void MagicBox<T>::pop()
  110. {
  111.  
  112.     if (current > 0)
  113.     {
  114.         srand(time(NULL));
  115.         int index = rand() % current + 0;
  116.         if (index == current)
  117.         {
  118.             --current;
  119.             return;
  120.         }
  121.         for (int i = index; i < current; ++i)
  122.         {
  123.             box[i] = box[i + 1];
  124.         }
  125.         --current;
  126.     }
  127.     else
  128.     {
  129.         delete[] box;
  130.         box = new T[0];
  131.         current = -1;
  132.     }
  133.  
  134. }
  135.  
  136. template<class T>
  137. inline void MagicBox<T>::list() const
  138. {
  139.     if (current >= 0)
  140.     {
  141.         for (int i = 0; i <= current; i++)
  142.         {
  143.             cout << box[i] << " ";
  144.         }
  145.         cout << "\n";
  146.     }
  147.     else return;
  148. }
  149.  
  150. template<class T>
  151. inline const int MagicBox<T>::getCapacity() const
  152. {
  153.     return capacity;
  154. }
  155.  
  156. template<class T>
  157. inline const int MagicBox<T>::getCurr() const
  158. {
  159.     return current;
  160. }
  161.  
  162. template<class T>
  163. inline T* MagicBox<T>::getBox() const
  164. {
  165.     return box;
  166. }
  167.  
  168. template<class T>
  169. inline char MagicBox<T>::indlist(int a) const
  170. {
  171.     return box[a];
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement