-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.cpp
155 lines (139 loc) · 3.32 KB
/
Source.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
#include <locale.h>
using namespace std;
void input(double** matrix, double** copy_matrix, int size1, int size2) {
int i, j;
for (i = 0; i < size1; i++) {
cout << "Введи элементы строки " << i + 1 << "\n";
for (j = 0; j <= size2; j++) {
cin >> matrix[i][j];
copy_matrix[i][j] = matrix[i][j];
}
}
}
void output(double** matrix, int row, int col) {
int i, j, flag;
for (i = row - 1; i >= 0; i--) {
flag = 0;
for (int j = i; j < col; j++) {
if (matrix[i][j] != 0) {
flag = 1;
break;
}
}
if (flag == 0) row--;
}
for (i = 0; i < row; i++) {
for (j = 0; j <= col; j++) {
if (j == col) {
cout << "| ";
}
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
void swap(double* first, double* second, int col) {
double temp;
for (int i = 0; i <= col; i++) {
temp = first[i];
first[i] = second[i];
second[i] = temp;
}
}
void forward(double** matrix, int row, int col) {
int i, j, k;
for (i = 0; i < row; i++) {
if (matrix[i][i] != 0) {
double coeff = matrix[i][i];
for (j = i; j <= col; j++) {
matrix[i][j] /= coeff;
}
for (k = i + 1; k < row; k++) {
double nextcoeff = matrix[k][i];
for (j = 0; j <= col; j++) {
matrix[k][j] -= (matrix[i][j] * nextcoeff);
}
}
}
else {
for (int j = i + 1; j < row; j++) {
if (matrix[j][i] != 0) {
swap(matrix[i], matrix[j], col);
}
}
}
output(matrix, row, col);
cout << endl;
}
}
void backward(double** matrix, double* x, int row, int col) {
for (int i = row - 1; i >= 0; i--) {
double sum = 0.0;
for (int j = i + 1; j < col; j++) {
sum += matrix[i][j] * x[j];
}
x[i] = (matrix[i][col] - sum) / matrix[i][i];
}
}
void solution(double* x, int row) {
for (int i = 0; i < row; i++) {
if (abs(x[i]) < 0.00000001) {
cout << "X[" << i + 1 << "] = " << 0 << endl;
}
else {
cout << "X[" << i + 1 << "] = " << x[i] << endl;
}
}
}
void test(double** copy_matrix, double* x, int col) {
double sum = 0.0;
for (int i = 0; i < col; i++) {
sum += copy_matrix[0][i] * x[i];
}
if ((sum == copy_matrix[0][col]) || (abs(copy_matrix[0][col] - sum < 0.001))) {
cout << "Проверка пройдена успешно!" << endl;
}
}
int main() {
setlocale(LC_ALL, "Rus");
double** matrix;
double** copy_matrix;
double* x;
int row, col;
cout << "Введи количество строк ";
cin >> row;
cout << "Введи количество столбцов ";
cin >> col;
//выделение памяти
matrix = new double* [row];
for (int i = 0; i < row; i++) {
matrix[i] = new double[col + 1];
}
x = new double[row];
copy_matrix = new double* [row];
for (int i = 0; i < row; i++) {
copy_matrix[i] = new double[col + 1];
}
input(matrix, copy_matrix, row, col);
cout << endl;
output(matrix, row, col);
cout << endl;
forward(matrix, row, col);
cout << endl;
backward(matrix, x, row, col);
solution(x, row);
cout << endl;
test(copy_matrix, x, col);
//освобождение памяти
for (int i = 0; i < row; i++) {
delete[] matrix[i];
}
delete[] matrix;
for (int i = 0; i < row; i++) {
delete[] copy_matrix[i];
}
delete[] copy_matrix;
delete[] x;
return 0;
}