diff options
| author | Syndamia <kamen@syndamia.com> | 2024-04-04 20:11:12 +0300 |
|---|---|---|
| committer | Syndamia <kamen@syndamia.com> | 2024-04-04 20:11:12 +0300 |
| commit | a2e284b0056075e2365deaa2455be567c3b3c945 (patch) | |
| tree | 8a842d1f2ffb9f00989b39d2ab7a7ade1a858d81 /week07/Exercise6.cpp | |
| parent | 44d085f265583f0e3cbef294bbe2c8e300aaa452 (diff) | |
| download | oop-2023-solutions-a2e284b0056075e2365deaa2455be567c3b3c945.tar oop-2023-solutions-a2e284b0056075e2365deaa2455be567c3b3c945.tar.gz oop-2023-solutions-a2e284b0056075e2365deaa2455be567c3b3c945.zip | |
[w7] Added exercise descriptions and solutions
Diffstat (limited to 'week07/Exercise6.cpp')
| -rw-r--r-- | week07/Exercise6.cpp | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/week07/Exercise6.cpp b/week07/Exercise6.cpp new file mode 100644 index 0000000..200f4d8 --- /dev/null +++ b/week07/Exercise6.cpp @@ -0,0 +1,83 @@ +#include "Exercise6.h" +#include <iostream> + +/* Private */ + +void Matrix::addBy(int amount) { + for (int i = 0; i < 4; i++) { + values[i] += amount; + } +} + +/* Public */ + +Matrix::Matrix(int a, int b, int c, int d) { + values[0] = a; + values[1] = b; + values[2] = c; + values[3] = d; +} + +Matrix& Matrix::operator++() { // ++matrix + addBy(+1); + return *this; +} +Matrix Matrix::operator++(int) { // matrix++ + Matrix copy = *this; + addBy(+1); + return copy; +} +Matrix& Matrix::operator--() { // --matrix + addBy(-1); + return *this; +} +Matrix Matrix::operator--(int) { // matrix-- + Matrix copy = *this; + addBy(-1); + return copy; +} + +Matrix& Matrix::operator+=(const Matrix& rhs) { + for (int i = 0; i < 4; i++) { + values[i] += rhs.values[i]; + } + return *this; +} +Matrix& Matrix::operator-=(const Matrix& rhs) { + for (int i = 0; i < 4; i++) { + values[i] -= rhs.values[i]; + } + return *this; +} +Matrix& Matrix::operator*=(const Matrix& rhs) { + values[0] = values[0] * rhs.values[0] + values[1] * rhs.values[2]; + values[1] = values[0] * rhs.values[1] + values[1] * rhs.values[3]; + values[2] = values[2] * rhs.values[0] + values[3] * rhs.values[2]; + values[3] = values[2] * rhs.values[1] + values[3] * rhs.values[3]; + return *this; +} + +/* Friend */ + +Matrix operator+(const Matrix& lhs, const Matrix& rhs) { + Matrix ret = lhs; + ret += rhs; + return ret; +} +Matrix operator-(const Matrix& lhs, const Matrix& rhs) { + Matrix ret = lhs; + ret -= rhs; + return ret; +} +Matrix operator*(const Matrix& lhs, const Matrix& rhs) { + Matrix ret = lhs; + ret *= rhs; + return ret; +} + +std::ostream& operator<<(std::ostream& lhs, const Matrix& rhs) { + return lhs << rhs.values[0] << "," << rhs.values[1] << std::endl << rhs.values[2] << "," << rhs.values[3] << std::endl; +} +std::istream& operator>>(std::istream& lhs, Matrix& rhs) { + return lhs >> rhs.values[0] >> rhs.values[1] >> rhs.values[2] >> rhs.values[3]; +} |
