Advertisement
kqlul123

2l

Oct 22nd, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "iostream"
  3. using namespace std;
  4.  
  5.  
  6.  
  7. class TArr
  8. {
  9. double *Array;
  10. int Amount;
  11. public:
  12. TArr(int AmountAmount = 4, double ArrayArray = 1);
  13. TArr(TArr&obj);
  14. ~TArr();
  15. void Input();
  16. void Output();
  17. TArr&operator=(TArr&arr);
  18. bool operator >=(TArr &arr);
  19. TArr&operator+=(int x);
  20. friend TArr operator *=(TArr &arr,int number);
  21. friend bool operator==(TArr&arr1, TArr&arr2);
  22. };
  23.  
  24. TArr::TArr(int AmountAmount, double ArrayArray)
  25. {
  26. Amount = AmountAmount;
  27. Array = new double[Amount];
  28. for (int i = 0; i < Amount; i++)
  29. Array[i] = ArrayArray;
  30. }
  31.  
  32. TArr::TArr(TArr&obj)
  33. {
  34. Amount = obj.Amount;
  35. Array = new double[Amount];
  36. for (int i = 0; i < Amount; i++)
  37. {
  38. Array[i] = obj.Array[i];
  39. }
  40. }
  41.  
  42.  
  43. TArr::~TArr()
  44. {
  45. delete[]Array;
  46. }
  47.  
  48.  
  49. void TArr::Input()
  50. {
  51. cout << "Количество элементов: ";
  52. delete[]Array;
  53. cin >> Amount;
  54. Array = new double[Amount];
  55. for (int i = 0; i < Amount; i++)
  56. {
  57. cout << "Введите Array[" << i << "] элемент динамического массива: ";
  58. cin >> Array[i];
  59. }
  60. }
  61.  
  62. void TArr::Output()
  63. {
  64. cout << "Вывод массива типа double с количеством элементов " << Amount << ": " << endl;
  65. for (int i = 0; i < Amount; i++)
  66. {
  67. cout << "Array" << "[" << i << "] = " << Array[i] << endl;
  68. }
  69. }
  70.  
  71. TArr&TArr::operator=(TArr&arr)
  72. {
  73. delete[]Array;
  74. Amount = arr.Amount;
  75. Array = new double[Amount];
  76. for (int i = 0; i < Amount; i++)
  77. {
  78. Array[i] = arr.Array[i];
  79. }
  80. return *this;
  81. }
  82.  
  83. TArr&TArr::operator+=(int x)
  84. {
  85. for (int i = 0; i < Amount; i++)
  86. {
  87. Array[i] += x;
  88. }
  89. return *this;
  90. }
  91.  
  92. TArr operator*=(TArr &arr, int number) {
  93. arr.Amount += number;
  94. double *copy = new double[arr.Amount];
  95. for (int i = 0; i < arr.Amount; i++)
  96. {
  97. if (i < arr.Amount - number)
  98. copy[i] = arr.Array[i];
  99. else
  100. copy[i] = 0;
  101. }
  102. delete []arr.Array;
  103.  
  104. arr.Array = new double[arr.Amount];
  105. for (int i = 0; i < arr.Amount; i++)
  106. {
  107. arr.Array[i] = copy[i];
  108. }
  109. delete[]copy;
  110. return arr;
  111. }
  112.  
  113. bool operator==(TArr&arr1, TArr&arr2)
  114. {
  115. if (arr1.Amount != arr2.Amount) //сравниваем размеры массивов объектов
  116. {
  117. cout << "В массивах разное количество элементов" << endl;
  118. return 0;
  119. }
  120. else //проверяем равны ли данныев в ячейках массивов
  121. {
  122. for (int i = 0; i < arr1.Amount; i++)
  123. {
  124. if (arr1.Array[i] != arr2.Array[i])
  125. {
  126. cout << "Значения массивов не равны" << endl;
  127. return 0;
  128. }
  129. }
  130. }
  131. return 1;
  132. }
  133.  
  134. bool TArr::operator>=(TArr &arr) {
  135. if (Amount >= arr.Amount)
  136. return true;
  137. else
  138. return false;
  139. }
  140.  
  141. int main()
  142. {
  143. setlocale(LC_ALL, "Russian");
  144.  
  145.  
  146. TArr test, test2, test3(3, 4);
  147. test.Input();
  148. test.Output();
  149. test2.Output();
  150. test3.Output();
  151.  
  152. test3 = test2;
  153. cout << "Пункт 2) Присвоение(test3 = test2)" << endl;
  154. cout << "Массив test" << endl;
  155. test.Output();
  156. cout << "Массив test2" << endl;
  157. test2.Output();
  158. cout << "Массив test3" << endl;
  159. test3.Output();
  160. test += 3;
  161. cout << "Пункт 3) Объект + число (test += 3)" << endl;
  162. cout << "Массив test" << endl;
  163. test.Output();
  164. cout << "Массив test2" << endl;
  165. test2.Output();
  166. cout << "Массив test3" << endl;
  167. test3.Output();
  168. cout << "Пункт 4) Сравнение массивов(test/test2)" << endl;
  169. if (test == test2)
  170. cout << "test = test2" << endl;
  171. else
  172. cout << "test != test2" << endl;
  173. cout << "Сравнение массивов(test2/test3)" << endl;
  174. if (test2 == test3)
  175. cout << "test2 = test3" << endl;
  176. else
  177. cout << "test2 != test3" << endl;
  178. cout << "Пункт 5) Увеличение длины массива test на 5 элементов и заполнение их нулями " << endl;
  179. test *= 5;
  180. cout << "Массив test" << endl;
  181. test.Output();
  182. cout << "Массив test2" << endl;
  183. test2.Output();
  184. cout << "Массив test3" << endl;
  185. test3.Output();
  186. cout << "Пункт 6) Сравнение количества элементов test/test2" << endl;
  187. if (test >= test2)
  188. cout << "test >= test2" << endl;
  189. else
  190. cout << "test < test2" << endl;
  191.  
  192.  
  193.  
  194. system("pause");
  195. return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement