diff --git a/SoftwareDesign/Code/ex01/main.cpp b/SoftwareDesign/Code/ex01/main.cpp index caee0c6..ca707ad 100644 --- a/SoftwareDesign/Code/ex01/main.cpp +++ b/SoftwareDesign/Code/ex01/main.cpp @@ -1,7 +1,23 @@ +/* + * @Author: iR + * @Date: 2022-03-11 20:22:54 + * @LastEditors: iR + * @LastEditTime: 2022-03-12 12:30:06 + * @FilePath: \Code\ex01\main.cpp + * @Description: + * + * Copyright (c) 2022 by iR, All Rights Reserved. + */ + #include "../inc/matrix.hpp" #include int main() { - Matrix a(2,2); - printf("哈哈了"); + double data[] = {1, 2, 3, 4}; + Matrix a(2, 2, data); + Matrix b(2, 2, 5); + Matrix c = a; + Matrix d; + + return 0; } diff --git a/SoftwareDesign/Code/inc/matrix.hpp b/SoftwareDesign/Code/inc/matrix.hpp index f34b1fa..9f9d727 100644 --- a/SoftwareDesign/Code/inc/matrix.hpp +++ b/SoftwareDesign/Code/inc/matrix.hpp @@ -1,6 +1,19 @@ +/* + * @Author: iR + * @Date: 2022-03-11 16:44:46 + * @LastEditors: iR + * @LastEditTime: 2022-03-12 12:30:44 + * @FilePath: \Code\inc\matrix.hpp + * @Description: + * + * Copyright (c) 2022 by iR, All Rights Reserved. + */ #ifndef _MATRIX_HPP_ #define _MATRIX_HPP_ +#include +#include + #include "../inc/vector.hpp" template @@ -12,29 +25,87 @@ private: Vector *_mat; protected: - public: - - // 确定矩阵元素大小,不初始化 + /** + * @description: 极度缺省初始化, 大小为2*2, 初值为0 + * @param {int} w + * @param {int} h + */ + Matrix(); + /** + * @description: 缺省初始化,初值为0 + * @param {int} w + * @param {int} h + */ Matrix(int w, int h); - // 以相同值初始化矩阵元素 + + /** + * @description: 以相同值初始化矩阵元素 + * @param {int} w + * @param {int} h + * @param {T} init_value + */ Matrix(int w, int h, T init_value); - // 以顺序访问数组的值初始化矩阵元素 - Matrix(int w, int h, const double *elements); - // 以另一个矩阵初始化当前矩阵的元素,实际是元素拷贝 + + /** + * @description: 以顺序访问数组的值初始化矩阵元素 + * @note: 必须给出正确的数组,因为在函数内无法判定数组是否正确 + * @param {int} w + * @param {int} h + * @param {const T} elements[] + */ + Matrix(int w, int h, const T elements[]); + + /** + * @description: 以另一个矩阵初始化当前矩阵的元素,实际是元素拷贝 + * @param {Matrix} &another + */ Matrix(const Matrix &another); - //只读访问内容 + + /** + * @description: 只读访问内容 + * @param {int} w + * @param {int} h + * @return {*} + */ const T get(int w, int h) const; - //运算符重载 - T &operator[](int i) { return _mat[i]; } + + /** + * @description:运算符重载 + */ + const T operator[](int i) const { return _mat->get(i); } + + /** + * @description:运算符重载 + */ + T operator=(const Matrix &b); + + /** + * @description: 可读可写访问 + * @param {int} w + * @param {int} h + * @return {*} + */ + T &at(int w, int h) { return _mat[h * _width + w]; } + + const int getWidth() const { return _width; } + const int getHeight() const { return _height; } }; +template +Matrix::Matrix() +{ + _width = 2; + _height = 2; + _mat = new Vector(2 * 2, 0); +} + template Matrix::Matrix(int w, int h) { _width = w; _height = h; - _mat = new Vector(w * h); + _mat = new Vector(w * h, 0); } template @@ -45,10 +116,68 @@ Matrix::Matrix(int w, int h, T init_value) _mat = new Vector(w * h, init_value); } +template +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) + // throw "Error: The number of array elements is greater than the matrix elements.\n"; + + _width = w; + _height = h; + _mat = new Vector; + for (int i = 0; i < size; i++) + _mat->put(i, elements[i]); + } + catch (const char *msg) + { + std::cout << msg << "SIZE:" << size << std::endl; + ; + } +} +template +Matrix::Matrix(const Matrix &another) +{ + try + { + _width = another.getWidth(); + _height = another.getHeight(); + _mat = new Vector; + for (int i = 0; i < _width * _height; i++) + _mat->put(i, another[i]); + } + catch (const char *msg) + { + std::cout << msg; + } +} + +template +T Matrix::operator=(const Matrix &another) +{ + try + { + _width = another.getWidth(); + _height = another.getHeight(); + _mat = new Vector; + for (int i = 0; i < _width * _height; i++) + _mat->put(i, another[i]); + } + catch (const char *msg) + { + std::cout << msg; + } +} + template const T Matrix::get(int w, int h) const { - return _mat[w * _width + h]; + return _mat[h * _width + w]; } #endif \ No newline at end of file diff --git a/SoftwareDesign/Code/inc/vector.hpp b/SoftwareDesign/Code/inc/vector.hpp index 4df2b5e..dd7bb47 100644 --- a/SoftwareDesign/Code/inc/vector.hpp +++ b/SoftwareDesign/Code/inc/vector.hpp @@ -1,3 +1,13 @@ +/* + * @Author: iR + * @Date: 2022-03-11 16:47:31 + * @LastEditors: iR + * @LastEditTime: 2022-03-12 12:20:35 + * @FilePath: \Code\inc\vector.hpp + * @Description: + * + * Copyright (c) 2022 by iR, All Rights Reserved. + */ #ifndef _VECTOR_HPP_ #define _VECTOR_HPP_ @@ -149,6 +159,7 @@ T Vector::put(int a, T value) } _v[a] = value; + return _v[a]; } template