Mempron

SW2.1

May 20th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. void inPutCountEquations(int& countEquations);
  7. void inPutMatrix(const int countEquations, double* matrix[]);
  8. void inPutAnswer(const int countEquations, double yVector[]);
  9. void outPutMatrix(const int countEquations, double* matrix[], double yVector[]);
  10. void outSystemOfLinearEquations(const int countEquations, double* matrix[], double yVector[]);
  11. void findInColmn(const int countEquations, const double eps, double* matrix[], int& indexStringWithMaxElementInTheBegin, int& maxElementInColumn, int& k);
  12. void swapRows(const int countEquations, const int indexStringWithMaxElementInTheBegin, const double eps, double* matrix[], int& maxElementInColumn, double yVector[], int& k, int& flag);
  13. void normalizeRows(const int countEquations, const double eps, double* matrix[], double yVector[], int& k);
  14. void correctionRows(const int countEquations, double* matrix[]);
  15. void reverseSubstitutionRows(const int countEquations, double* matrix[], double yVector[], double xVector[], int& k);
  16. void showMeResult(const int countEquations, double xVector[]);
  17. void showMeError(const int countEquations, double* matrix[], double xVector[], double yVector[], double yVectorError[]);
  18. void cleanMemory(const int countEquations, double* matrix[], double* matrixCopy[], double xVector[], double yVector[], double yVectorCopy[], double yVectorError[]);
  19.  
  20.  
  21. int main()
  22. {
  23.     int countEquations = 0, indexStringWithMaxElementInTheBegin = 0, indexColumnWithMaxElementInTheBegin = 0, k = 0, maxElementInColumn = 0, maxElementInRow = 0, flag = 1;
  24.     const double eps = 1e-10;
  25.  
  26.     // Ввод количества уравнений
  27.     inPutCountEquations(countEquations);
  28.  
  29.     double** matrix = new double* [countEquations]; // Левая часть матрицы
  30.     double** matrixCopy = new double* [countEquations]; // Левая часть матрицы
  31.     double* yVector = new double[countEquations]; // Правая часть матрицы
  32.     double* yVectorCopy = new double[countEquations]; // Правая часть матрицы
  33.     double* yVectorError = new double[countEquations]; // Правая часть матрицы для проверки погрешности
  34.     double* xVector = new double[countEquations]; // Тут ответики у нас будут
  35.  
  36.     // Ввод левой части матрицы
  37.     inPutMatrix(countEquations, matrix);
  38.     // Ввод правой части матрицы
  39.     inPutAnswer(countEquations, yVector);
  40.  
  41.     // Копирование левой и правой части матрицы для дальнейшего расчёта погрешности
  42.     for (int j = 0; j < countEquations; j++) {
  43.         matrixCopy[j] = new double[countEquations];
  44.         for (int i = 0; i < countEquations; i++) {
  45.             matrixCopy[j][i] = matrix[j][i];
  46.         }
  47.         yVectorCopy[j] = yVector[j];
  48.     }
  49.  
  50.     // Вывод введеной матрицы
  51.     outPutMatrix(countEquations, matrix, yVector);
  52.  
  53.  
  54.     // Вывод полученной СЛАУ
  55.     outSystemOfLinearEquations(countEquations, matrix, yVector);
  56.  
  57.  
  58.     for (k = 0; k < countEquations; k++) {
  59.         findInColmn(countEquations, eps, matrix, indexStringWithMaxElementInTheBegin, maxElementInColumn, k);
  60.         swapRows(countEquations, indexStringWithMaxElementInTheBegin, eps, matrix, maxElementInColumn, yVector, k, flag);
  61.         if (!flag)
  62.             return 0;
  63.         normalizeRows(countEquations, eps, matrix, yVector, k);
  64.     }
  65.     correctionRows(countEquations, matrix);
  66.     reverseSubstitutionRows(countEquations, matrix, yVector, xVector, k);
  67.  
  68.     showMeResult(countEquations, xVector);
  69.  
  70.     showMeError(countEquations, matrixCopy, xVector, yVectorCopy, yVectorError);
  71.  
  72.  
  73.     // Удаление массивов
  74.     cleanMemory(countEquations, matrix, matrixCopy, xVector, yVector, yVectorCopy, yVectorError);
  75.     return 0;
  76. }
  77.  
  78. void inPutCountEquations(int& countEquations) {
  79.     do {
  80.         cout << "Enter count of equations: "; cin >> countEquations;
  81.     } while (countEquations < 1);
  82. }
  83. void inPutMatrix(const int countEquations, double* matrix[]) {
  84.     cout << "Enter your matrix: " << endl;
  85.     for (int j = 0; j < countEquations; j++) {
  86.         matrix[j] = new double[countEquations];
  87.         for (int i = 0; i < countEquations; i++) {
  88.             cin >> matrix[j][i];
  89.         }
  90.     }
  91. }
  92. void inPutAnswer(const int countEquations, double yVector[]) {
  93.     cout << "Enter your Y-vector: " << endl;
  94.     for (int j = 0; j < countEquations; j++) {
  95.             cin >> yVector[j];
  96.     }
  97. }
  98. void outPutMatrix(const int countEquations, double* matrix[], double yVector[]) {
  99.     system("cls");
  100.     cout << "You've entered: " << endl;
  101.     for (int m = 0; m < countEquations; m++) {
  102.         for (int k = 0; k < countEquations; k++)
  103.             cout << matrix[m][k] << " ";
  104.         cout << "| " << yVector[m];
  105.         cout << endl;
  106.     }
  107. }
  108. void outSystemOfLinearEquations(const int countEquations, double* matrix[], double yVector[]) {
  109.     cout << endl << "System of linear equations: " << endl;
  110.     for (int j = 0; j < countEquations; j++) {
  111.         for (int i = 0; i < countEquations; i++)
  112.         {
  113.             cout << matrix[j][i] << "*x" << i;
  114.             if (i < countEquations - 1)
  115.                 cout << " + ";
  116.         }
  117.         cout << " = " << yVector[j] << endl;
  118.     }
  119. }
  120. void findInColmn(const int countEquations, const double eps, double* matrix[], int& indexStringWithMaxElementInTheBegin, int& maxElementInColumn, int& k) {
  121.         // Поиск строки с максимальным a[i][k]
  122.         maxElementInColumn = abs(matrix[k][k]);
  123.         indexStringWithMaxElementInTheBegin = k;
  124.         for (int i = k; i < countEquations; i++)
  125.         {
  126.             if (abs(matrix[i][k]) > maxElementInColumn) {
  127.                 maxElementInColumn = abs(matrix[i][k]);
  128.                 indexStringWithMaxElementInTheBegin = i;
  129.             }
  130.         }
  131. }
  132. void swapRows(const int countEquations, const int indexStringWithMaxElementInTheBegin, const double eps, double* matrix[], int& maxElementInColumn, double yVector[], int& k, int& flag) {
  133.         if (maxElementInColumn < eps){
  134.             // Проверка, есть ли на главной диагонали нулевой элемент
  135.             cout << "The decision can't be obtained because of A[" << indexStringWithMaxElementInTheBegin + 1 << "][" << indexStringWithMaxElementInTheBegin + 1 << "]" << endl << "Non-degenerate matrix" << endl;
  136.             flag = 0;
  137.             return;
  138.         }
  139.         for (int j = 0; j < countEquations; j++) {
  140.             double temp = matrix[k][j];
  141.             matrix[k][j] = matrix[indexStringWithMaxElementInTheBegin][j];
  142.             matrix[indexStringWithMaxElementInTheBegin][j] = temp;
  143.         }
  144.         double tmp = yVector[k];
  145.         yVector[k] = yVector[indexStringWithMaxElementInTheBegin];
  146.         yVector[indexStringWithMaxElementInTheBegin] = tmp;
  147. }
  148. void normalizeRows(const int countEquations, const double eps, double* matrix[], double yVector[], int& k) {
  149.     for (int i = k; i < countEquations; i++) {
  150.         double tmp = matrix[i][k];
  151.         if (abs(tmp) < eps) continue; // для нулевого коэффициента пропустить
  152.         for (int j = 0; j < countEquations; j++)
  153.             matrix[i][j] /= tmp;
  154.         yVector[i] /= tmp;
  155.         if (i == k)  continue; // уравнение не вычитать само из себя
  156.         for (int j = 0; j < countEquations; j++)
  157.             matrix[i][j] = matrix[i][j] - matrix[k][j];
  158.         yVector[i] = yVector[i] - yVector[k];
  159.     }
  160. }
  161. void correctionRows(const int countEquations, double* matrix[]) {
  162.     for (int i = 0; i < countEquations; i++){
  163.         for (int j = 0; j < countEquations; j++) {
  164.             if (j < i)
  165.                 matrix[i][j] = 0;
  166.         }
  167.     }
  168. }
  169. void reverseSubstitutionRows(const int countEquations, double* matrix[], double yVector[], double xVector[], int& k) {
  170.     for (k = countEquations - 1; k >= 0; k--) {
  171.         xVector[k] = yVector[k];
  172.         for (int i = 0; i < k; i++)
  173.             yVector[i] = yVector[i] - matrix[i][k] * xVector[k];
  174.     }
  175. }
  176. void showMeResult(const int countEquations, double xVector[]) {
  177.     cout << endl;
  178.     for (int i = 0; i < countEquations; i++) {
  179.         cout << "x" << i << " = " << xVector[i] << endl;
  180.     }
  181.     cout << endl << "X = (";
  182.     for (int i = 0; i < countEquations; i++) {
  183.         if (i == countEquations - 1)
  184.             cout << xVector[i] << ")" << endl;
  185.         else
  186.             cout << xVector[i] << ", ";
  187.     }
  188. }
  189. void showMeError(const int countEquations, double* matrix[], double xVector[], double yVector[], double yVectorError[]) {
  190.     double max = 0;
  191.     for (int i = 0; i < countEquations; i++) {
  192.         yVectorError[i] = 0;
  193.         for (int j = 0; j < countEquations; j++) {
  194.             yVectorError[i] += matrix[i][j] * xVector[j];
  195.         }
  196.         yVectorError[i] = fabs(yVector[i] - yVectorError[i]);
  197.         if (yVectorError[i] > max)
  198.             max = yVectorError[i];
  199.     }
  200.     cout << "Max error: " << max << endl;
  201. }
  202. void cleanMemory(const int countEquations, double* matrix[], double* matrixCopy[], double xVector[], double yVector[], double yVectorCopy[], double yVectorError[]) {
  203.     for (int j = 0; j < countEquations; j++) {
  204.         delete[] matrix[j];
  205.         delete[] matrixCopy[j];
  206.     }
  207.     delete[] matrix;
  208.     delete[] xVector;
  209.     delete[] yVector;
  210.     delete[] yVectorCopy;
  211.     delete[] yVectorError;
  212. }
Add Comment
Please, Sign In to add comment