Advertisement
Lesnic

AGLA 3

Mar 13th, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <math.h>
  4.  
  5. using namespace std;
  6.  
  7. class Matrix;
  8. ostream& operator << (ostream& cout, const Matrix& matrix);
  9.  
  10. void output(int& step, Matrix base, bool permutation);
  11.  
  12.  
  13. class SquareMatrix {
  14.     friend Matrix;
  15.  
  16. private:
  17.     int size;
  18.     double** matrix;
  19.  
  20. public:
  21.     SquareMatrix(int size) {
  22.         (*this).size = size;
  23.         matrix = new double* [size];
  24.         for (int i = 0; i < size; i++) {
  25.             matrix[i] = new double[size];
  26.         }
  27.     }
  28.  
  29.     void permutation(int r1, int r2) {
  30.         double* temp = *(matrix + r1);
  31.         *(matrix + r1) = *(matrix + r2);
  32.         *(matrix + r2) = temp;
  33.     }
  34.  
  35.     friend istream& operator>>(istream& cin, SquareMatrix& base) {
  36.         for (int i = 0; i < base.size; i++)
  37.             for (int j = 0; j < base.size; j++)
  38.                 cin >> base.matrix[i][j];
  39.         return cin;
  40.     }
  41.  
  42.     friend ostream& operator<<(ostream& cout, SquareMatrix* base) {
  43.         for (int i = 0; i < base->size; i++) {
  44.             for (int j = 0; j < base->size; j++) {
  45.                 if (base->matrix[i][j] < 0.005 && base->matrix[i][j] > -0.005)
  46.                     cout << "0.00";
  47.                 else
  48.                     cout << fixed << setprecision(2) << base->matrix[i][j];
  49.  
  50.                 if (j != base->size - 1)
  51.                     cout << " ";
  52.             }
  53.             cout << endl;
  54.         }
  55.         return cout;
  56.     }
  57. };
  58.  
  59.  
  60. class ColumnVector {
  61.     friend Matrix;
  62.  
  63. private:
  64.     int size;
  65.     double* matrix;
  66.  
  67. public:
  68.     ColumnVector(int n) {
  69.         size = n;
  70.         matrix = new double[size];
  71.     }
  72.  
  73.     void permutation(int r1, int r2) {
  74.         double temp = matrix[r1];
  75.         matrix[r1] = matrix[r2];
  76.         matrix[r2] = temp;
  77.     }
  78.  
  79.     friend istream& operator>>(istream& cin, ColumnVector& base) {
  80.         int n;
  81.         cin >> n;
  82.         for (int i = 0; i < base.size; i++)
  83.             cin >> base.matrix[i];
  84.         return cin;
  85.     }
  86.  
  87.     friend ostream& operator<<(ostream& cout, ColumnVector* base) {
  88.         for (int i = 0; i < base->size; i++) {
  89.             if (base->matrix[i] < 0.005 && base->matrix[i] > -0.005)
  90.                 cout << "0.00";
  91.             else
  92.                 cout << fixed << setprecision(2) << base->matrix[i];
  93.  
  94.             cout << endl;
  95.         }
  96.         return cout;
  97.     }
  98. };
  99.  
  100.  
  101. class Matrix {
  102. private:
  103.     int size;
  104.     SquareMatrix* square;
  105.     ColumnVector* vector;
  106.  
  107. public:
  108.     Matrix(int size) {
  109.         this->size = size;
  110.         square = new SquareMatrix(size);
  111.         vector = new ColumnVector(size);
  112.     }
  113.  
  114.     void directOrder(int& step) {
  115.         for (int line = 0; line < size; line++) {
  116.             int maxLine = line;
  117.             for (int i = line + 1; i < size; i++) {
  118.                 if (abs(square->matrix[i][line]) > abs(square->matrix[maxLine][line]))
  119.                     maxLine = i;
  120.             }
  121.  
  122.             if (maxLine != line) {
  123.                 square->permutation(line, maxLine);
  124.                 vector->permutation(line, maxLine);
  125.                 output(step, *this, true);
  126.             }
  127.  
  128.             for (int i = line + 1; i < size; i++) {
  129.                 double constant = square->matrix[i][line] / square->matrix[line][line];
  130.                 if (square->matrix[i][line] != 0) {
  131.                     for (int j = 0; j < size; j++)
  132.                         square->matrix[i][j] -= square->matrix[line][j] * constant;
  133.                     vector->matrix[i] -= vector->matrix[line] * constant;
  134.                 }
  135.  
  136.                 output(step, *this, false);
  137.             }
  138.         }
  139.     }
  140.  
  141.     void reversOrder(int& step) {
  142.         double constant;
  143.         for (int i = size - 1; i >= 0; i--) {
  144.             for (int j = i - 1; j >= 0; j--) {
  145.                 constant = square->matrix[j][i] / square->matrix[i][i];
  146.                 square->matrix[j][i] -= square->matrix[i][i] * constant;
  147.                 vector->matrix[j] -= vector->matrix[i] * constant;
  148.                 output(step, *this, false);
  149.             }
  150.         }
  151.     }
  152.  
  153.     void normalization() {
  154.         for (int i = 0; i < size; i++) {
  155.             double now = square->matrix[i][i];
  156.             square->matrix[i][i] /= now;
  157.             vector->matrix[i] /= now;
  158.         }
  159.     }
  160.  
  161.     void result() {
  162.         cout << vector;
  163.     }
  164.  
  165.     friend istream& operator >> (istream& cin, Matrix& matrix) {
  166.         cin >> *(matrix.square);
  167.         cin >> *(matrix.vector);
  168.         return cin;
  169.     }
  170.  
  171.     friend ostream& operator << (ostream& cout, const Matrix& matrix) {
  172.         cout << matrix.square << matrix.vector;
  173.         return cout;
  174.     }
  175. };
  176.  
  177. void output(int& step, Matrix base, bool permutation) {
  178.     cout << "step #" << step << ": ";
  179.     if (permutation)
  180.         cout << "permutation";
  181.     else {
  182.         cout << "elimination";
  183.         ++step;
  184.     }
  185.     cout << endl << base;
  186. }
  187.  
  188. int main()
  189. {
  190.     int n;
  191.     cin >> n;
  192.     Matrix matrix(n);
  193.     cin >> matrix;
  194.  
  195.     int step = 1;
  196.     cout << "step #0:\n" << matrix;
  197.  
  198.     matrix.directOrder(step);
  199.     cout << "Way back:\n";
  200.     matrix.reversOrder(step);
  201.  
  202.     matrix.normalization();
  203.     cout << "Diagonal normalization:" << endl << matrix;
  204.     cout << "result:" << endl;
  205.     matrix.result();
  206.    
  207.     return 0;
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement