Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include "Matrix.hpp"
  4. using namespace std;
  5.  
  6. // получение элемента матрицы
  7. double Matrix::GetAij(int i, int j) const {
  8. if((i>=m)||(j>=n)||(i<0)||(j<0))
  9. throw "Ошибка! НЕверные индексы! Выход за границы массива.";
  10. return p[i][j];
  11.  
  12. }
  13. // вставка элемента матрицы
  14. void Matrix::SetAij(int i, int j, double ch) {
  15. if((i>=m)||(j>=n)||(i<0)||(j<0))
  16. throw "Ошибка! НЕверные индексы! Выход за границы массива.";
  17. p[i][j]=ch;
  18.  
  19. }
  20.  
  21. // задание ID
  22. int Matrix::NextID=0;
  23.  
  24. //функция получения ID
  25. int Matrix::GetID()const{return ID;}
  26.  
  27. // конструктор без параметров
  28. Matrix::Matrix(): m(0), n(0), p(NULL), ID(++NextID) {}
  29.  
  30. // конструктор с параметрами
  31. Matrix::Matrix(int am, int an): ID(++NextID) {
  32. if (am<=0 || an<=0)
  33. throw "Ошибка! НЕверно задано количество строк/столбцов!";
  34. m=am; n=an;
  35. p=new double*[m];
  36. for (int i=0; i<m; i++)
  37. p[i]=new double[n];
  38. for (int i=0; i<m; i++)
  39. for (int j=0; j<n; j++)
  40. p[i][j]=0;
  41. }
  42.  
  43. // конструктор копирования
  44. Matrix::Matrix(const Matrix& b): ID(++NextID) {
  45. m=b.m; n=b.n;
  46. p=new double*[m];
  47. for (int i=0; i<m; i++)
  48. p[i]=new double[n];
  49. for (int i=0; i<m; i++)
  50. for (int j=0; j<n; j++)
  51. p[i][j]=b.p[i][j];
  52. }
  53.  
  54. // деструктор
  55. Matrix::~Matrix() {
  56. for (int i=0; i<m; i++)
  57. delete []p[i];
  58. delete []p;
  59. m=0; n=0;
  60. }
  61.  
  62. // оператор присваивания
  63. Matrix& Matrix::operator=(const Matrix& b) {
  64. if (*this==b) // -/&
  65. return *this;
  66. for (int i=0; i<m; i++)
  67. delete []p[i];
  68. delete []p;
  69. m=b.m; n=b.n;
  70. p=new double*[m];
  71. for (int i=0; i<m; i++)
  72. p[i]=new double[n];
  73. for (int i=0; i<m; i++)
  74. for (int j=0; j<n; j++)
  75. p[i][j]=b.p[i][j];
  76. return *this;
  77. }
  78.  
  79. // ________________составные операторы присваивания_____________________
  80.  
  81. // сложение + присваиваине
  82. Matrix& Matrix::operator+=(const Matrix& b) {
  83. if (m!=b.m || n!=b.n)
  84. throw "Ошибка! Матрицы должны быть равных размеров!(сложение)";
  85. for (int i=0; i<m; i++)
  86. for (int j=0; j<n; j++)
  87. p[i][j]+=b.p[i][j];
  88. return *this;
  89. }
  90.  
  91. // вычитание + присваивание
  92. Matrix& Matrix::operator-=(const Matrix& b) {
  93. if (m!=b.m || n!=b.n)
  94. throw "Ошибка! Матрицы должны быть равных размеров!(разность)";
  95. for (int i=0; i<m; i++)
  96. for (int j=0; j<n; j++)
  97. p[i][j]-=b.p[i][j];
  98. return *this;
  99. }
  100.  
  101. // умножение + присваивание
  102. Matrix& Matrix::operator*=(const Matrix& b) {
  103. if (n!=b.m)
  104. throw "Ошибка! Неверный размер матриц(число столбцов в 1 должно = числу строк 2). ";
  105. Matrix result(m, b.n);
  106. for (int i=0; i<m; i++)
  107. for (int j=0; j<b.n; j++)
  108. for (int k=0; k<n; k++)
  109. result.p[i][j]+=p[i][k]*b.p[k][j];
  110. *this=result;
  111. return *this;
  112. }
  113.  
  114. // умножение на константу + присваивание
  115. Matrix& Matrix::operator*=(double c) {
  116. for (int i=0; i<m; i++)
  117. for (int j=0; j<n; j++)
  118. p[i][j]*=c;
  119. return *this;
  120. }
  121.  
  122. // деление на константу + присваивание
  123. Matrix& Matrix::operator/=(double c) {
  124. for (int i=0; i<m; i++)
  125. for (int j=0; j<n; j++)
  126. p[i][j]/=c;
  127. return *this;
  128. }
  129.  
  130. //_______________Бинарные арифметические операции________________
  131.  
  132.  
  133. // умножение на константу (справа)
  134. Matrix Matrix::operator*(double c) {
  135. Matrix b(*this);
  136. for (int i=0; i<b.m; i++)
  137. for (int j=0; j<b.n; j++)
  138. b.p[i][j]*=c;
  139. return b;
  140. }
  141.  
  142. // дружественное умножение (константа слева)
  143. Matrix operator*(double c, const Matrix& a) {
  144. Matrix b(a);
  145. for (int i=0; i<b.m; i++)
  146. for (int j=0; j<b.n; j++)
  147. b.p[i][j]*=c;
  148. return b;
  149. }
  150.  
  151. // деление на константу
  152. Matrix Matrix::operator/(double c) {
  153. Matrix b(*this);
  154. if (c==0){
  155. throw "Ошибка! На нуль делить нельзя!";
  156. }
  157. for (int i=0; i<b.m; i++)
  158. for (int j=0; j<b.n; j++)
  159. b.p[i][j]/=c;
  160. return b;
  161. }
  162.  
  163.  
  164. // сложение матриц
  165. Matrix Matrix::operator+(const Matrix& b) {
  166. Matrix a(*this);
  167. a+=b;
  168. return a;
  169. }
  170.  
  171. // вычитание матриц
  172. Matrix Matrix::operator-(const Matrix& b) {
  173. Matrix a(*this);
  174. a-=b;
  175. return a;
  176. }
  177.  
  178. // умножение матриц
  179. Matrix Matrix::operator*(const Matrix& b) {
  180. Matrix a(*this);
  181. a*=b;
  182. return a;
  183. }
  184.  
  185. // сравнение матриц
  186. bool Matrix::operator==(const Matrix& b) const {
  187. if (m!=b.m || n!=b.n)
  188. return false;
  189. for (int i=0; i<m; i++)
  190. for (int j=0; j<n; j++)
  191. if (p[i][j]!=b.p[i][j])
  192. return false;
  193. return true;
  194. }
  195.  
  196. // оператор не равно!
  197. bool Matrix::operator!=(const Matrix& b) const {
  198. return !(*this==b);
  199. }
  200.  
  201. // ввод матриц
  202. istream& operator>>(istream& cin, Matrix& mtr) {
  203. if (mtr.m==0 || mtr.n==0)
  204. throw "Ошибка! Нет матрицы размером 0x0";
  205. cout<<"Введите матрицу по строкам, размером "<<mtr.m<<" x "<<mtr.n<<":"<<endl;
  206. for (int i=0; i<mtr.m; i++)
  207. for (int j=0; j<mtr.n; j++)
  208. cin>>mtr.p[i][j];
  209. return cin;
  210. }
  211. // вывод матриц
  212. ostream& operator<<(ostream& cout, const Matrix& mtr) {
  213. if (mtr.m==0 || mtr.n==0)
  214. throw "Ошибка! Нет матрицы размером 0x0";
  215. for (int i=0; i<mtr.m; i++) {
  216. for (int j=0; j<mtr.n; j++)
  217. cout<<setw(8)<<mtr.p[i][j];
  218. cout<<endl;
  219. }
  220. return cout;
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement