Skip to content

Commit 71e16db

Browse files
committed
added multiplication and division by scalar
1 parent 7829739 commit 71e16db

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

Variants/Lab_12/var_12/main.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,11 @@ int main() {
168168
cout << " =\n" << endl;
169169
mult1 *= mult2;
170170
mult1.print();
171+
172+
Matrix mult3(6, 6);
173+
mult3.print();
174+
cout << "* 6 =\n" << endl;
175+
176+
mult3 *= 6;
177+
mult3.print();
171178
}

Variants/Lab_12/var_12/matrix.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,32 @@ Matrix& Matrix::operator*=(const Matrix& right) {
186186
Matrix operator*(Matrix a, const Matrix& b) {
187187
a *= b;
188188
return a;
189+
}
190+
191+
Matrix& Matrix::operator*=(const double& n) {
192+
for (int x = 0; x < arr.size(); x++) {
193+
for (int y = 0; y < arr.size(); y++) {
194+
arr[y][x] *= n;
195+
}
196+
}
197+
return *this;
198+
}
199+
200+
Matrix operator*(Matrix a, const double &b) {
201+
a *= b;
202+
return a;
203+
}
204+
205+
Matrix& Matrix::operator/=(const double& n) {
206+
for (int x = 0; x < arr.size(); x++) {
207+
for (int y = 0; y < arr.size(); y++) {
208+
arr[y][x] /= n;
209+
}
210+
}
211+
return *this;
212+
}
213+
214+
Matrix operator/(Matrix a, const double &b) {
215+
a /= b;
216+
return a;
189217
}

Variants/Lab_12/var_12/matrix.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,17 @@ class Matrix {
3232

3333
// операторы
3434

35-
Matrix& operator+=(const Matrix& x);
36-
Matrix& operator-=(const Matrix& x);
37-
Matrix& operator*=(const Matrix& x);
35+
Matrix& operator+=(const Matrix& x); // сложение с матрицей
36+
Matrix& operator-=(const Matrix& x); // вычитание матрицы
37+
Matrix& operator*=(const Matrix& x); // умножение на матрицу
38+
Matrix& operator*=(const double& x); // умножение на скаляр
39+
Matrix& operator/=(const Matrix& x); // деление на матрицу
40+
Matrix& operator/=(const double& x); // деление на скаляр
3841

3942
friend Matrix operator+(Matrix a, const Matrix &b);
4043
friend Matrix operator-(Matrix a, const Matrix &b);
4144
friend Matrix operator*(Matrix a, const Matrix &b);
45+
friend Matrix operator*(Matrix a, const double &b);
46+
friend Matrix operator/(Matrix a, const Matrix &b);
47+
friend Matrix operator/(Matrix a, const double &b);
4248
};

0 commit comments

Comments
 (0)