From da0876a9f3293025a28826251dc3c415b27e0478 Mon Sep 17 00:00:00 2001 From: justcppdev Date: Thu, 26 Oct 2017 20:49:24 +0300 Subject: [PATCH] Update Matrix 0.0.2 --- Matrix 0.0.2 | 276 ++++++++++++++++++++++++++------------------------- 1 file changed, 142 insertions(+), 134 deletions(-) diff --git a/Matrix 0.0.2 b/Matrix 0.0.2 index 1ce9e0a..265aadf 100644 --- a/Matrix 0.0.2 +++ b/Matrix 0.0.2 @@ -5,159 +5,167 @@ using namespace std; bool read(float **matrix, unsigned int rows, unsigned int columns) { - bool result = true; - string string; - for (unsigned int i = 0; i < rows; ++i) { - matrix[i] = new float[columns]; - getline(cin, string); - istringstream stream(string); - for (unsigned int j = 0; j < columns; j++) { - if (!(stream >> matrix[i][j])) { - result = false; - break; - } - } - } - return result; + bool result = true; + string string; + for (unsigned int i = 0; i < rows; ++i) { + matrix[i] = new float[columns]; + getline(cin, string); + istringstream stream(string); + for (unsigned int j = 0; j < columns; j++) { + if (!(stream >> matrix[i][j])) { + result = false; + break; + } + } + } + return result; } +// объедините чтение элементов матрицы и строк со столбцами в один метод bool rowcol(unsigned int &rows, unsigned int &columns) { - char zap; - string string; - getline(cin, string); - istringstream stream(string); - if (stream >> rows && stream >> zap && zap == ',' && stream >> columns) { - return true; - } - else return false; - + char zap; + string string; + getline(cin, string); + istringstream stream(string); + if (stream >> rows && stream >> zap && zap == ',' && stream >> columns) { + return true; + } + else return false; + } +// метод должен вернуть матрицу а не просто вывести ее в стандартный поток вывода void add(float ** matrix1, float ** matrix2, unsigned int rows, unsigned int columns) { - cout << endl; - for (unsigned int i = 0; i < rows; i++) { - for (unsigned int j = 0; j < columns; j++) { - cout << matrix1[i][j] + matrix2[i][j] << " "; - } - cout << endl; - } + cout << endl; + for (unsigned int i = 0; i < rows; i++) { + for (unsigned int j = 0; j < columns; j++) { + cout << matrix1[i][j] + matrix2[i][j] << " "; + } + cout << endl; + } } +// метод должен вернуть матрицу а не просто вывести ее в стандартный поток вывода void sub (float ** matrix1, float ** matrix2, unsigned int rows, unsigned int columns) { - cout << endl; - for (unsigned int i = 0; i < rows; i++) { - for (unsigned int j = 0; j < columns; j++) { - cout << matrix1[i][j] - matrix2[i][j] << " "; - } - cout << endl; - } + cout << endl; + for (unsigned int i = 0; i < rows; i++) { + for (unsigned int j = 0; j < columns; j++) { + cout << matrix1[i][j] - matrix2[i][j] << " "; + } + cout << endl; + } } +// метод должен вернуть матрицу а не просто вывести ее в стандартный поток вывода void mult(float ** matrix1, float ** matrix2, unsigned int rows, unsigned int columns) { - cout << endl; - for (unsigned int i = 0; i < rows; i++) { - for (unsigned int j = 0; j < columns; j++) { - float res = 0; - for (unsigned int k = 0; k< rows; k++) - res += matrix1[i][k] * matrix2[k][j]; - cout << res << " "; - } - cout << endl; - } + cout << endl; + for (unsigned int i = 0; i < rows; i++) { + for (unsigned int j = 0; j < columns; j++) { + float res = 0; + for (unsigned int k = 0; k< rows; k++) + res += matrix1[i][k] * matrix2[k][j]; + cout << res << " "; + } + cout << endl; + } } +// метод должен вернуть матрицу а не просто вывести ее в стандартный поток вывода void Trans(float ** matrix, unsigned int rows, unsigned int columns) { - float ** nmatrix = new float *[rows]; - for (unsigned int i = 0; i < columns; i++) { - nmatrix[i] = new float[rows]; - for (unsigned int j = 0; j < rows; j++) { - nmatrix[i][j] = matrix[j][i]; - } - } - for (unsigned int i = 0; i < columns; i++) { - for (unsigned int j = 0; j < rows; j++) { - cout << nmatrix[i][j] << " "; - } - cout << endl; - } + float ** nmatrix = new float *[rows]; + for (unsigned int i = 0; i < columns; i++) { + nmatrix[i] = new float[rows]; + for (unsigned int j = 0; j < rows; j++) { + nmatrix[i][j] = matrix[j][i]; + } + } + for (unsigned int i = 0; i < columns; i++) { + for (unsigned int j = 0; j < rows; j++) { + cout << nmatrix[i][j] << " "; + } + cout << endl; + } } +// метод должен вернуть матрицу а не просто вывести ее в стандартный поток вывода void Gauss(float ** matrix, float ** matrix1,int rows) { - float a, b, result; - for (int i = 0; i < rows; i++) { - matrix[i] = new float[rows]; - - for (int j = 0; j < rows; j++) { - matrix[i][j] = 0; - matrix[i][i] = 1; - } - } - for (int i = 0; i < rows; i++) { - a = matrix1[i][i]; - for (int j = i + 1; j < rows; j++) { - b = matrix1[j][i]; - for (int k = 0; k < rows; k++) { - matrix1[j][k] = matrix1[i][k] * b - matrix1[j][k] * a; - matrix[j][k] = matrix[i][k] * b - matrix[j][k] * a; - } - } - } - for (int i = 0; i < rows; i++) { - for (int j = rows - 1; j > -1; j--) { - result = 0; - for (int k = rows - 1; k > j; k--) { - result += matrix1[j][k] * matrix[k][i]; - if (matrix1[j][j] == 0) { - for (i = 0; i < rows; i++) { - delete[] matrix[i]; - delete[]matrix; - } - } - } - matrix[j][i] = (matrix[j][i] - result) / matrix1[j][j]; - } - } - cout << endl; - for (int i = 0; i < rows; i++) { - for (int j = 0; j < rows; j++) { - cout << matrix[i][j] << " "; - } - cout << endl; - } + float a, b, result; + for (int i = 0; i < rows; i++) { + matrix[i] = new float[rows]; + + for (int j = 0; j < rows; j++) { + matrix[i][j] = 0; + matrix[i][i] = 1; + } + } + for (int i = 0; i < rows; i++) { + a = matrix1[i][i]; + for (int j = i + 1; j < rows; j++) { + b = matrix1[j][i]; + for (int k = 0; k < rows; k++) { + matrix1[j][k] = matrix1[i][k] * b - matrix1[j][k] * a; + matrix[j][k] = matrix[i][k] * b - matrix[j][k] * a; + } + } + } + for (int i = 0; i < rows; i++) { + for (int j = rows - 1; j > -1; j--) { + result = 0; + for (int k = rows - 1; k > j; k--) { + result += matrix1[j][k] * matrix[k][i]; + if (matrix1[j][j] == 0) { + for (i = 0; i < rows; i++) { + delete[] matrix[i]; + delete[]matrix; + } + } + } + matrix[j][i] = (matrix[j][i] - result) / matrix1[j][j]; + } + } + cout << endl; + for (int i = 0; i < rows; i++) { + for (int j = 0; j < rows; j++) { + cout << matrix[i][j] << " "; + } + cout << endl; + } } +// очистка памяти int main() { - unsigned int rows1 = 0, rows2 = 0, columns1 = 0, columns2 = 0; - char op; - rowcol(rows1, columns1); - float ** matrix1 = new float *[rows1]; - if (read(matrix1, rows1, columns1)) { - cin >> op; - if (op == 'T') { - Trans(matrix1, rows1, columns1); - } - else if (rows1 == columns1 && (op == '-' || op == '+' || op == '*')) { - cin.get(); - rowcol(rows2, columns2); - float ** matrix2 = new float *[rows2]; - if( read(matrix2, rows2, columns2) && rows1 == rows2 && columns1 == columns2) { - switch (op) { - case '+': { add(matrix1, matrix2, rows1, columns1); - break; } - case '-': { sub(matrix1, matrix2, rows1, columns1); - break; } - case '*': { mult(matrix1, matrix2, rows1, columns1); - break; } - default: cout << "An error has occured while reading input data"; - } - } - else cout << "An error has occured while reading input data"; - } - else if (rows1 == columns1 && op == 'R') { - float **matrix = new float *[columns1]; - Gauss(matrix, matrix1, rows1); - } - else cout << "An error has occured while reading input data"; - } - else cout << "An error has occured while reading input data"; + unsigned int rows1 = 0, rows2 = 0, columns1 = 0, columns2 = 0; + char op; + rowcol(rows1, columns1); + float ** matrix1 = new float *[rows1]; + if (read(matrix1, rows1, columns1)) { + cin >> op; + if (op == 'T') { + Trans(matrix1, rows1, columns1); + } + else if (rows1 == columns1 && (op == '-' || op == '+' || op == '*')) { + cin.get(); + rowcol(rows2, columns2); + float ** matrix2 = new float *[rows2]; + if( read(matrix2, rows2, columns2) && rows1 == rows2 && columns1 == columns2) { + switch (op) { + case '+': { add(matrix1, matrix2, rows1, columns1); + break; } + case '-': { sub(matrix1, matrix2, rows1, columns1); + break; } + case '*': { mult(matrix1, matrix2, rows1, columns1); + break; } + default: cout << "An error has occured while reading input data"; + } + } + else cout << "An error has occured while reading input data"; + } + else if (rows1 == columns1 && op == 'R') { + float **matrix = new float *[columns1]; + Gauss(matrix, matrix1, rows1); + } + else cout << "An error has occured while reading input data"; + } + else cout << "An error has occured while reading input data"; } +