物聯網/嵌入式工程師
重磅升級:新增硬件電路設計與實戰,讓你軟硬通吃,同級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 可將課程添加到書簽
舉報