Дружественные функции для класса Матрица
08.12.2021, 01:22. Показов 597. Ответов 1
Реализовал класс матрица и перегрузил некоторые операторы, а теперь хочу еще дружественные функции написать. Для сложения,вычитания и умножения матриц написал, но не получается написать для умножения матрицы на число и транспонирования, помогите если несложно .
| C++ | 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
| #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "Header.h"
using namespace std;
int main()
{
setlocale(LC_ALL, "Russian");
FILE* fin = fopen("in.txt", "r");
printf("Матрица X:\n");
int nX, mX;
fscanf_s(fin, "%d %d", &nX, &mX);
Matrix X;
X.SetSize(nX, mX);
X.matrin(fin);
X.matrout();
printf("\n");
printf("Матрица Y:\n");
int nY, mY;
fscanf_s(fin, "%d %d", &nY, &mY);
Matrix Y(nY, mY);
Y.matrin(fin);
Y.matrout();
printf("\n");
printf("Матрица Z:\n");
int nZ, mZ;
fscanf_s(fin, "%d %d", &nZ, &mZ);
Matrix Z(nZ, mZ);
Z.matrin(fin);
Z.matrout();
printf("\n");
/*printf("равны ли матрицы X и Y?\n");
if (X == Y)
printf("- Да\n");
else printf("- Нет\n");
printf("\n");
printf("не равны ли матрицы X и Z?\n");
if (X != Z)
printf("- Да\n");
else printf("- Нет\n");
printf("\n");
int index1, index2;
printf("Введите индексы для возврата элемента:\n");
scanf_s("%d%d", &index1, &index2);
printf("Y[%d][%d] = ",index1, index2 );
printf("%lf\n",Y[index1][index2]);
printf("\n");
printf("R1 = X + Y:\n");
Matrix R1;
if ((nX != nY) || (mX != mY))
printf("размеры матриц не равны\n");
else {
R1 = (X + Y);
R1.matrout();
}
printf("\n");
int number;
printf("Введите число: ");
scanf_s("%d",&number);
printf("R2 += %d\n", number);
Matrix R2;
R2 = X;
R2 += number;
R2.matrout();
printf("\n");
printf("R3 = X:\n");
Matrix R3;
R3 = X;
R3.matrout();
printf("\n");
printf("R3 += Y:\n");
if ((nX != nY) || (mX != mY))
printf("размеры матриц не равны\n");
else {
R3 += Y;
R3.matrout();
}
printf("\n");*/
Matrix R1;
printf("R1 = X + Y:\n");
R1 = X + Y;
R1.matrout();
printf("\n");
Matrix R2;
printf("R2 = Z - X:\n");
R2 = Z - X;
R2.matrout();
printf("\n");
Matrix R3;
printf("R3 = Z * Y:\n");
R3 = Z * Y;
R3.matrout();
printf("\n");
fclose(fin);
} |
|
| C++ | 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
| #pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Matrix
{
private:
int rows, cols;
double** matrix;
public:
class Array
{
private:
double* array;
public:
Array(double* array) : array(array) {}
double operator[](int index)
{
return array[index];
}
};
Matrix(int r = 0, int c = 0);
Matrix(const Matrix& mtrx, double number);
void CreateMatrix(double elem);
operator double();
Matrix& operator = (const Matrix& mtrx);
bool operator == (const Matrix& mtrx) const;
bool operator != (const Matrix& mtrx) const;
Array operator [] (int n);
Matrix operator + (const Matrix& mtrx);
Matrix operator - (const Matrix& mtrx);
Matrix& operator += (const double number);
friend Matrix operator +(const Matrix& mtrx, const Matrix& mat);
friend Matrix operator -(const Matrix& mtrx, const Matrix& mat);
friend Matrix operator *(const Matrix& mtrx, const Matrix& mat);
void matrin(FILE* fin);
void SetSize(int n, int m);
int GetRows();
int GetCols();
void matrout();
~Matrix();
}; |
|
| C++ | 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
| #include "Header.h"
Matrix::Matrix(int r, int c) : rows(r), cols(c), matrix(nullptr)
{
if ((rows > 0) && (cols > 0)) {
matrix = new double* [rows];
for (int i = 0; i < rows; i++)
matrix[i] = nullptr;
for (int i = 0; i < rows; i++)
matrix[i] = new double[cols];
}
}
//конструктор, который создаёт новую матрицу таким образом, что все её элементы больше элементов другой матрицы на заданное число
Matrix::Matrix(const Matrix& mtrx, double number)
{
rows = mtrx.rows;
cols = mtrx.cols;
if ((rows > 0) && (cols > 0)) {
matrix = new double* [rows];
for (int i = 0; i < rows; i++)
matrix[i] = nullptr;
for (int i = 0; i < rows; i++)
matrix[i] = new double[cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
matrix[i][j] = mtrx.matrix[i][j] + number;
}
}
void Matrix::CreateMatrix(double elem)
{
if (matrix) {
for (int i = 0; i < rows; i++)
delete[] matrix[i];
delete[] matrix;
}
matrix = new double* [1];
matrix[0] = new double[1];
rows = 1;
cols = 1;
matrix[0][0] = elem;
}
Matrix::operator double() {
double s = 0;
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
s += matrix[i][j];
return s / (rows * cols);
}
Matrix& Matrix::operator=(const Matrix& mtrx)
{
if (this != &mtrx) {
if (this->matrix == nullptr) {
for (int i = 0; i < this->rows; i++)
delete[] this->matrix[i];
delete[] this->matrix;
}
this->rows = mtrx.rows;
this->cols = mtrx.cols;
this->matrix = nullptr;
if ((this->rows > 0) && (this->cols > 0)) {
this->matrix = new double* [this->rows];
for (int i = 0; i < this->rows; i++)
this->matrix[i] = nullptr;
for (int i = 0; i < this->rows; i++)
this->matrix[i] = new double[this->cols];
for (int i = 0; i < this->rows; i++)
for (int j = 0; j < this->cols; j++)
this->matrix[i][j] = mtrx.matrix[i][j];
}
}
return *this;
}
bool Matrix::operator==(const Matrix& mtrx) const
{
bool flag = false;
if ((rows == mtrx.rows) && (cols == mtrx.cols)) {
flag = true;
for (int i = 0; (i < rows) && flag; i++)
for (int j = 0; (j < cols) && flag; j++)
if (matrix[i][j] != mtrx.matrix[i][j])
flag = false;
}
return flag;
}
bool Matrix::operator!=(const Matrix& mtrx) const
{
bool flag = true;
if ((rows != mtrx.rows) || (cols != mtrx.cols)) {
cout << "размеры матриц не равны\n";
}
else {
flag = false;
for (int i = 0; (i < rows) && !flag; i++)
for (int j = 0; (j < cols) && !flag; j++)
if (matrix[i][j] != mtrx.matrix[i][j])
flag = true;
}
return flag;
}
Matrix::Array Matrix::operator[](int n)
{
return matrix[n];
}
Matrix Matrix::operator+(const Matrix& mtrx)
{
Matrix res(mtrx.rows, mtrx.cols);
for (int i = 0; i < this->rows; i++)
for (int j = 0; j < this->cols; j++)
res.matrix[i][j] = matrix[i][j] + mtrx.matrix[i][j];
;
return res;
}
Matrix Matrix::operator-(const Matrix& mtrx)
{
Matrix res(mtrx.rows, mtrx.cols);
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
res.matrix[i][j] = matrix[i][j] - mtrx.matrix[i][j];
return res;
}
Matrix& Matrix::operator+=(const double number)
{
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
matrix[i][j] += number;
return *this;
}
Matrix operator+(const Matrix& mtrx, const Matrix& mat)
{
Matrix temp(mtrx.rows, mtrx.cols);
if (mtrx.rows == mat.rows && mtrx.cols == mat.cols)
for (int i = 0; i < mtrx.rows; i++)
for (int j = 0; j < mtrx.cols; j++)
temp.matrix[i][j] = mtrx.matrix[i][j] + mat.matrix[i][j];
else
cout << "Несоответствие размеров!\n";
return temp;
}
Matrix operator-(const Matrix& mtrx, const Matrix& mat)
{
Matrix temp(mtrx.rows, mtrx.cols);
if (mtrx.rows == mat.rows && mtrx.cols == mat.cols)
for (int i = 0; i < mtrx.rows; i++)
for (int j = 0; j < mtrx.cols; j++)
temp.matrix[i][j] = mtrx.matrix[i][j] - mat.matrix[i][j];
else
cout << "Несоответствие размеров!\n";
return temp;
}
Matrix operator*(const Matrix& mtrx, const Matrix& mat)
{
Matrix temp(mtrx.rows, mat.cols);
if (mtrx.cols == mat.rows)
for (int i = 0; i < mtrx.rows; i++)
for (int j = 0; j < mat.cols; j++) {
temp.matrix[i][j] = 0;
for (int k = 0; k < mtrx.cols; k++)
temp.matrix[i][j] += mtrx.matrix[i][k] * mat.matrix[k][j];
}
else
cout << "Несоответствие размеров!\n";
return temp;
}
void Matrix::matrin(FILE* fin)
{
double elem;
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++) {
fscanf_s(fin, "%lf", &elem);
matrix[i][j] = elem;
}
}
void Matrix::SetSize(int n, int m)
{
rows = n;
cols = m;
if ((rows > 0) && (cols > 0)) {
matrix = new double* [rows];
for (int i = 0; i < rows; i++)
matrix[i] = new double[cols];
}
}
int Matrix::GetRows() {
return rows;
}
int Matrix::GetCols() {
return cols;
}
void Matrix::matrout()
{
for (int i = 0; i < GetRows(); i++) {
for (int j = 0; j < GetCols(); j++)
printf("%f ", this->matrix[i][j]);
printf("\n");
}
}
Matrix::~Matrix() {
if (this->matrix == nullptr) {
for (int i = 0; i < this->rows; i++)
delete[] this->matrix[i];
delete[] this->matrix;
}
} |
|
Добавлено через 4 часа 14 минут
Матрицы на число написал,осталось только транспонирования
Добавлено через 1 час 10 минут
и транспонирование тоже написал,всем огромное спасибо за помощь(нет)
0
|