Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
276 changes: 142 additions & 134 deletions Matrix 0.0.2
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}