From 1722bc6cfe998a7105235af909d0b3127ce29127 Mon Sep 17 00:00:00 2001 From: iridiumR Date: Sat, 12 Mar 2022 14:14:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=95=E7=94=A8=E5=BA=93=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SoftwareDesign/Code/inc/matrix.hpp | 77 ++++++++++++++++++++++++++++-- SoftwareDesign/Code/inc/vector.hpp | 3 +- 2 files changed, 74 insertions(+), 6 deletions(-) diff --git a/SoftwareDesign/Code/inc/matrix.hpp b/SoftwareDesign/Code/inc/matrix.hpp index 93cfd02..fe3a9c4 100644 --- a/SoftwareDesign/Code/inc/matrix.hpp +++ b/SoftwareDesign/Code/inc/matrix.hpp @@ -2,7 +2,7 @@ * @Author: iR * @Date: 2022-03-11 16:44:46 * @LastEditors: iR - * @LastEditTime: 2022-03-12 12:49:04 + * @LastEditTime: 2022-03-12 14:11:55 * @FilePath: \Code\inc\matrix.hpp * @Description: * @@ -71,15 +71,31 @@ public: const T get(int w, int h) const; /** - * @description:运算符重载 + * @description: []运算符重载 */ const T operator[](int i) const { return _mat->get(i); } /** - * @description:运算符重载 + * @description: =运算符重载 */ T operator=(const Matrix &b); + /** + * @description: +=运算符重载 + * @note: 即 a += b,其中*this即为a + * @param {*} + * @return None + */ + void operator+=(const Matrix &b); + + /** + * @description: +=运算符重载 + * @note:即 c = a + b,其中*this即为a,返回值即为c + * @param {*} + * @return None + */ + Matrix operator+(const Matrix &b) const; + /** * @description: 可读可写访问 * @param {int} w @@ -138,7 +154,7 @@ Matrix::Matrix(int w, int h, const T elements[]) int size = w * h; try { - + //实际上似乎并不能在这里判断数组是否合法 // if (w * h > size) // throw "Error: The number of array elements is less than the matrix elements.\n"; // if (w * h < size) @@ -193,7 +209,58 @@ T Matrix::operator=(const Matrix &another) template const T Matrix::get(int w, int h) const { - return _mat[h * _width + w]; + return _mat->get(h * _width + w); +} + +template +void Matrix::operator+=(const Matrix &b) +{ + try + { + if (this->_height == b.getHeight() && this->_width == b.getWidth()) + { + for (int i = 0; i < _width * _height; i++) + this->_mat->put(i, (*this)[i] + b[i]); + + return; + } + throw "Error: Different size."; + } + catch (const char *msg) + { + std::cout << msg; + } +} + +/** + * @description: +=运算符重载 + * @note:即 c = a + b,其中*this即为a,返回值即为c + * @param {*} + * @return None + */ +template +Matrix Matrix::operator+(const Matrix &b) const +{ + int h = this->_height; + int w = this->_width; + try + { + if (h == b.getHeight() && w == b.getWidth()) + { + Matrix temp(w, h, T(0)); + temp += *this; + temp += b; + return temp; + } + + throw "Error: Different size."; + } + catch (const char *msg) + { + std::cout << msg; + Matrix temp(w, h, T(0)); + return temp; + } } #endif \ No newline at end of file diff --git a/SoftwareDesign/Code/inc/vector.hpp b/SoftwareDesign/Code/inc/vector.hpp index 5ec46da..bd7e709 100644 --- a/SoftwareDesign/Code/inc/vector.hpp +++ b/SoftwareDesign/Code/inc/vector.hpp @@ -2,7 +2,7 @@ * @Author: iR * @Date: 2022-03-11 16:47:31 * @LastEditors: iR - * @LastEditTime: 2022-03-12 12:49:25 + * @LastEditTime: 2022-03-12 14:08:48 * @FilePath: \Code\inc\vector.hpp * @Description: * @@ -124,6 +124,7 @@ T &Vector::get(int a) catch (const char *msg) { std::cout << msg; + return _v[0]; } }