Advertisement
Zennoma

PRACTICE

May 26th, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.18 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string.h>
  5. #define ZERO 0.000001
  6.  
  7. using namespace  std;
  8. float** matrix = new float* [4];
  9. float** safematrix = new float* [4];
  10. float* answers = new float[3];
  11. char* name = new char[15];
  12. bool exist_solution=1;
  13.  
  14. void printmatrix(float**);
  15. void fromfile(FILE*, float**);
  16. void matrixgen(float**);
  17. float swap_row(float**, int, int);
  18. void solution();
  19. void gauss_straight(float**);
  20. void gauss_partly(float**);
  21. void name_get(char*);
  22. void choose_way();
  23. void clean_stdin();
  24. void gauss_full(float**);
  25. float swap_col(float**, int, int);
  26. void QR(float**);
  27. void Spin(float**, int, int, float, float);
  28. void matrixcopy();
  29. void discrepancy();
  30.  
  31. int main()
  32. {
  33.     matrixgen(matrix);
  34.     matrixgen(safematrix);
  35.     name_get(name);
  36.     matrixcopy();
  37.     printmatrix(matrix);
  38.     choose_way();
  39. }
  40. void choose_way()
  41. {
  42.     char Key;
  43.     printf("\n ENTER NUMBER TO CHOOSE METHOD:"
  44.         "\n 1 - Gaussian elimination"
  45.         "\n 2 - Gaussian elimination with parial selection of the lead element "
  46.         "\n 3 -  Gaussian elimination with full selection of the lead element"
  47.         "\n 4 - QR method\n");
  48.  
  49.     cin >> Key;
  50.     switch (Key)
  51.     {
  52.         clean_stdin;
  53.     case '1':
  54.         cout << "gauss straight" << endl;
  55.         gauss_straight(matrix);
  56.         if (exist_solution)
  57.         {
  58.             cout << "discrepancy is" << endl;
  59.             discrepancy();
  60.         }
  61.         break;
  62.     case '2':
  63.         cout << "gauss partial selection" << endl;
  64.         gauss_partly(matrix);
  65.         if (exist_solution)
  66.         {
  67.             cout << "discrepancy is" << endl;
  68.             discrepancy();
  69.         }
  70.         break;
  71.     case'3':
  72.         cout << "gauss full selection" << endl;
  73.         gauss_full(matrix);
  74.         if (exist_solution)
  75.         {
  76.             cout << "discrepancy is" << endl;
  77.             discrepancy();
  78.         }
  79.         break;
  80.  
  81.     case'4':
  82.         cout << "QR method" << endl;
  83.         QR(matrix);
  84.         if (exist_solution)
  85.         {
  86.             cout << "discrepancy is" << endl;
  87.             discrepancy();
  88.         }
  89.         break;
  90.     default:break;
  91.     }
  92. }
  93. void printmatrix(float** matrix)
  94. {
  95.     cout.setf(ios::fixed, ios::floatfield);
  96.     cout.setf(ios::showpoint);
  97.    
  98.     for (int i = 0; i < 4; i++)
  99.     {
  100.         for (int j = 0; j < 5; j++)
  101.         {
  102.            
  103.             cout.width(10);
  104.             cout.precision(7);
  105.             if(j!=4)
  106.             cout << matrix[i][j] << setprecision(7) <<"*x"<<j+1<<" + ";
  107.             else
  108.             {
  109.                 cout.width(2);
  110.                 cout.precision(7);
  111.                 cout << "=" << matrix[i][j];
  112.             }
  113.         }
  114.         cout << endl;
  115.     }
  116. }//печать матрицы
  117. void fromfile(FILE* file, float** matrix)
  118. {
  119.     int countrow=0;
  120.     int countcoll=0;
  121.     char* string= new char[128];
  122.     while (fgets(string, 128, file) > 0)
  123.     {
  124.         if (strlen(string) < 12)
  125.             continue;
  126.         char* pch = strtok(string, " \n");
  127.             while (pch != NULL)                         // пока есть лексемы
  128.             {
  129.                 matrix[countrow][countcoll] = atof(pch);
  130.                 pch = strtok(NULL," \n");
  131.                 countcoll++;
  132.             }
  133.             countrow++;
  134.             countcoll = 0;
  135.     }
  136.     rewind(file);
  137.     fclose(file);
  138. }//запись из файла в матрицу
  139. void matrixgen(float** matrix)// создание матрицы
  140. {
  141.     for (int i = 0; i < 4; i++)
  142.     {
  143.         matrix[i] = new float[5]; // Создаем элементы
  144.     }
  145. }
  146. float swap_row(float** matrix, int n, int k)//n меняем с k
  147. {
  148.     float buffer;
  149.     for (int j = 0; j < 5; j++)
  150.     {
  151.         buffer = matrix[n][j];
  152.         matrix[n][j] = matrix[k][j];
  153.         matrix[k][j] = buffer;
  154.     }
  155.     return(**matrix);
  156. }
  157. float swap_col(float** matrix, int n, int k)//n меняем с k
  158. {
  159.     float buffer;
  160.     for (int i = 0; i < 4; i++)
  161.     {
  162.         buffer = matrix[i][n];
  163.         matrix[i][n] = matrix[i][k];
  164.         matrix[i][k] = buffer;
  165.     }
  166.     return(**matrix);
  167. }
  168. void solution()
  169. {
  170.     answers[3] = matrix[3][4] / matrix[3][3];
  171.     answers[2] = (matrix[2][4] - answers[3] * matrix[2][3]) / matrix[2][2];
  172.     answers[1] = (matrix[1][4] - answers[2] * matrix[1][2] - answers[3] * matrix[1][3]) / matrix[1][1];
  173.     answers[0] = (matrix[0][4] - answers[1] * matrix[0][1] - answers[2] * matrix[0][2] - answers[3] * matrix[0][3])/matrix[0][0];
  174.     cout << endl << "Solutions for this SLAU are " << endl << endl;
  175.     for (int i = 1; i < 5; i++)
  176.         cout << "x" << i << "=" << answers[i - 1] << endl;
  177. }
  178. void gauss_straight(float** matrix)
  179. {
  180.     int zerorow=-1;
  181.     float buff;
  182.     for (int row = 0; row < 4; row++)
  183.     {
  184.         if (matrix[row][row] == 0)//если главный элемент равен 0, то нужно найти другую строку и поменять их местами
  185.         {
  186.             for (int i = row; i < 4; i++)
  187.                 if (matrix[i][row] != 0)
  188.                     zerorow = i;
  189.             if (zerorow == -1) { cout << "SLAU doesn't have solutions/infinite solutions"; exist_solution = 0; return; }
  190.             swap_row(matrix, row, zerorow);
  191.             zerorow = -1;
  192.         }
  193.         for (int extrarow = row + 1; extrarow < 4; extrarow++)
  194.         {
  195.             buff = -matrix[extrarow][row];
  196.             for (int col = 0; col < 6; col++)
  197.             {
  198.  
  199.                 matrix[extrarow][col] += matrix[row][col] * buff / matrix[row][row];
  200.             }
  201.         }
  202.     }
  203.     puts("SLAU after Gauss straight transformation");
  204.     printmatrix(matrix);
  205.     solution();
  206. }
  207. void gauss_partly(float** matrix)
  208. {
  209.     float max = 0;
  210.     int zerorow = -1;
  211.     float buff;
  212.     for (int row = 0; row < 4; row++)
  213.     {
  214.         for (int i = row; i < 4; i++)
  215.             if (fabs(matrix[i][row]) > max)
  216.             {
  217.                 zerorow = i;
  218.                 max = fabs(matrix[i][row]);
  219.             }
  220.         if (fabs(max) < FLT_EPSILON) { cout << "SLAU doesn't have solutions/infinite solutions"; exist_solution = 0; return; }
  221.         swap_row(matrix, row, zerorow);
  222.         max = 0;
  223.         zerorow = -1;
  224.         for (int extrarow = row + 1; extrarow < 4; extrarow++)
  225.         {
  226.             buff = -matrix[extrarow][row];
  227.             for (int col = 0; col < 6; col++)
  228.             {
  229.  
  230.                 matrix[extrarow][col] += matrix[row][col] * buff / matrix[row][row];
  231.             }
  232.         }
  233.     }
  234.     puts("Row echelon form");
  235.     printmatrix(matrix);
  236.     solution();
  237. }
  238. void name_get(char* name)
  239. {
  240.     FILE* file;
  241.     puts("input file name");
  242.     cin >> name;
  243.     strcat(name, ".txt");
  244.     fopen_s(&file, name, "r");
  245.     if (file == NULL)
  246.     {
  247.         puts("file not found");
  248.         exit(1);
  249.     }
  250.     delete[] name;
  251.     fromfile(file, matrix);
  252.     cout << "file read successfully" << endl;
  253. }
  254. void clean_stdin()//очистка ввода от косых n fflush не работает в VC2019 :(
  255. {
  256.     int c;
  257.     do {
  258.         c = getchar();
  259.     } while (c != '\n' && c != EOF);
  260. }
  261. void gauss_full(float** matrix)
  262. {
  263.  
  264.     float max = 0;
  265.     int zerorow = -1;
  266.     int zerocollum = -1;
  267.     float buff;
  268.     for (int row = 0; row < 4; row++)
  269.     {
  270.         for (int i = row; i < 4; i++)
  271.             for(int j=row; j<4;j++)
  272.             if (fabs(matrix[i][j]) > max)
  273.             {
  274.                 zerocollum = j;
  275.                 zerorow = i;
  276.                 max = fabs(matrix[i][j]);
  277.             }
  278.         if (fabs(max) < FLT_EPSILON) { cout << "SLAU doesn't have solutions/infinite solutions"; exist_solution = 0; return; }
  279.         swap_row(matrix, row, zerorow);
  280.         swap_col(matrix, row, zerocollum);
  281.         max = 0;
  282.         zerorow = -1;
  283.         for (int extrarow = row + 1; extrarow < 4; extrarow++)
  284.         {
  285.             buff = -matrix[extrarow][row];
  286.             for (int col = 0; col < 6; col++)
  287.             {
  288.  
  289.                 matrix[extrarow][col] += matrix[row][col] * buff / matrix[row][row];
  290.             }
  291.         }
  292.     }
  293.     puts("Row echelon form");
  294.     printmatrix(matrix);
  295.     solution();
  296.  
  297. }
  298. void QR(float** matrix)
  299. {
  300.     int NomMax;
  301.     float c, s;
  302.     for (int j = 0; j < 4; j++)
  303.     {
  304.         for (int i = j + 1; i < 4; i++)
  305.         {
  306.             if (matrix[i][j] != 0)
  307.             {
  308.                 c = matrix[j][j] / sqrt(pow(matrix[j][j],2) + pow(matrix[i][j],2));
  309.                 s = matrix[i][j] / sqrt(pow(matrix[j][j], 2) + pow(matrix[i][j],2));
  310.                 Spin(matrix, j, i, c, s);
  311.             }
  312.         }
  313.     }
  314.     if(matrix[3][4]/matrix[3][3]==INFINITY) { cout << "SLAU doesn't have solutions/infinite solutions"; exist_solution = 0; return; }
  315.     puts("Row echelon form");
  316.     printmatrix(matrix);
  317.     solution();
  318. }
  319. void Spin(float** matrix, int a, int b, float c, float s)
  320. {
  321.     int j;
  322.     float* buffer = new float[4];
  323.     for (j = 0; j <5; j++)
  324.         buffer[j] = matrix[a][j];
  325.     for (j = 0; j < 5; j++)
  326.     {
  327.         matrix[a][j] *= c;
  328.         matrix[a][j] += s * matrix[b][j];
  329.     }
  330.     for (j = 0; j < 5; j++)
  331.     {
  332.         matrix[b][j] *= c;
  333.         matrix[b][j] -= s * buffer[j];
  334.     }
  335. }
  336. void matrixcopy()
  337. {
  338.     for (int i = 0; i < 4; i++)
  339.         for (int j = 0; j < 5; j++)
  340.             safematrix[i][j] = matrix[i][j];
  341. }
  342. void discrepancy()
  343. {
  344.     float discrepancy = 0;
  345.     for (int i = 0; i < 4; i++)
  346.     {
  347.         for (int j = 0; j < 4; j++)
  348.         {
  349.             discrepancy += safematrix[i][j] * answers[j];
  350.         }
  351.         cout << " discrepancy for " << i + 1 << " row is " << fabs(safematrix[i][4] - discrepancy) << endl;
  352.         discrepancy = 0;
  353.     }
  354. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement