物联网/嵌入式工程师
重磅升级:新增硬件电路设计与实战,让你软硬通吃,同级PK无敌!行业风口、政策倾斜,新晋热门高薪不内卷!0基础一站式就业完整路径,抢占先发优势!
矩阵是我们常用的数学工具,请你编写一个矩阵的工具类,要求包含矩阵的乘法功能,当然,拷贝也是必要的
class Mat { public: int row = 0; int col = 0; float * * mat = nullptr; private: void init(int row, int col) { if (row && col) { mat = new float*[row]; for (int i = 0; i < row; i++) { mat[i] = new float[col]; for (int j = 0; j < col; j++){ mat[i][j] = 0; if(i == j){ mat[i][j] = 1; } } } } } public: Mat(int row = 0, int col = 0) { this->row = row; this->col = col; init(row, col); } Mat(const Mat &m) { this->row = m.row; this->col = m.col; init(row, col); for (int i = 0; i < row; i++){ for (int j = 0; j < col; j++){ mat[i][j] = m.mat[i][j]; } } } ~Mat() { if (mat != nullptr) { for (int i = 0; i < row; i++){ if (mat[i]) { delete[] mat[i]; mat[i] = nullptr; } } if (mat){ delete[] mat; } mat = nullptr; } } Mat & operator = (const Mat &m) { if (mat != nullptr) { for (int i = 0; i < row; i++){ if (mat[i]) { delete[] mat[i]; mat[i] = nullptr; } } if (mat){ delete[] mat; } mat = nullptr; } row = m.row; col = m.col; init(row, col); for (int i = 0; i < row; i++){ for (int j = 0; j < col; j++){ mat[i][j] = m.mat[i][j]; } } return *this; } Mat operator * (const Mat &m) { EyerMat res(row, m.col); for (int i = 0; i < res.row; i++) { for (int j = 0; j < res.col; j++) { res.mat[i][j] = 0.0f; } } if (m.row != col){ } else { for (int i = 0; i < res.row; i++) { for (int j = 0; j < res.col; j++) { for (int k = 0; k < res.row; k++) { res.mat[i][j] += mat[i][k] * m.mat[k][j]; } } } } return res; } }
请验证,完成请求
由于请求次数过多,请先验证,完成再次请求
打开微信扫码自动绑定
绑定后可得到
使用 Ctrl+D 可将课程添加到书签
举报