Advertisement
kanciastopantalones

Untitled

Jan 9th, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. class tablica
  2. {
  3. private:
  4. int dl;
  5. int *tab;
  6. public:
  7. tablica();
  8. tablica(int rozmiar);
  9. void wpis(int indeks);
  10. void wypis();
  11. void realokacja(int element);
  12. tablica &operator=(const tablica& obiekt);
  13. ~tablica();
  14. };
  15.  
  16.  
  17.  
  18.  
  19.  
  20. #include "tablica.h"
  21. #include <iostream>
  22.  
  23. using namespace std;
  24.  
  25. tablica::tablica()
  26. {
  27. dl=0;
  28. tab=NULL;
  29. }
  30.  
  31. tablica::tablica(int rozmiar)
  32. {
  33. dl=rozmiar;
  34. tab=new int[dl];
  35. int i;
  36. for (i=0;i<=dl-1;i++)
  37. {
  38.  
  39. tab[i]=i*2;
  40. cout << tab[i] << " ";
  41. }
  42. cout << endl;
  43.  
  44. }
  45.  
  46. void tablica::wpis(int indeks)
  47. {
  48. if (indeks<=dl)
  49. tab[indeks-1]=(indeks-1)*2;
  50. else{
  51. int element=indeks-dl;
  52. realokacja(element);
  53. tab[indeks-1]=(indeks-1)*2;
  54. }
  55. }
  56.  
  57. void tablica:: realokacja(int element)
  58. {
  59.  
  60. int *tmp=new int[dl];
  61. for (int i=0; i<dl; i++)
  62. {
  63. tmp[i]=tab[i];
  64. };
  65.  
  66. tab=new int[dl+element];
  67. for (int i=0; i<dl; i++)
  68. {
  69. tab[i]=tmp[i];
  70. };
  71.  
  72. dl=dl+element;
  73. delete tmp;
  74.  
  75. };
  76.  
  77. void tablica::wypis()
  78. {
  79. int i;
  80. for (i=0;i<=dl-1;i++)
  81. cout<<i+1<<" wartosc wektora: "<<tab[i]<<endl;
  82. cout<<endl;
  83.  
  84. }
  85.  
  86. tablica & tablica::operator=(const tablica &wekt)
  87. {
  88. if (dl!=wekt.dl)
  89. tab=new int[wekt.dl];
  90.  
  91. dl=wekt.dl;
  92. int i;
  93. for (i=0;i<dl;i++)
  94. tab[i]=wekt.tab[i];
  95. return *this;
  96. };
  97.  
  98.  
  99. tablica::~tablica()
  100. {
  101. delete tab;
  102. }
  103.  
  104.  
  105.  
  106.  
  107.  
  108. #include <iostream>
  109. #include "tablica.h"
  110. #include <conio.h>
  111.  
  112. using namespace std;
  113.  
  114.  
  115. int main()
  116. {
  117.  
  118.  
  119. int dlugosc=5;
  120.  
  121.  
  122. tablica tab(dlugosc);
  123.  
  124. tab.wypis();
  125.  
  126. cout << "O ile elementow powiekszyc istniejaca tablice: ";
  127. int powieksz;
  128. cin >> powieksz;
  129.  
  130. for(int i=0;i<=powieksz;i++)
  131. {
  132. tab.wpis(dlugosc+i);
  133. }
  134. tab.wypis();
  135.  
  136.  
  137.  
  138.  
  139. tablica t1(9);
  140. tablica t2(2);
  141. tablica t3(4);
  142.  
  143.  
  144. t1.wypis();
  145. t2.wypis();
  146. t3.wypis();
  147. t1=t2=t3;
  148. cout<<"Wypisane tablice po przeciazeniu operatorow"<<endl;
  149. t1.wypis();
  150. t2.wypis();
  151. t3.wypis();
  152.  
  153. getch();
  154. return 0;
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement