fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. #include <iostream>
  9. #include <cassert>
  10.  
  11. using namespace std;
  12.  
  13. class Matrix
  14. {
  15. private:
  16. class CountingReference
  17. {
  18. private:
  19. int * countingReference;
  20. public:
  21. int wiersz;
  22. int kolumna;
  23. double **wsk;
  24. CountingReference();
  25. CountingReference(int, int);
  26. CountingReference(const CountingReference &);
  27. ~CountingReference();
  28. CountingReference& operator=(CountingReference rhs);
  29. };
  30. CountingReference dane;
  31.  
  32. public:
  33.  
  34. friend ostream& operator<<(ostream& o, const Matrix&);
  35. friend ostream& operator<<(const Matrix&, ostream& o);
  36. Matrix();
  37. Matrix(int, int);
  38. static Matrix clone(const Matrix &);
  39. Matrix operator+(const Matrix &) const;
  40. Matrix operator-(const Matrix &) const;
  41. Matrix operator*(const Matrix &) const;
  42. Matrix operator+=(const Matrix&);
  43. Matrix& operator-=(const Matrix&);
  44. Matrix& operator*=(const Matrix&);
  45. bool operator ==(const Matrix &);
  46. friend Matrix& Random(Matrix&);
  47. };
  48. ostream& operator<<(ostream& o, const Matrix& Object)
  49. {
  50. for (int i = 0; i < Object.dane.wiersz; i++)
  51. {
  52. for (int j = 0; j < Object.dane.kolumna; j++)
  53. {
  54. o << Object.dane.wsk[i][j] << " ";
  55. }
  56. o << endl;
  57. }
  58. return o;
  59. }
  60.  
  61. Matrix::Matrix() :
  62. dane()
  63. {
  64. }
  65.  
  66. Matrix::Matrix(int wiersz, int col) :
  67. dane(wiersz, col)
  68. {
  69. }
  70.  
  71. Matrix Matrix::clone(const Matrix &Object)
  72. {
  73. Matrix result(Object.dane.wiersz,
  74. Object.dane.kolumna);
  75. for (int i = 0; i < result.dane.wiersz; i++)
  76. {
  77. for (int j = 0; j < result.dane.kolumna; j++)
  78. {
  79. result.dane.wsk[i][j] = Object.dane.wsk[i][j];
  80. }
  81. }
  82. return result;
  83. }
  84.  
  85. Matrix Matrix::operator+(const Matrix &Object) const
  86. {
  87. return Matrix::clone(*this) +=Object;
  88. }
  89.  
  90. Matrix Matrix::operator-(const Matrix &Object) const
  91. {
  92. return Matrix::clone(*this) -=Object;
  93. }
  94.  
  95. Matrix Matrix::operator*(const Matrix &Object) const
  96. {
  97. return Matrix::clone(*this) *=Object;
  98. }
  99.  
  100. Matrix Matrix::operator+=(const Matrix& Object)
  101. {
  102. if (dane.wiersz != Object.dane.wiersz
  103. || dane.kolumna != Object.dane.kolumna)
  104. {
  105. string wyjatek = "Nie sa rowne.";
  106. throw wyjatek;
  107. }
  108. else
  109. {
  110. for (int i = 0; i < dane.wiersz; i++)
  111. {
  112. for (int j = 0; j < dane.kolumna; j++)
  113. {
  114. dane.wsk[i][j] += Object.dane.wsk[i][j];
  115. }
  116. }
  117. }
  118.  
  119. return *this;
  120. }
  121.  
  122. Matrix& Matrix::operator-=(const Matrix& Object)
  123. {
  124. if (dane.wiersz != Object.dane.wiersz
  125. || dane.kolumna != Object.dane.kolumna)
  126. {
  127. string wyjatek = "Nie sa rowne.";
  128. throw wyjatek;
  129. }
  130. else
  131. {
  132. for (int i = 0; i < dane.wiersz; i++)
  133. {
  134. for (int j = 0; j < dane.kolumna; j++)
  135. {
  136. dane.wsk[i][j] -= Object.dane.wsk[i][j];
  137. }
  138. }
  139. }
  140. return *this;
  141. }
  142.  
  143. Matrix& Matrix::operator*=(const Matrix& Object)
  144. {
  145. if (dane.kolumna != Object.dane.wiersz)
  146. {
  147. string wyjatek = "Nie sa rowne.";
  148. throw wyjatek;
  149. }
  150. else
  151. {
  152. int m = 0, n = 0;
  153. double temp = 0;
  154. m = dane.wiersz;
  155. n = Object.dane.kolumna;
  156. Matrix A(m, n);
  157. for (int i = 0; i < dane.wiersz; i++)
  158. {
  159. for (int j = 0; j < Object.dane.kolumna; j++)
  160. {
  161. for (int k = 0; k < dane.kolumna; k++)
  162. {
  163. temp += dane.wsk[i][k] * Object.dane.wsk[k][j];
  164. }
  165.  
  166. A.dane.wsk[i][j] = temp;
  167. temp = 0;
  168. }
  169. }
  170.  
  171. for (int i = 0; i < m; i++)
  172. {
  173. for (int j = 0; j < n; j++)
  174. {
  175. dane.wsk[i][j] = A.dane.wsk[i][j];
  176. }
  177. }
  178. }
  179. return *this;
  180. }
  181.  
  182. bool Matrix::operator ==(const Matrix& Object)
  183. {
  184. int i, j;
  185. for (i = 0; i < dane.wiersz; i++)
  186. {
  187. for (j = 0; j < dane.kolumna; j++)
  188. {
  189. if (dane.wsk[i][j] != Object.dane.wsk[i][j])
  190. {
  191. return false;
  192. }
  193. }
  194. }
  195. return true;
  196. }
  197.  
  198. Matrix& Random(Matrix& Object)
  199. {
  200. for (int i = 0; i < Object.dane.wiersz; i++)
  201. {
  202. for (int j = 0; j < Object.dane.kolumna; j++)
  203. {
  204. Object.dane.wsk[i][j] = rand() % 100 + 1;
  205. }
  206. }
  207.  
  208. return Object;
  209. }
  210.  
  211. Matrix::CountingReference::CountingReference() :
  212. countingReference(new int(1)), wiersz(0), kolumna(0), wsk(NULL)
  213.  
  214. {
  215. }
  216.  
  217. Matrix::CountingReference::CountingReference(int wier, int kol) :
  218. countingReference(new int(1)), wiersz(wier), kolumna(kol), wsk(new double*[wier])
  219. {
  220. for (int i = 0; i < wier; i++)
  221. {
  222. wsk[i] = new double[kol];
  223. }
  224. cout << countingReference << " was created \n";
  225. }
  226.  
  227. Matrix::CountingReference::CountingReference(const CountingReference & src) :
  228. countingReference(src.countingReference), wiersz(src.wiersz), kolumna(src.kolumna), wsk(src.wsk)
  229.  
  230. {
  231. ++(*countingReference);
  232. cout << countingReference << " was copied (" << *countingReference << ")\n";
  233. }
  234.  
  235. Matrix::CountingReference::~CountingReference()
  236. {
  237. --(*countingReference);
  238. cout << countingReference << " was decremented (" << *countingReference << ")\n";
  239. if (!*countingReference)
  240. {
  241. cout << countingReference << " was destroyed\n";
  242. for (int i = 0; i < wiersz; i++)
  243. {
  244. delete[] wsk[i];
  245. }
  246. delete[] wsk;
  247. delete countingReference;
  248. }
  249. }
  250. Matrix::CountingReference & Matrix::CountingReference::operator=(CountingReference rhs)
  251. {
  252. cout << countingReference << " was replaced by " << rhs.countingReference << "\n";
  253. swap(countingReference, rhs.countingReference);
  254. swap(wsk, rhs.wsk);
  255. swap(wiersz, rhs.wiersz);
  256. swap(kolumna, rhs.kolumna);
  257. return *this;
  258. }
  259.  
  260. int main()
  261. {
  262. cout << "Create A\n";
  263. Matrix A(3, 3);
  264. cout << "Create B\n";
  265. Matrix B(3, 3);
  266. cout << "Create C\n";
  267. Matrix C(3, 3);
  268. cout << "Create D\n";
  269. Matrix D(3, 3);
  270. Matrix F(); // declares function F. See Most Vexing Parse
  271.  
  272. Random(B);
  273. Random(C);
  274. Random(D);
  275.  
  276. cout << "B: " << endl << B << endl;
  277. cout << "C: " << endl << C << endl;
  278. cout << "D: " << endl << D << endl;
  279.  
  280. cout << "A = B + C\n";
  281. A = B + C;
  282. cout << "A = B\n";
  283. A = B;
  284. cout << "B = C\n";
  285. B = C;
  286. cout << "C = D\n";
  287. C = D;
  288. cout << "A = B\n";
  289. A = B;
  290. cout << "B = C\n";
  291. B = C;
  292. cout << "A = B\n";
  293. A = B;
  294. cout << "Done. Cleaning up\n";
  295. return 0;
  296. }
Success #stdin #stdout 0s 16072KB
stdin
Standard input is empty
stdout
Create A
0x2b3e66f5ec30 was created 
Create B
0x2b3e66f5ecd0 was created 
Create C
0x2b3e66f5ed70 was created 
Create D
0x2b3e66f5ee10 was created 
B: 
84 87 78 
16 94 36 
87 93 50 

C: 
22 63 28 
91 60 64 
27 41 27 

D: 
73 37 12 
69 68 30 
83 31 63 

A = B + C
0x2b3e66f5eeb0 was created 
0x2b3e66f5eeb0 was copied (2)
0x2b3e66f5eeb0 was decremented (1)
0x2b3e66f5eeb0 was copied (2)
0x2b3e66f5ec30 was replaced by 0x2b3e66f5eeb0
0x2b3e66f5ec30 was decremented (0)
0x2b3e66f5ec30 was destroyed
0x2b3e66f5eeb0 was decremented (1)
A = B
0x2b3e66f5ecd0 was copied (2)
0x2b3e66f5eeb0 was replaced by 0x2b3e66f5ecd0
0x2b3e66f5eeb0 was decremented (0)
0x2b3e66f5eeb0 was destroyed
B = C
0x2b3e66f5ed70 was copied (2)
0x2b3e66f5ecd0 was replaced by 0x2b3e66f5ed70
0x2b3e66f5ecd0 was decremented (1)
C = D
0x2b3e66f5ee10 was copied (2)
0x2b3e66f5ed70 was replaced by 0x2b3e66f5ee10
0x2b3e66f5ed70 was decremented (1)
A = B
0x2b3e66f5ed70 was copied (2)
0x2b3e66f5ecd0 was replaced by 0x2b3e66f5ed70
0x2b3e66f5ecd0 was decremented (0)
0x2b3e66f5ecd0 was destroyed
B = C
0x2b3e66f5ee10 was copied (3)
0x2b3e66f5ed70 was replaced by 0x2b3e66f5ee10
0x2b3e66f5ed70 was decremented (1)
A = B
0x2b3e66f5ee10 was copied (4)
0x2b3e66f5ed70 was replaced by 0x2b3e66f5ee10
0x2b3e66f5ed70 was decremented (0)
0x2b3e66f5ed70 was destroyed
Done. Cleaning up
0x2b3e66f5ee10 was decremented (3)
0x2b3e66f5ee10 was decremented (2)
0x2b3e66f5ee10 was decremented (1)
0x2b3e66f5ee10 was decremented (0)
0x2b3e66f5ee10 was destroyed