Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <iomanip>
- #include <string.h>
- #define ZERO 0.000001
- using namespace std;
- float** matrix = new float* [4];
- float** safematrix = new float* [4];
- float* answers = new float[3];
- char* name = new char[15];
- bool exist_solution=1;
- void printmatrix(float**);
- void fromfile(FILE*, float**);
- void matrixgen(float**);
- float swap_row(float**, int, int);
- void solution();
- void gauss_straight(float**);
- void gauss_partly(float**);
- void name_get(char*);
- void choose_way();
- void clean_stdin();
- void gauss_full(float**);
- float swap_col(float**, int, int);
- void QR(float**);
- void Spin(float**, int, int, float, float);
- void matrixcopy();
- void discrepancy();
- int main()
- {
- matrixgen(matrix);
- matrixgen(safematrix);
- name_get(name);
- matrixcopy();
- printmatrix(matrix);
- choose_way();
- }
- void choose_way()
- {
- char Key;
- printf("\n ENTER NUMBER TO CHOOSE METHOD:"
- "\n 1 - Gaussian elimination"
- "\n 2 - Gaussian elimination with parial selection of the lead element "
- "\n 3 - Gaussian elimination with full selection of the lead element"
- "\n 4 - QR method\n");
- cin >> Key;
- switch (Key)
- {
- clean_stdin;
- case '1':
- cout << "gauss straight" << endl;
- gauss_straight(matrix);
- if (exist_solution)
- {
- cout << "discrepancy is" << endl;
- discrepancy();
- }
- break;
- case '2':
- cout << "gauss partial selection" << endl;
- gauss_partly(matrix);
- if (exist_solution)
- {
- cout << "discrepancy is" << endl;
- discrepancy();
- }
- break;
- case'3':
- cout << "gauss full selection" << endl;
- gauss_full(matrix);
- if (exist_solution)
- {
- cout << "discrepancy is" << endl;
- discrepancy();
- }
- break;
- case'4':
- cout << "QR method" << endl;
- QR(matrix);
- if (exist_solution)
- {
- cout << "discrepancy is" << endl;
- discrepancy();
- }
- break;
- default:break;
- }
- }
- void printmatrix(float** matrix)
- {
- cout.setf(ios::fixed, ios::floatfield);
- cout.setf(ios::showpoint);
- for (int i = 0; i < 4; i++)
- {
- for (int j = 0; j < 5; j++)
- {
- cout.width(10);
- cout.precision(7);
- if(j!=4)
- cout << matrix[i][j] << setprecision(7) <<"*x"<<j+1<<" + ";
- else
- {
- cout.width(2);
- cout.precision(7);
- cout << "=" << matrix[i][j];
- }
- }
- cout << endl;
- }
- }//печать матрицы
- void fromfile(FILE* file, float** matrix)
- {
- int countrow=0;
- int countcoll=0;
- char* string= new char[128];
- while (fgets(string, 128, file) > 0)
- {
- if (strlen(string) < 12)
- continue;
- char* pch = strtok(string, " \n");
- while (pch != NULL) // пока есть лексемы
- {
- matrix[countrow][countcoll] = atof(pch);
- pch = strtok(NULL," \n");
- countcoll++;
- }
- countrow++;
- countcoll = 0;
- }
- rewind(file);
- fclose(file);
- }//запись из файла в матрицу
- void matrixgen(float** matrix)// создание матрицы
- {
- for (int i = 0; i < 4; i++)
- {
- matrix[i] = new float[5]; // Создаем элементы
- }
- }
- float swap_row(float** matrix, int n, int k)//n меняем с k
- {
- float buffer;
- for (int j = 0; j < 5; j++)
- {
- buffer = matrix[n][j];
- matrix[n][j] = matrix[k][j];
- matrix[k][j] = buffer;
- }
- return(**matrix);
- }
- float swap_col(float** matrix, int n, int k)//n меняем с k
- {
- float buffer;
- for (int i = 0; i < 4; i++)
- {
- buffer = matrix[i][n];
- matrix[i][n] = matrix[i][k];
- matrix[i][k] = buffer;
- }
- return(**matrix);
- }
- void solution()
- {
- answers[3] = matrix[3][4] / matrix[3][3];
- answers[2] = (matrix[2][4] - answers[3] * matrix[2][3]) / matrix[2][2];
- answers[1] = (matrix[1][4] - answers[2] * matrix[1][2] - answers[3] * matrix[1][3]) / matrix[1][1];
- answers[0] = (matrix[0][4] - answers[1] * matrix[0][1] - answers[2] * matrix[0][2] - answers[3] * matrix[0][3])/matrix[0][0];
- cout << endl << "Solutions for this SLAU are " << endl << endl;
- for (int i = 1; i < 5; i++)
- cout << "x" << i << "=" << answers[i - 1] << endl;
- }
- void gauss_straight(float** matrix)
- {
- int zerorow=-1;
- float buff;
- for (int row = 0; row < 4; row++)
- {
- if (matrix[row][row] == 0)//если главный элемент равен 0, то нужно найти другую строку и поменять их местами
- {
- for (int i = row; i < 4; i++)
- if (matrix[i][row] != 0)
- zerorow = i;
- if (zerorow == -1) { cout << "SLAU doesn't have solutions/infinite solutions"; exist_solution = 0; return; }
- swap_row(matrix, row, zerorow);
- zerorow = -1;
- }
- for (int extrarow = row + 1; extrarow < 4; extrarow++)
- {
- buff = -matrix[extrarow][row];
- for (int col = 0; col < 6; col++)
- {
- matrix[extrarow][col] += matrix[row][col] * buff / matrix[row][row];
- }
- }
- }
- puts("SLAU after Gauss straight transformation");
- printmatrix(matrix);
- solution();
- }
- void gauss_partly(float** matrix)
- {
- float max = 0;
- int zerorow = -1;
- float buff;
- for (int row = 0; row < 4; row++)
- {
- for (int i = row; i < 4; i++)
- if (fabs(matrix[i][row]) > max)
- {
- zerorow = i;
- max = fabs(matrix[i][row]);
- }
- if (fabs(max) < FLT_EPSILON) { cout << "SLAU doesn't have solutions/infinite solutions"; exist_solution = 0; return; }
- swap_row(matrix, row, zerorow);
- max = 0;
- zerorow = -1;
- for (int extrarow = row + 1; extrarow < 4; extrarow++)
- {
- buff = -matrix[extrarow][row];
- for (int col = 0; col < 6; col++)
- {
- matrix[extrarow][col] += matrix[row][col] * buff / matrix[row][row];
- }
- }
- }
- puts("Row echelon form");
- printmatrix(matrix);
- solution();
- }
- void name_get(char* name)
- {
- FILE* file;
- puts("input file name");
- cin >> name;
- strcat(name, ".txt");
- fopen_s(&file, name, "r");
- if (file == NULL)
- {
- puts("file not found");
- exit(1);
- }
- delete[] name;
- fromfile(file, matrix);
- cout << "file read successfully" << endl;
- }
- void clean_stdin()//очистка ввода от косых n fflush не работает в VC2019 :(
- {
- int c;
- do {
- c = getchar();
- } while (c != '\n' && c != EOF);
- }
- void gauss_full(float** matrix)
- {
- float max = 0;
- int zerorow = -1;
- int zerocollum = -1;
- float buff;
- for (int row = 0; row < 4; row++)
- {
- for (int i = row; i < 4; i++)
- for(int j=row; j<4;j++)
- if (fabs(matrix[i][j]) > max)
- {
- zerocollum = j;
- zerorow = i;
- max = fabs(matrix[i][j]);
- }
- if (fabs(max) < FLT_EPSILON) { cout << "SLAU doesn't have solutions/infinite solutions"; exist_solution = 0; return; }
- swap_row(matrix, row, zerorow);
- swap_col(matrix, row, zerocollum);
- max = 0;
- zerorow = -1;
- for (int extrarow = row + 1; extrarow < 4; extrarow++)
- {
- buff = -matrix[extrarow][row];
- for (int col = 0; col < 6; col++)
- {
- matrix[extrarow][col] += matrix[row][col] * buff / matrix[row][row];
- }
- }
- }
- puts("Row echelon form");
- printmatrix(matrix);
- solution();
- }
- void QR(float** matrix)
- {
- int NomMax;
- float c, s;
- for (int j = 0; j < 4; j++)
- {
- for (int i = j + 1; i < 4; i++)
- {
- if (matrix[i][j] != 0)
- {
- c = matrix[j][j] / sqrt(pow(matrix[j][j],2) + pow(matrix[i][j],2));
- s = matrix[i][j] / sqrt(pow(matrix[j][j], 2) + pow(matrix[i][j],2));
- Spin(matrix, j, i, c, s);
- }
- }
- }
- if(matrix[3][4]/matrix[3][3]==INFINITY) { cout << "SLAU doesn't have solutions/infinite solutions"; exist_solution = 0; return; }
- puts("Row echelon form");
- printmatrix(matrix);
- solution();
- }
- void Spin(float** matrix, int a, int b, float c, float s)
- {
- int j;
- float* buffer = new float[4];
- for (j = 0; j <5; j++)
- buffer[j] = matrix[a][j];
- for (j = 0; j < 5; j++)
- {
- matrix[a][j] *= c;
- matrix[a][j] += s * matrix[b][j];
- }
- for (j = 0; j < 5; j++)
- {
- matrix[b][j] *= c;
- matrix[b][j] -= s * buffer[j];
- }
- }
- void matrixcopy()
- {
- for (int i = 0; i < 4; i++)
- for (int j = 0; j < 5; j++)
- safematrix[i][j] = matrix[i][j];
- }
- void discrepancy()
- {
- float discrepancy = 0;
- for (int i = 0; i < 4; i++)
- {
- for (int j = 0; j < 4; j++)
- {
- discrepancy += safematrix[i][j] * answers[j];
- }
- cout << " discrepancy for " << i + 1 << " row is " << fabs(safematrix[i][4] - discrepancy) << endl;
- discrepancy = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement