Advertisement
Guest User

Untitled

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