Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. //.h
  2. #pragma once
  3. #ifndef MATRIX_H
  4. #define MATRIX_H
  5. class Matrix {
  6. public:
  7. Matrix();
  8. Matrix(int m, int n);
  9. Matrix(int tab[]);
  10. Matrix(Matrix &m);
  11. ~Matrix();
  12. void ChangeSize(int n, int m);
  13. int GetColumns();
  14. int GetRows();
  15. void FillRandom();
  16. void Print();
  17. int **_tab;
  18. Matrix operator = (Matrix m);
  19. Matrix operator + (Matrix m);
  20. Matrix operator - (Matrix m);
  21. Matrix operator * (float m);
  22. Matrix operator * (Matrix m);
  23. Matrix operator += (Matrix m);
  24. Matrix operator -= (Matrix m);
  25. Matrix operator *= (float m);
  26. Matrix operator *= (Matrix m);
  27. bool operator == (Matrix m);
  28. bool operator != (Matrix m);
  29. Matrix operator >> (Matrix m);
  30. void operator << (Matrix m);
  31. private:
  32. int columns;
  33. int rows;
  34. };
  35. #endif
  36. //end.h
  37. //.cpp
  38. #include "matrix.h"
  39. #include <random>
  40. #include <iostream>
  41. #define RAND_MAX = 1;
  42.  
  43. using namespace std;
  44. Matrix::Matrix(int n, int m) {
  45. rows = n;
  46. columns = m;
  47. _tab = new int*[n];
  48. for (int i = 0; i < n; ++i)
  49. {
  50. _tab[i] = new int[m];
  51. }
  52. for (int i = 0; i < n; i++) {
  53. for (int j = 0; j < m; j++) {
  54. _tab[i][j] = 0;
  55. }
  56. }
  57. }
  58. Matrix::Matrix(int tab[]) {
  59. rows = tab[0];
  60. columns = tab[1];
  61. _tab = new int*[tab[0]];
  62. for (int i = 0; i < tab[0]; ++i)
  63. {
  64. _tab[i] = new int[tab[1]];
  65. }
  66. for (int i = 0; i < tab[0]; i++) {
  67. for (int j = 0; j < tab[1]; j++) {
  68. _tab[i][j] = 0;
  69. }
  70. }
  71. }
  72. Matrix::Matrix() {
  73. _tab = new int*[0];
  74. _tab[0] = new int[0];
  75. }
  76. Matrix::~Matrix() {
  77. for (int i = 0; i < rows; ++i)
  78. {
  79. delete _tab[i];
  80. }
  81. delete _tab;
  82. }
  83. int Matrix::GetColumns() {
  84. return columns;
  85. }
  86. int Matrix::GetRows() {
  87. return rows;
  88. }
  89. void Matrix::FillRandom() {
  90. for (int i = 0; i < GetRows(); i++) {
  91. for (int j = 0; j < GetColumns(); j++) {
  92. _tab[i][j] = (rand() %2 );
  93. }
  94. }
  95. }
  96. void Matrix::Print() {
  97. for (int i = 0; i < GetRows(); i++) {
  98. for (int j = 0; j < GetColumns(); j++) {
  99. printf("%d", _tab[i][j]);
  100. }
  101. printf("\n");
  102. }
  103. }
  104. void Matrix::ChangeSize(int n, int m) {
  105. rows = n;
  106. columns = m;
  107. _tab = new int*[n];
  108. for (int i = 0; i < n; ++i)
  109. {
  110. _tab[i] = new int[m];
  111. }
  112. for (int i = 0; i < n; ++i)
  113. {
  114. for (int j = 0; j < n; ++j)
  115. {
  116. _tab[i][j] = 0;
  117. }
  118. }
  119. }
  120. Matrix::Matrix(Matrix &m) {
  121. _tab = new int*[m.GetRows()];
  122. for (int i = 0; i < m.GetRows(); ++i)
  123. {
  124. _tab[i] = new int[m.GetColumns()];
  125. }
  126. for (int i = 0; i < m.GetRows(); i++) {
  127. for (int j = 0; j < m.GetColumns(); j++) {
  128. _tab[i][j] = m._tab[i][j];
  129. }
  130. }
  131. }
  132. Matrix Matrix::operator=(Matrix m) {
  133. if (rows == m.GetRows() && columns == m.GetColumns()) {
  134. Matrix r(rows, columns);
  135. for (int i = 0; i < rows; i++) {
  136. for (int j = 0; j < columns; j++) {
  137. r._tab[i][j] = this->_tab[i][j] - m._tab[i][j];
  138. }
  139. }
  140. }
  141. }
  142. Matrix Matrix::operator+(Matrix m) {
  143. if (rows == m.GetRows() && columns == m.GetColumns()) {
  144. Matrix r(rows, columns);
  145. for (int i = 0; i < rows; i++) {
  146. for (int j = 0; j < columns; j++) {
  147. r._tab[i][j] = this->_tab[i][j] + m._tab[i][j];
  148. }
  149. }
  150. return r;
  151. }
  152. return NULL;
  153. }
  154. Matrix Matrix::operator-(Matrix m) {
  155. if (rows == m.GetRows() && columns == m.GetColumns()) {
  156. Matrix r(rows, columns);
  157. for (int i = 0; i < rows; i++) {
  158. for (int j = 0; j < columns; j++) {
  159. r._tab[i][j] = this->_tab[i][j] - m._tab[i][j];
  160. }
  161. }
  162. return r;
  163. }
  164. return NULL;
  165. }
  166. Matrix Matrix::operator*(float m) {
  167. Matrix r(rows, columns);
  168. for (int i = 0; i < rows; i++) {
  169. for (int j = 0; j < columns; j++) {
  170. r._tab[i][j] = this->_tab[i][j] * m;
  171. }
  172. }
  173. return r;
  174. }
  175. Matrix Matrix::operator*(Matrix m) {
  176. if (rows == m.GetRows() && columns == m.GetColumns()) {
  177. Matrix r(rows, columns);
  178. for (int i = 0; i < rows; i++) {
  179. for (int j = 0; j < columns; j++) {
  180. r._tab[i][j] = this->_tab[i][j] * m._tab[i][j];
  181. }
  182. }
  183. return r;
  184. }
  185. return NULL;
  186. }
  187. Matrix Matrix::operator+=(Matrix m) {
  188. if (rows == m.GetRows() && columns == m.GetColumns()) {
  189. for (int i = 0; i < rows; i++) {
  190. for (int j = 0; j < columns; j++) {
  191. _tab[i][j] += m._tab[i][j];
  192. }
  193. }
  194. }
  195. }
  196. Matrix Matrix::operator-=(Matrix m) {
  197. if (rows == m.GetRows() && columns == m.GetColumns()) {
  198. for (int i = 0; i < rows; i++) {
  199. for (int j = 0; j < columns; j++) {
  200. _tab[i][j] -= m._tab[i][j];
  201. }
  202. }
  203. }
  204. }
  205. Matrix Matrix::operator *= (float m) {
  206. for (int i = 0; i < rows; i++) {
  207. for (int j = 0; j < columns; j++) {
  208. _tab[i][j] *= m;
  209. }
  210. }
  211. }
  212. Matrix Matrix::operator *= (Matrix m) {
  213. if (rows == m.GetRows() && columns == m.GetColumns()) {
  214. for (int i = 0; i < rows; i++) {
  215. for (int j = 0; j < columns; j++) {
  216. _tab[i][j] *= m._tab[i][j];
  217. }
  218. }
  219. }
  220. }
  221. bool Matrix::operator == (Matrix m) {
  222. if (rows == m.GetRows() && columns == m.GetColumns()) {
  223. bool r = true;
  224. for (int i = 0; i < rows; i++) {
  225. for (int j = 0; j < columns; j++) {
  226. if (_tab[i][j] != m._tab[i][j]) {
  227. r = false;
  228. }
  229. }
  230. }
  231. return r;
  232. }
  233. return NULL;
  234. }
  235. bool Matrix::operator != (Matrix m) {
  236. if (rows == m.GetRows() && columns == m.GetColumns()) {
  237. bool r = false;
  238. for (int i = 0; i < rows; i++) {
  239. for (int j = 0; j < columns; j++) {
  240. if (_tab[i][j] != m._tab[i][j]) {
  241. r = true;
  242. }
  243. }
  244. }
  245. return r;
  246. }
  247. return NULL;
  248. }
  249. Matrix Matrix::operator >> (Matrix m) {
  250. for (int i = 0; i < m.GetRows(); i++) {
  251. for (int j = 0; j < m.GetColumns(); j++) {
  252. cout << std::endl << "Matrix[" << i << "][" << j << "]";
  253. cin >> m._tab[i][j];
  254. }
  255. }
  256. }
  257. void Matrix::operator << (Matrix m) {
  258. if (rows == m.GetRows() && columns == m.GetColumns()) {
  259. m.Print();
  260. }
  261. }
  262. //.cpp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement