Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. template <typename typ> class zbior
  7. {
  8. typ* tablica;
  9. int ilosc;
  10. ///ta funkcja
  11. int obliczSume(int ilosc)
  12. {
  13. int suma = 0;
  14. for (int i = 0; i < ilosc; i++)
  15. {
  16. suma += tablica[i];
  17. }
  18. //dla 1 elementu
  19. if (suma == 0)
  20. return 1;
  21. //dla 2 elementu
  22. else if (suma == 1)
  23. return 2;
  24. //dla n-tego elementu
  25. else
  26. return suma;
  27. }
  28. public:
  29. ///zbedne
  30. zbior(zbior<typ>& z)
  31. {
  32. try
  33. {
  34. tablica = new typ[z.ilosc];
  35. ilosc = z.ilosc;
  36. for (int i = 0; i < ilosc; i++)
  37. tablica[i] = z.tablica[i];
  38. }
  39. catch (bad_alloc)
  40. {
  41. cout << "Za mało pamięci!";
  42. };
  43. }
  44.  
  45. ///zbedne
  46. zbior<typ>& operator =(zbior<typ> z)
  47. {
  48. try
  49. {
  50. tablica = new typ[z.ilosc];
  51. ilosc = z.ilosc;
  52. for (int i = 0; i < ilosc; i++)
  53. tablica[i] = z.tablica[i];
  54. }
  55. catch (bad_alloc)
  56. {
  57. cout << "Za mało pamięci!";
  58. };
  59. return *this;
  60. }
  61. ///1 punkt
  62. ///parametr
  63. zbior(int a = 100)
  64. {
  65. try
  66. {
  67. ilosc = a;
  68. tablica = new typ[a];
  69. for (int i = 0; i < ilosc; i++)
  70. ///ciag arytmetyczny
  71. tablica[i] = obliczSume(i);
  72. }
  73. catch (bad_alloc)
  74. {
  75. cout << "Za mało pamięci!";
  76. }
  77. };
  78. ///2 punkt
  79. ~zbior()
  80. {
  81. delete[] tablica;
  82. };
  83. ///3 punkt
  84. zbior<typ>& operator <<(typ x)
  85. {
  86. typ* temp;
  87. try
  88. {
  89. ilosc++;
  90. temp = new typ[ilosc];
  91. for (int i = 0; i < ilosc - 1; i++)
  92. temp[i] = tablica[i];
  93. temp[ilosc - 1] = x;
  94. delete[] tablica;
  95. tablica = temp;
  96. }
  97. catch (bad_alloc)
  98. {
  99. ilosc--;
  100. cout << "Za mało pamięci!";
  101. };
  102.  
  103. return *this;
  104. };
  105.  
  106. ///punkt 4
  107. ///nowy
  108. zbior<typ>& operator >>(typ x)
  109. {
  110. typ* temp;
  111. try
  112. {
  113. ilosc++;
  114. temp = new typ[ilosc];
  115. for (int i = 1; i < ilosc; i++)
  116. temp[i] = tablica[i - 1];
  117. temp[0] = x;
  118. delete[] tablica;
  119. tablica = temp;
  120. }
  121. catch (bad_alloc)
  122. {
  123. ilosc--;
  124. cout << "Za mało pamięci!";
  125. };
  126.  
  127. return *this;
  128. };
  129. ///zbedne
  130. zbior<typ>& operator --()
  131. {
  132. typ* temp;
  133. try
  134. {
  135. ilosc--;
  136. temp = new typ[ilosc];
  137. for (int i = 0; i < ilosc; i++)
  138. temp[i] = tablica[i];
  139. delete[] tablica;
  140. tablica = temp;
  141. }
  142. catch (bad_alloc)
  143. {
  144. ilosc++;
  145. cout << "Za mało pamięci!";
  146. };
  147.  
  148. return *this;
  149. };
  150. ///zbedne
  151. int operator %(typ x)
  152. {
  153. for (int i = 0; i < ilosc; i++)
  154. {
  155. if (tablica[i] == x)
  156. return 1;
  157. }
  158. return 0;
  159. }
  160.  
  161.  
  162. ///zbedne
  163. typ operator [](int a)
  164. {
  165. return tablica[a];
  166. }
  167. ///4 punkt
  168. friend ostream& operator<< (ostream& wyj, const zbior<typ>& zet);// Dziedziczenie funkcji
  169.  
  170. ///5 punkt
  171. int GetSize() const// Funkcja zwracająca wielkość zbioru
  172. {
  173. return ilosc;
  174. }
  175.  
  176. };
  177.  
  178. template <class parametr> ostream& operator << (ostream& wyj, zbior<parametr>& zet)// klasa do strumienia wartości zbioru
  179. {
  180. int j = 0;
  181. wyj << "[";
  182. int i = 0;
  183. int max = zet.GetSize();
  184. while (i < max)
  185. {
  186. wyj << zet[i];
  187. i++;
  188. if (i != max)
  189. {
  190. wyj << ",";
  191. }
  192. }
  193. wyj << "]" << endl;
  194.  
  195.  
  196. wyj << endl;
  197. return wyj;
  198. }
  199.  
  200.  
  201.  
  202. int main(int argc, char* argv[])
  203. {
  204. /*
  205. int k;
  206. int p;
  207. zbior<int> z1(4);
  208. zbior<int> z4(100000000000000);
  209. zbior<int> z2(4);
  210. zbior<float> z5(9);
  211. z1 << 3;
  212. z1 << 4;
  213. z1 << 5;
  214. z1 << 7;
  215. p = z1 % 5;
  216. cout << "Czy element nalezy do zbioru: " << endl;
  217. if (p == 1)
  218. {
  219. cout << "Dany element nalezy do zbioru" << endl;
  220. }
  221. else
  222. {
  223. cout << "Dany element nie nalezy do zbioru" << endl;
  224. }
  225. k = z1[3];
  226. cout << "Wartosc dla danego indeksu: " << k << endl;
  227. cout << "zbior z1:" << z1 << endl;
  228. z2 = z1;
  229. cout << "zbior z2 przepisany operatorem przypisania: " << z2;
  230. zbior<int> z3(z1);
  231. cout << "Zbior z3 skopiowany konsturktorem kopiujacym: " << z3;
  232. system("PAUSE");
  233. return EXIT_SUCCESS;
  234. */
  235. zbior<int> z1(4);
  236. cout << "zbior podstawowy: ";
  237. cout << z1 << endl;
  238.  
  239. z1 << 30;
  240. z1 << 40;
  241. z1 << 50;
  242. z1 << 70;
  243.  
  244. cout << "zbior po dodaniu 4 elementow na koniec: ";
  245. cout << z1 << endl;
  246.  
  247. z1 >> -30;
  248. z1 >> -40;
  249. z1 >> -50;
  250. z1 >> -70;
  251.  
  252. cout << "zbior po dodaniu 4 elementow na poczatek: ";
  253. cout << z1 << endl;
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement