C++实现矩阵类型的基本操作:创建矩阵(M x N)、释放内存、获取某一元素的值、修改某一元素的值、乘法操作

来源:互联网 发布:青少年编程网 编辑:程序博客网 时间:2024/06/07 00:06
矩阵类型的基本操作。
   2.1. 声明一个矩阵类,支持多种数据类型(例如int、float、double等)。
   2.2. 实现下述基本操作:创建矩阵(M x N)、释放内存、获取某一元素的值、修改某一元素的值。
   2.3. 实现矩阵的乘法操作。例如矩阵A的大小为M x K,矩阵B的大小为K x N,计算矩阵C = A * B。测试用例如下:
A: [ 1.0,  0.8, -1.2; -0.1,  0.4,  0.3]
B: [ 0.9, -0.1,  0.2;  0.5, -0.2,  0.0; -0.3,  0.5,  0.9]

参考结果:

C: [ 1.66, -0.86, -0.88;  0.02,  0.08,  0.25]

第一种代码:

#include<iostream>using namespace std;template<typename T>class MAT{public:MAT(const int rows, const int cols);MAT(const MAT<T>& m);~MAT(void);//释放矩阵void cin_data(void)const;T get_OneData(const int rows, const int clos) const;//获取某一元素的值void change_OneData(const int rows, const int clos, const T value);//修改某一元素的值friend MAT<T> operator*(const MAT<T>& A, const MAT<T>& B);//实现矩阵的乘法MAT<T>& operator=(const MAT<T>& m);void print(void) const;private:int Rows, Cols;T *array;};template<typename T>MAT<T>::MAT(const int rows, const int cols)//构建矩阵{Rows = rows;Cols = cols;array = new T[Rows*Cols];}template<typename T>MAT<T>::MAT(const MAT<T>& m){Rows = m.Rows;Cols = m.Cols;array = new T[Rows*Cols];for (int i = 0; i < Rows*Cols; ++i)array[i] = m.array[i];}template<typename T>MAT<T>::~MAT(){delete[] array;}template<typename T>void MAT<T>::cin_data()const//输入矩阵数据{for (int i = 0; i < Rows*Cols; ++i)cin >> array[i];}template<typename T>T MAT<T>::get_OneData(const int i_row, const int j_col)const//获取某一元素的值{return array[Cols*(i_row - 1) + j_col - 1];}template<typename T>void MAT<T>::change_OneData(const int i_row, const int j_col, const T value)//修改某一元素的值{array[Cols*(i_row - 1) + j_col - 1] = value;}template<typename T>MAT<T> operator*(const MAT<T>& A, const MAT<T>& B){if (A.Cols != B.Rows){cerr << "error! This dimensionality of the A matrix's col and the B matrix's row are not the same!" << endl;exit(EXIT_FAILURE);}MAT<T> C(A.Rows, B.Cols);for (int i = 0; i < A.Rows*B.Cols; ++i)C.array[i] = 0;for (int i = 0; i < A.Rows; ++i)for (int j = 0; j < B.Cols; ++j)for (int k = 0; k < B.Rows; ++k)C.array[i*B.Cols + j] += A.array[i*B.Rows + k] * B.array[k*B.Cols + j];return C;}template<typename T>MAT<T>& MAT<T>::operator = (const MAT<T>& m){array = new T[m.Rows*m.Cols];for (int i = 0; i < m.Rows*m.Cols; ++i)array[i] = m.array[i];return *this;}template<typename T>void MAT<T>::print()const{for (int i = 0; i < Rows*Cols; ++i){if ((i + 1) % Cols == 0)cout << " " << array[i] << endl;elsecout << " " << array[i];}}void main(){const int rows_A = 2;const int cols_A = 2;MAT<float> A(rows_A, cols_A);cout << "Please input the first matrix's data:" << endl;A.cin_data();const int rows_B = 2;const int cols_B = 2;MAT<float> B(rows_B, cols_B);cout << "Please input the second matrix's data:" << endl;B.cin_data();const int rows_C = 2;const int cols_C = 2;MAT<float> C(rows_C, cols_C);C = A*B;C.print();cout << C.get_OneData(2, 1) << endl;//获取某一元素的值C.change_OneData(2, 1, 1.4f);//修改某一元素的值C.print();}

第二种代码:

#ifndef INCLUDE_MATRIX_HPP_#define INCLUDE_MATRIX_HPP_/* DO NOT MODIFY THE CODE BELOW */#include <iostream>template<typename T>class Matrix {public:  // constructor function  Matrix(const int r,const int c);  Matrix(const T* array, const int rows, const int cols);  // de-constructor function  ~Matrix(void){};  // create a two-dimensional matrix  void Create(const int rows, const int cols);  // destroy a two-dimensional matrix  void Destroy(void);  // obtain the number of rows  int GetRows(void) const;  // obtain the number of columns  int GetCols(void) const;  // obtain the data pointer  T* GetDataPtr(void) const;  // obtain the value of a specified element  T GetEleAt(const int r, const int c) const;  // update the value of a specified element  void SetEleAt(const int r, const int c, const T val);  // display all the elements in the matrix (row-by-row)  void DispEle(void) const;  // perform matrix-matrix multiplication  static void MatMult(const Matrix<T>& matA, const Matrix<T>& matB, Matrix<T>& matC);private:  // number of rows  int rows_;  // number of cols  int cols_;  // data pointer  T* data_;};/* DO NOT MODIFY THE CODE ABOVE */// implement the <Matrix> class below/* Write your own code below *//* Write your own code above */#endif  // INCLUDE_MATRIX_HPP_

#include <iostream>#include <cstdlib>#include <iomanip>#include "Matrix.hpp"using namespace std;template<typename T>Matrix<T>::Matrix(const int r, const int c){rows_ = r;cols_ = c;Create(rows_, cols_);for (int i = 0; i < rows_*cols_; ++i)data_[i] = 0;}template<typename T>Matrix<T>::Matrix(const T* array, const int rows, const int cols){rows_ = rows;cols_ = cols;Create(rows, cols);for (int i = 0; i < rows; ++i)for (int j = 0; j < cols;++j)data_[i*cols + j] = array[i*cols + j];}template<typename T>// create a two-dimensional matrixvoid Matrix<T>::Create(const int rows, const int cols) {data_ = new T[rows* cols];}template<typename T>// destroy a two-dimensional matrixvoid Matrix<T>::Destroy(void){delete[] data_;}// obtain the number of rowstemplate<typename T>int Matrix<T>::GetRows(void) const{return rows_;}template<typename T>// obtain the number of columnsint Matrix<T>::GetCols() const{return cols_;}template<typename T>// obtain the data pointerT* Matrix<T>::GetDataPtr(void) const{return data_;}template<typename T>// obtain the value of a specified elementT Matrix<T>::GetEleAt(const int r, const int c) const{return data_[cols_*(r - 1) + c - 1];}template<typename T>// update the value of a specified elementvoid Matrix<T>::SetEleAt(const int r, const int c, const T val){data_[cols_*(r - 1) + c - 1] = val;}template<typename T>// display all the elements in the matrix (row-by-row)void Matrix<T>::DispEle(void) const{for (int i = 0; i < rows_*cols_; ++i){if ((i + 1) % cols_ == 0)cout << " "  << data_[i] << endl;elsecout << " "  << data_[i];}}template<typename T>// perform matrix-matrix multiplicationvoid Matrix<T>::MatMult(const Matrix<T>& matA, const Matrix<T>& matB, Matrix<T>& matC){if (matA.cols_ != matB.rows_){cerr << "error! This dimensionality of the matA matrix's col and the matB matrix's row are not the same!" << endl;exit(EXIT_FAILURE);}/*for (int i = 0; i < matA.rows_*matB.cols_; ++i)matC.data_[i] = 0;*/for (int i = 0; i < matA.rows_; ++i)for (int j = 0; j < matB.cols_; ++j)for (int k = 0; k < matB.rows_; ++k)matC.data_[i*matB.cols_ + j] += matA.data_[i*matB.rows_ + k] * matB.data_[k*matB.cols_ + j];}int main(int argc, char* argv[]) {  // define matrix A and B in the array format  const int rowsA = 2;  const int colsA = 3;  const float arrayA[][colsA] = {{1.0f, 0.8f, -1.2f}, {-0.1f, 0.4f, 0.3f}};  const int rowsB = 3;  const int colsB = 3;  const float arrayB[][colsB] = {{0.9f, -0.1f, 0.2f}, {0.5f, -0.2f, 0.0f}, {-0.3f, 0.5f, 0.9f}};  // create matrix A and B and compute their product  Matrix<float> matrixA(&(arrayA[0][0]), rowsA, colsA);  Matrix<float> matrixB(&(arrayB[0][0]), rowsB, colsB);  Matrix<float> matrixC(rowsA, colsB);  Matrix<float>::MatMult(matrixA, matrixB, matrixC);  // display all the elements in matrix A, B, and C  std::cout << "Matrix A:" << std::endl;  matrixA.DispEle();  std::cout << "Matrix B:" << std::endl;  matrixB.DispEle();  std::cout << "Matrix C:" << std::endl;  matrixC.DispEle();  system("pause");  return 0;}


0 0
原创粉丝点击