Advertisement
Guest User

Untitled

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