diff --git a/SoftwareDesign/Code/inc/matrix.hpp b/SoftwareDesign/Code/inc/matrix.hpp index fe3a9c4..968f51f 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 14:11:55 + * @LastEditTime: 2022-03-12 14:45:22 * @FilePath: \Code\inc\matrix.hpp * @Description: * @@ -26,6 +26,10 @@ private: protected: public: + ~Matrix() + { + delete[] _mat; + } /** * @description: 极度缺省初始化, 大小为2*2, 初值为0 * @param {int} w @@ -49,13 +53,22 @@ public: /** * @description: 以顺序访问数组的值初始化矩阵元素 - * @note: 必须给出正确的数组,因为在函数内无法判定数组是否正确 + * 必须给出正确的数组,因为在函数内无法判定数组是否正确 * @param {int} w * @param {int} h * @param {const T} elements[] */ Matrix(int w, int h, const T elements[]); + /** + * @description: 以顺序访问指针的值初始化矩阵元素 + * @note: 必须给出正确的数组,因为在函数内无法判定数组是否正确 + * @param {int} w + * @param {int} h + * @param {T*} data + */ + Matrix(int w, int h, T *data); + /** * @description: 以另一个矩阵初始化当前矩阵的元素,实际是元素拷贝 * @param {Matrix} &another @@ -172,6 +185,38 @@ Matrix::Matrix(int w, int h, const T elements[]) ; } } + +template +Matrix::Matrix(int w, int h, T *data) +{ + 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"; + if (data == NULL) + throw 1; + _width = w; + _height = h; + T temp; + _mat = new Vector; + for (int i = 0; i < size; i++) + _mat->put(i, *(data+sizeof(temp)*i)); + } + catch (const int a) + { + if (a == 1) + { + std::cout << "Error: Empty array. Set elements to 0." << std::endl; + for (int i = 0; i < size; i++) + _mat->put(i, 0); + } + } +} + template Matrix::Matrix(const Matrix &another) {