Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- ifstream fin;
- ofstream fout;
- int inputData()
- {
- int n = 0;
- bool isIncorrect;;
- do {
- cin >> n;
- isIncorrect = false;
- if (cin.fail()) {
- cout << "Please, enter an integer number:" << endl;
- isIncorrect = true;
- cin.clear();
- while (cin.get() != '\n');
- }
- } while (isIncorrect);
- return n;
- }
- bool choose()
- {
- int inputNumber;
- bool isIncorrect;
- const int MIN_NUM = 0;
- const int MAX_NUM = 1;
- do {
- inputNumber = inputData();
- isIncorrect = false;
- if (cin.fail()) {
- isIncorrect = true;
- cout << "Please, enter a number." << endl;
- cin.clear();
- while (cin.get() != '\n');
- }
- if (!isIncorrect && (inputNumber < MIN_NUM || inputNumber > MAX_NUM)) {
- cout << "You are out of input range!" << endl;
- isIncorrect = true;
- }
- } while (isIncorrect);
- if (inputNumber == 1)
- return true;
- else
- return false;
- }
- int readCountRoots()
- {
- bool isIncorrect;
- int tmpCountRoots = 0;
- const int MIN_SIZE = 2;
- do {
- tmpCountRoots = inputData();
- isIncorrect = false;
- if (tmpCountRoots < MIN_SIZE) {
- cout << "Please, enter a number > 2:" << endl;
- isIncorrect = true;
- cin.clear();
- while (cin.get() != '\n');
- }
- } while (isIncorrect);
- return tmpCountRoots;
- }
- double** inputTriangularMatrixConsole(int tmpCountRoots)
- {
- double** arrOfMatrixElements = new double* [tmpCountRoots];
- for (int i = 0; i < tmpCountRoots; i++)
- {
- arrOfMatrixElements[i] = new double[tmpCountRoots];
- }
- for (int i = 0; i < tmpCountRoots; i++)
- for (int j = 0; j < tmpCountRoots; j++)
- arrOfMatrixElements[i][j] = 0;
- for (int i = 0; i < tmpCountRoots; i++)
- {
- for (int j = 0; j < tmpCountRoots; j++)
- {
- if (j - i > -1)
- {
- cout << "a[" << i + 1 << "," << j + 1 << "] = ";
- arrOfMatrixElements[i][j] = inputData();
- }
- }
- }
- return arrOfMatrixElements;
- }
- double* inputFreeMembersConsole(int tmpCountRoots)
- {
- double* arrOfFreeMembers = new double[tmpCountRoots];
- for (int i = 0; i < tmpCountRoots; i++)
- {
- cout << "b[" << i + 1 << "] = ";
- arrOfFreeMembers[i] = inputData();
- }
- return arrOfFreeMembers;
- }
- double** randomizeOtherElements(int tmpCountRoots, double** arrOfMatrixElements)
- {
- int q = 0;
- double max = arrOfMatrixElements[0][0];
- double min = arrOfMatrixElements[0][0];
- for (int i = 0; i < tmpCountRoots; i++) {
- for (int j = 0; j < tmpCountRoots; j++) {
- if (arrOfMatrixElements[i][j] > max)
- max = arrOfMatrixElements[i][j];
- if (arrOfMatrixElements[i][j] < min)
- min = arrOfMatrixElements[i][j];
- }
- }
- int diff = max - min;
- double** arrOfMatrixElementsRandom = new double* [tmpCountRoots];
- for (int i = 0; i < tmpCountRoots; i++)
- arrOfMatrixElementsRandom[i] = new double[tmpCountRoots];
- for (int i = 0; i < tmpCountRoots; i++)
- for (int j = 0; j < tmpCountRoots; j++)
- arrOfMatrixElementsRandom[i][j] = 0;
- for (int i = 0; i < tmpCountRoots; i++) {
- for (int j = 0; j < tmpCountRoots; j++) {
- if (j - i < 0) {
- q = min + rand() % diff;
- arrOfMatrixElementsRandom[i][j] = q;
- }
- }
- }
- return arrOfMatrixElementsRandom;
- }
- double** createMatrix(double** arrOfMatrixElements, int tmpCountRoots, double** arrOfMatrixElementsRandom, double* arrOfFreeMembers)
- {
- double** matrix = new double* [tmpCountRoots];
- for (int i = 0; i < tmpCountRoots; i++)
- matrix[i] = new double[tmpCountRoots + 1];
- for (int i = 0; i < tmpCountRoots; i++) {
- for (int j = 0; j < tmpCountRoots; j++) {
- if (j - i < 0)
- matrix[i][j] = arrOfMatrixElementsRandom[i][j];
- if (j - i > -1)
- matrix[i][j] = arrOfMatrixElements[i][j];
- }
- }
- for (int i = 0; i < tmpCountRoots; i++) {
- matrix[i][tmpCountRoots] = arrOfFreeMembers[i];
- }
- return matrix;
- }
- void writeMatrixInC(double** matrix, int tmpCountRoots)
- {
- int lastNum = tmpCountRoots + 1;
- cout << "Your virgin matrix:" << endl;
- for (int i = 0; i < tmpCountRoots; i++) {
- for (int j = 0; j < lastNum; j++) {
- cout << matrix[i][j] << " ";
- }
- cout << endl;
- }
- }
- void moveStrings(double** matrix, int firstRow, int secondRow, int tmpCountRoots)
- {
- int lastNum = tmpCountRoots + 1;
- double r;
- if ((firstRow < lastNum) && (secondRow < lastNum)) {
- for (int j = 0; j < lastNum; j++) {
- r = matrix[secondRow][j];
- matrix[secondRow][j] = matrix[firstRow][j];
- matrix[firstRow][j] = r;
- }
- }
- }
- double** newMatrix(double** matrix, int tmpCountRoots)
- {
- int tmpI, lastNum1, lastNum2;
- double k;
- lastNum1 = tmpCountRoots - 1;
- lastNum2 = tmpCountRoots + 1;
- for (int j = 0; j < tmpCountRoots; j++) {
- if ((matrix[j][j] == 0) && (j < lastNum1)) {
- tmpI = j + 1;
- do {
- if (matrix[tmpI][j] != 0) {
- moveStrings(matrix, j, tmpI, tmpCountRoots);
- }
- } while ((matrix[j][j] == 0));
- }
- for (int i = (j + 1); i < tmpCountRoots; i++) {
- k = matrix[i][j] / matrix[j][j];
- for (int m = j; m < lastNum2; m++) {
- matrix[i][m] = matrix[i][m] - k * matrix[j][m];
- }
- }
- }
- return matrix;
- }
- double* findRoots(double** matrix, int tmpCountRoots)
- {
- double* tmpRoots = new double[tmpCountRoots];
- double sum;
- for (int i = tmpCountRoots - 1; i > -1; i--) {
- sum = 0;
- for (int j = i + 1; j < tmpCountRoots; j++) {
- sum = sum + tmpRoots[j] * matrix[i][j];
- }
- if (matrix[i][i] == 0) {
- tmpRoots[i] = 0;
- }
- else {
- tmpRoots[i] = (matrix[i][tmpCountRoots] - sum) /
- matrix[i][i];
- }
- }
- return tmpRoots;
- }
- double* gaussM(double** matrix, int tmpCountRoots)
- {
- writeMatrixInC(matrix, tmpCountRoots);
- newMatrix(matrix, tmpCountRoots);
- return findRoots(matrix, tmpCountRoots);
- }
- void outputsystemRoots(double* roots, int tmpCountRoots)
- {
- for (int i = 0; i < tmpCountRoots; i++)
- cout << "x[" << i + 1 << "] = " << roots[i] << " " << endl;
- }
- string inputFilePath()
- {
- const int EXTENSION_SIZE = 4;
- string path;
- string ext;
- bool isIncorrect;
- do {
- isIncorrect = false;
- cout << "Input path to file: " << endl;
- cin >> path;
- fin.open(path);
- if (path.size() > EXTENSION_SIZE) {
- ext = path.substr(path.size() - EXTENSION_SIZE, EXTENSION_SIZE);
- }
- else {
- cout << "Incorrect file name." << endl;
- isIncorrect = true;
- } if (!isIncorrect && ext != ".txt") {
- cout << "Must have .txt!" << endl;
- isIncorrect = true;
- }
- else if (!isIncorrect && !fin.is_open()) {
- cout << "Wrong way to file." << endl;
- isIncorrect = true;
- }
- } while (isIncorrect);
- fin.close();
- return path;
- }
- int inputSizeOfMatrixFromFile(string path)
- {
- int num;
- bool isIncorrect;
- const int MIN_ORDER = 2;
- do
- {
- isIncorrect = false;
- fin.open(path);
- fin >> num;
- if (num < MIN_ORDER) {
- isIncorrect = true;
- cout << "Incorrect matrix size. Your matrix size must be from 2 " << endl;
- path = inputFilePath();
- }
- } while (isIncorrect);
- return num;
- }
- double** inputTriangularMatrixFile(string path, int num)
- {
- ifstream fin(path);
- bool isIncorrect = false;
- double** arrOfMatrixElements;
- arrOfMatrixElements = new double* [num];
- for (int i = 0; i < num; i++)
- arrOfMatrixElements[i] = new double[num];
- for (int i = 0; (i < num) && (!isIncorrect); i++) {
- for (int j = 0; (j < num) && (!isIncorrect); j++) {
- if (j - i > -1) {
- do
- {
- isIncorrect = false;
- try
- {
- fin >> arrOfMatrixElements[i][j];
- }
- catch (fstream::failure& e)
- {
- cout << "Mistake of reading from file." << endl;
- isIncorrect = true;
- }
- } while (isIncorrect);
- }
- }
- }
- return arrOfMatrixElements;
- }
- double* inputFreeMembersFile(string path, int num)
- {
- bool isIncorrect = false;
- double* arrOfFreeMembers = new double[num];
- for (int i = 0; (i < num) && (!isIncorrect); i++) {
- do {
- isIncorrect = false;
- try {
- fin >> arrOfFreeMembers[i];
- }
- catch (fstream::failure& e) {
- cout << "Mistake of reading from file. Code of mistake " << e.code() << "\n";
- isIncorrect = true;
- }
- } while (isIncorrect);
- }
- return arrOfFreeMembers;
- }
- void fileOutput(double* arrOfSystemRoots, int num)
- {
- string path = inputFilePath();
- fout.open(path);
- for (int i = 0; i < num; i++)
- {
- fout << arrOfSystemRoots[i] << endl;
- }
- cout << "Successful output in file." << endl;
- fout.close();
- }
- int main()
- {
- int choice;
- int num = 0;
- double** arrOfMatrixElements;
- double* arrOfFreeMembers;
- double** arrOfMatrixElementsRandom;
- double** matrix;
- double* roots;
- cout << "Gauss method. Input elements above main diagonal and free members." << endl;
- cout << "Type 0 - console input, type 1 - file input." << endl;
- choice = choose();
- if (!choice)
- {
- cout << "Input matrix size:" << endl;
- num = readCountRoots();
- cout << "Input matrix elements: " << endl;
- arrOfMatrixElements = inputTriangularMatrixConsole(num);
- cout << "Input free members: " << endl;
- arrOfFreeMembers = inputFreeMembersConsole(num);
- }
- else
- {
- string path = inputFilePath();
- num = inputSizeOfMatrixFromFile(path);
- arrOfMatrixElements = inputTriangularMatrixFile(path, num);
- arrOfFreeMembers = inputFreeMembersFile(path, num);
- }
- arrOfMatrixElementsRandom = randomizeOtherElements(num, arrOfMatrixElements);
- matrix = createMatrix(arrOfMatrixElements, num, arrOfMatrixElementsRandom, arrOfFreeMembers);
- roots = gaussM(matrix, num);
- cout << "Type 0 - console output, type 1 - file output." << endl;
- choice = choose();
- if (!choice)
- outputsystemRoots(roots, num);
- else
- fileOutput(roots, num);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement