Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. #include "Exception.h"
  4.  
  5.  
  6. using namespace std;
  7.  
  8. class DinArr {
  9. protected:
  10. const int ADD = 10;
  11. int size;
  12. double *arr;
  13. int cap;
  14. public:
  15. DinArr() {
  16. size = 0;
  17. arr = new double[10];
  18. cap = 10;
  19. cout << "Created default DinArr!" << endl;
  20. }
  21. DinArr(int size) {
  22. if (size < 0) {
  23. throw NegativeSizeException();
  24. }
  25. else {
  26. this->size = size;
  27. cap = size + ADD;
  28. arr = new double[cap];
  29. cout << "Created new DinArr with size=" << size << endl;
  30. }
  31. }
  32. DinArr(double *a, int size) {
  33. if (size < 0) {
  34. throw NegativeSizeException();
  35. }
  36. if (a == nullptr) {
  37. throw NullPointerException();
  38. }
  39. this->size = size;
  40. cap = size + ADD;
  41. arr = new double[cap];
  42. for (int i = 0; i < size; i++) {
  43. arr[i] = a[i];
  44. }
  45. cout << "Copy ended!" << endl;
  46. }
  47.  
  48. int getCap() {
  49. return cap;
  50. }
  51.  
  52. double &operator[](int ind) {
  53. if (ind < 0) throw BAD_INDEX_EXP();
  54. if (ind >= size) throw OutOfBoundException();
  55. return arr[ind];
  56. }
  57.  
  58. DinArr &operator=(DinArr &A) {
  59. if (this != &A) {
  60. delete[] arr;
  61. arr = new double[A.getCap()];
  62. size = A.getSize();
  63. cap = A.getCap();
  64. for (int i = 0; i < size; i++) {
  65. arr[i] = A[i];
  66. }
  67. }
  68. cout << "operator= ok!" << endl;
  69. return *this;
  70. }
  71.  
  72. void reSize(int newSize) {
  73. if (size >= cap) {
  74. double *tmp = new double[newSize + ADD];
  75. for (int i = 0; i < size; i++) {
  76. tmp[i] = arr[i];
  77. }
  78. size = newSize;
  79. cap = size + ADD;
  80. delete[] arr;
  81. arr = tmp;
  82. cout << "Resized!" << "NEW SIZE:" << size << endl;
  83. }
  84. else size += 1;
  85. }
  86.  
  87. int getPos(double x) {
  88. for (int i = 0; i < size; i++) {
  89. if (arr[i] == x) return i;
  90. }
  91. return -1;
  92. }
  93.  
  94. int getSize()
  95. {
  96. return size;
  97. }
  98.  
  99. int setSize(int newSize) {
  100. if (newSize < 0) {
  101. throw NegativeSizeException();
  102. }
  103. if (newSize> cap) {
  104. reSize(newSize);
  105. }
  106. else size = newSize;
  107. }
  108.  
  109. void deletePos(int pos) {
  110. if (pos != -1) {
  111. for (int i = pos; i < size; i++) {
  112. arr[i] = arr[i + 1];
  113. }
  114. size -= 1;
  115. }
  116. }
  117.  
  118. void deleteElem(double x) {
  119. deletePos(getPos(x));
  120. }
  121.  
  122. void append(double x) {
  123. if (size == cap) {
  124. reSize(size + 1);
  125. }
  126. arr[size] = x;
  127. size += 1;
  128. }
  129.  
  130. void insert(double x, int pos) {
  131. reSize(size + 1);
  132. for (int i = size; i> pos; i--) arr[i] = arr[i - 1];
  133. arr[pos] = x;
  134. }
  135.  
  136. void set(double x, int pos) {
  137. if (pos >size - 1) {
  138. reSize(size + 2);
  139. }
  140. arr[pos] = x;
  141. }
  142.  
  143. ~DinArr() {
  144. size = cap = 0;
  145. delete[] arr;
  146. cout << "Destroed DinArr!" << endl;
  147. }
  148.  
  149. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement