I have written a matrix class, everyone gives some opinions.

xiaoxiao2021-03-06  96

// File name: cmatrix.h // function: matrix CMAMATRIX Declaration // Author: 01 Meter (1) Class He Harui 200124151109 # i if _cmatrix_h_ # define _cmatrix_h_

Class CMatrix // Matrix class {public: // Constructor, destructuring function, copy constructor, assignment operator CMAMATRIX (int Row, int co); cmatrix (); cmatrix (const cmatrix & rhs); cmatrix & operator = Const CMAMATRIX & RHS); // Pack Matrix Bool SetMatrix (Const Double * Array, Int Size); // Get all Elements Bool getMatrix (Double * Array) in the matrix; // Get the number of rows of the matrix VOID GETROW (INT & ROW) ; // Get the column number Void getcol (INT & COL); // Display Matrix Bool Display (Void); // Matrix plus CMARRIX Operator (Const CMAMATRIX & RHS); // Matrix CMARIX Operator- (Const CMAMATRIX & RHS ); // matrix multiplied CMAtrix Operator * (const cmatrix & rhs); // constant is multiplied by matrix CMARIX Operator * (Double constant);

Private: double * m_matrix; // Pointer INT m_ROW; // Matrix of the matrix point INT m_col; // matrix of the number};

#ENDIF

// File name: cmatrix.cpp // function: Matrix CMAMATRIX implementation // Author: 01 Meter (1) Class He Harui 200124151109 # include #include "cmatrix.h"

Using namespace std;

// General constructor CMATRIX :: CMatrix (int Row, int co): m_matrix (null), m_row (0), m_col (0) {INT length = 0;

/ / The number of rows and the number of columns are specified in IF ((ROW <1) || (col <1)) {RETURN;} Length = ROW * COL;

M_matrix = new double [length]; // assigns memory space IF (NULL == m_matrix) {return;} else {m_row = row; m_col = col; // First use 0 to populate all Elements in the matrix in the matrix FOR ( INDEX = 0; Index

}

}

// Destructor CMatrix :: ~ cmatrix () {delete [] m_matrix; // Release matrix occupied by memory}

// Copy constructor cmatrix :: cmatrix (const cmatrix & rhs): m_matrix (null), m_row (rhs.m_row), m_col (rhs.m_col) {INT length = 0; if (null == rhs.m_matrix) // Matrix RHS is empty {RETURN;} length = m_row * m_col;

m_matrix = new double [length]; // For matrix allocation memory space IF (NULL == m_matrix) {m_row = 0; m_col = 0; RETURN;} else {// with the elements in the RHS matrix to fill this matrix for ( INDEX = 0; index

}

}

// Assignment Operator CMARRIX & CMATRIX :: Operator = (const cmatrix & rhs) {INT length = 0;

/ / Judgment whether the 自 (this! = & R HS) {// release the memory delete [] m_matrix; m_matrix; m_matrix = null; m_row = 0; m_col = 0;

// The matrix RHS is empty IF (rhs.m_matrix! = Null) {length = rhs.m_row * rhs.m_col;

M_matrix = new double [length]; / / to allocate memory space IF (M_Matrix! = null) {m_row = rhs.m_col; m_ row = rhs.m_col; m_ row = rhs.m_col;

// Fill this matrix for (int index = 0; index

}

} // IF ((rhs.m_row> 0) && (rhs.m_col> 0))))))

}

Return * this; // Return to the reference to this object

}

// Plug Matrix Bool CMAMATRIX :: SetMatrix (Const Double * Array, INT Size {IF ((NULL == m_matrix) || (null == array)) {Return False;}

IF (size! = (m_row * m_col)) // Length does not match {RETURN FALSE;} Else {// Fill this matrix for (int index = 0; Index

Return True;

}

}

/ / Get all Elements in the matrix BOOL CMAMATRIX:: GetMatrix (Double * Array) {INT length = 0;

IF ((null == m_matrix) || (null == array)) {return false;} else {length = m_row * m_col; // uses array array to return all element values ​​in this matrix for (int index = 0; Index

Return True;

}

}

// Get the number of rows of matrices VOID CMAMATRIX:: GETROW (INT & ROW) {row = m_row;

/ / Get the number of columns of the matrix Void CMatrix :: getcol (int & inc) {col = m_col;

// Display Matrix Bool CMAMATRIX :: Display (Void) {if (null == m_matrix) // This matrix is ​​empty {RETURN FALSE;} else {// Press line output matrix for (int Row = 0; ROW

Cout << '/ n'; // Preparing to output a line

}

Return True;

}

}

// Matrix plus CMATRIX CMAMATRIX :: Operator (const cmatrix & rhs) {intlength = 0;

/ / Judgment whether the number of rows of the two matrices and columns are equal to IF ((m_row == rhs.m_row) && (m_col == rhs.m_col)) {if (null == m_matrix) {return * this; // Returns an empty matrix} else {cmatrix tempmatrix (m_row, m_col);

Length = M_ROW * m_col;

For (int index = 0; index

Return Tempmatrix; // Returns the matrix obtained after the addition

} // IF ((M_ROW <1) || (M_COL <1))

} Else {// Defines an empty matrix and return it CMAtrix Tempmatrix (0, 0); return temptrix;}}

// Matrix Slow CMARIX CMATRIX :: Operator- (const cmatrix & r Hs) {intlength = 0;

/ / Judgment whether the number of rows of the two matrices and columns are equal to IF ((m_row == rhs.m_row) && (m_col == rhs.m_col)) {if (null == m_matrix) {return * this; // Returns an empty matrix} else {cmatrix tempmatrix (m_row, m_col);

Length = m_row * m_col; for (int index = 0; index

Return Tempmatrix; // Returns the matrix obtained after subtraction

} // IF ((M_ROW <1) || (M_COL <1))

} Else {// Defines an empty matrix and return it CMAtrix Tempmatrix (0, 0); return temptrix;}}

// Matrix multiplied CMATRIX CMATRIX :: Operator * (const cmatrix & rhs) {if (m_col == rhs.m_row) // The number of columns of the first matrix is ​​equal to the number of rows of the second matrix, etc., etc. {if (null == M_matrix) {return * this; // Returns an empty matrix} else {cmatrix tempmatrix (m_row, rhs.m_col);

For (int Row = 0; ROW

}

Return Tempmatrix;

} // if (null == m_matrix)

} Else {// Defines an empty matrix and return it CMAMATRIX TEMPMATRIX (0, 0); Return Tempmatrix;}

}

// constant is multiplied by the matrix CMATRIX CMATRIX :: Operator * (Double constant) {intlength = 0;

// Whether this matrix is ​​empty if (null == m_matrix) {return * this; // Returns an empty matrix} else {cmatrix Tempmatrix (m_row, m_col);

Length = M_ROW * m_col;

For (int index = 0; index

Return Tempmatrix; // Return the matrix of the resulting phase

}

}

转载请注明原文地址:https://www.9cbs.com/read-104290.html

New Post(0)