fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. class MatrixMN {
  6. private:
  7. int m, n;
  8. double** a;
  9.  
  10. public:
  11. // конструктор
  12. MatrixMN(int rows = 1, int cols = 1) {
  13. m = rows;
  14. n = cols;
  15.  
  16. a = new double*[m];
  17. for (int i = 0; i < m; i++)
  18. a[i] = new double[n]{0};
  19. }
  20.  
  21. // деструктор
  22. ~MatrixMN() {
  23. for (int i = 0; i < m; i++)
  24. delete[] a[i];
  25. delete[] a;
  26. }
  27.  
  28. // доступ до елементів
  29. double& at(int i, int j) {
  30. return a[i][j];
  31. }
  32.  
  33. // перевірка нульової матриці
  34. bool isZero() {
  35. for (int i = 0; i < m; i++)
  36. for (int j = 0; j < n; j++)
  37. if (a[i][j] != 0)
  38. return false;
  39. return true;
  40. }
  41.  
  42. // норма Фробеніуса
  43. double getNorm() {
  44. double s = 0;
  45. for (int i = 0; i < m; i++)
  46. for (int j = 0; j < n; j++)
  47. s += a[i][j] * a[i][j];
  48.  
  49. return sqrt(s);
  50. }
  51.  
  52. // додавання
  53. MatrixMN add(const MatrixMN& b) {
  54. MatrixMN r(m, n);
  55.  
  56. for (int i = 0; i < m; i++)
  57. for (int j = 0; j < n; j++)
  58. r.a[i][j] = a[i][j] + b.a[i][j];
  59.  
  60. return r;
  61. }
  62.  
  63. // +
  64. MatrixMN operator+(const MatrixMN& b) {
  65. return add(b);
  66. }
  67.  
  68. // -
  69. MatrixMN operator-(const MatrixMN& b) {
  70. MatrixMN r(m, n);
  71.  
  72. for (int i = 0; i < m; i++)
  73. for (int j = 0; j < n; j++)
  74. r.a[i][j] = a[i][j] - b.a[i][j];
  75.  
  76. return r;
  77. }
  78.  
  79. // *
  80. MatrixMN operator*(double x) {
  81. MatrixMN r(m, n);
  82.  
  83. for (int i = 0; i < m; i++)
  84. for (int j = 0; j < n; j++)
  85. r.a[i][j] = a[i][j] * x;
  86.  
  87. return r;
  88. }
  89.  
  90. // вивід
  91. friend ostream& operator<<(ostream& os, const MatrixMN& mtx) {
  92. for (int i = 0; i < mtx.m; i++) {
  93. for (int j = 0; j < mtx.n; j++)
  94. os << mtx.a[i][j] << " ";
  95. os << endl;
  96. }
  97. return os;
  98. }
  99. };
  100.  
  101. // ================= MAIN =================
  102. int main() {
  103. MatrixMN A(2, 2);
  104. MatrixMN B(2, 2);
  105.  
  106. // заповнення A
  107. A.at(0,0) = 1;
  108. A.at(0,1) = 2;
  109. A.at(1,0) = 3;
  110. A.at(1,1) = 4;
  111.  
  112. // заповнення B
  113. B.at(0,0) = 5;
  114. B.at(0,1) = 6;
  115. B.at(1,0) = 7;
  116. B.at(1,1) = 8;
  117.  
  118. cout << "Matrix A:\n" << A;
  119. cout << "Matrix B:\n" << B;
  120.  
  121. cout << "A + B:\n" << (A + B);
  122. cout << "A - B:\n" << (A - B);
  123. cout << "A * 2:\n" << (A * 2);
  124.  
  125. cout << "Norm of A: " << A.getNorm() << endl;
  126.  
  127. if (A.isZero())
  128. cout << "A is zero matrix\n";
  129. else
  130. cout << "A is NOT zero matrix\n";
  131.  
  132. return 0;
  133. }
  134.  
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
Matrix A:
1 2 
3 4 
Matrix B:
5 6 
7 8 
A + B:
6 8 
10 12 
A - B:
-4 -4 
-4 -4 
A * 2:
2 4 
6 8 
Norm of A: 5.47723
A is NOT zero matrix