在说矩阵前,先说一小小点关于数组的知识: 数组分为两种:ios
一个m×n的矩阵,是一个m行、n列的表,m和n是矩阵的维数。
矩阵主要完成的操做有三种:数组
/* * 矩阵测试代码 * matrix.cpp */ #include<iostream> #include"matrix.h" using namespace std; int main(void) { //测试矩阵类 matrix<int> x(3,2),y,z; int i,j; for(i = 1;i<=3;i++) for(j = 1;j<=2;j++) x(i,j) = 2*i+j; cout << "Initalized x(x,j) = 2*i + j"<<endl; cout <<"x(3,1) = "<<x(3,1)<<endl; cout<<"The metrix x is:"<<endl; cout <<x; //矩阵赋值 y = x; cout<<"The matrix y is:"<<endl; cout <<y; //两个矩阵相加结果 z = y + x; cout <<"y + x is"<<endl; cout<<z; //矩阵求负 cout<<"-(y+x) is "<<endl; cout<<-z; //矩阵相乘 matrix<int> w(2,3); for(i=1;i<=2;i++) { for(j=1;j<=3;j++) { w(i,j) = i+j; } } cout<<"Initialized w(i,j) = i+j"<<endl; cout<<"w is "<<endl; cout<< w <<endl; z = y*w; cout<<"y * w is"<<endl; cout<< z<<endl; cout<<"Hello World!"<<endl; return 0; }
matrix.h函数
/* * 矩阵类,实现了矩阵的一些基础性质:矩阵相加,相乘,矩阵转置 * matrix.h */ #ifndef MATRIX_H #define MATRIX_H #include"myexceptions.h" using namespace std; template<class T> class matrix { friend ostream& operator<<(ostream&,const matrix<T>&); public: //构造函数 matrix(int theRows = 0,int theColumns = 0); //复制构造函数 matrix(const matrix<T>&); //析构函数 ~matrix(){delete [] element;} int rows() const {return theRows;}//返回数组行数 int columns() const {return theColumns;}//返回数组列数 T& operator()(int i,int j) const;//对"()"进行运算符重载 matrix<T>& operator=(const matrix<T>&);//对"="进行运算符重载 matrix<T> operator+()const;//非数组“+” matrix<T> operator+(const matrix<T>&) const;//数组相加 matrix<T> operator-()const;//非数组减 matrix<T> operator-(const matrix<T>&) const;//数组减 matrix<T> operator*(const matrix<T>&) const;//数组乘 matrix<T>& operator+=(const T&); private: int theRows;//数组的行数 int theColumns;//数组的列数 T *element;//元素数组 }; /* * 如下是对类内各类函数的具体实现 */ //构造函数 template<class T> matrix<T>::matrix(int theRows, int theColumns) { if(theColumns < 0 || theRows < 0) throw illegalParameterValue("Rows and Columns must be >= 0"); if((theRows == 0 || theColumns == 0) && (theRows != 0 || theColumns != 0)) throw illegalParameterValue("Either both or neither rows and columns should be zero"); ///建立矩阵 this->theColumns = theColumns; this->theRows = theRows; element = new T[theRows*theColumns]; } //复制构造函数 template<class T> matrix<T>::matrix(const matrix<T> &m) { theRows = m.theRows; theColumns = m.theColumns; element = new T [theRows*theColumns]; copy(m.element, m.element+theColumns*theRows, element); } //“=”重载 template<class T> matrix<T>& matrix<T>::operator=(const matrix<T>& m) { if(this != &m) { delete [] element; theRows = m.theRows; theColumns = m.theColumns; element = new T[theColumns*theRows]; copy(m.element,m.element+theRows*theColumns,element); } return *this; } //"()"重载 template<class T> T& matrix<T>::operator ()(int i,int j) const { if(i<1 || i > theRows || j < 1 || j > theColumns) throw matrixIndexOutOfBounds(); return element[(i-1)*theColumns+j-1]; } //"+"重载 template<class T> matrix<T> matrix<T>::operator+(const matrix<T>& m)const { if(theRows != m.theRows || theColumns != m.theColumns) throw matrixSizeMismatch(); matrix<T> w(theRows,theColumns);//建立结果矩阵w for(int i = 0;i< theRows*theColumns;i++) w.element[i] = element[i] + m.element[i]; return w; } //"-"重载 template<class T> matrix<T> matrix<T>::operator - (const matrix<T>& m)const { if(theRows != m.theRows || theColumns != m.theColumns) throw matrixSizeMismatch(); matrix<T> w(theRows,theColumns);//建立结果数组 for(int i = 0;i< theColumns*theRows;i++) w.element[i] = element[i]-m.element[i]; return w; } template<class T> matrix<T> matrix<T>::operator - () const { matrix<T> w(theRows,theColumns); for(int i = 0;i<theColumns*theRows;i++) w.element[i] = -element[i]; return w; } //"*"重载,实现数组乘法 template<class T> matrix<T> matrix<T>::operator * (const matrix<T>& m) const { //计算(*this)*m的结果 if(theColumns != m.theRows) throw matrixSizeMismatch(); matrix<T> w(theRows,m.theColumns); int ct = 0,cm = 0,cw = 0; for(int i = 1;i <= theRows;i++) { for(int j = 1;j <= m.theColumns;j++) { T sum = element[ct] * m.element[cm]; for(int k = 2;k<=theColumns;k++) { ct++; cm += m.theColumns; sum += element[ct]*m.element[cm]; } w.element[cw++] = sum; ct -= theColumns - 1; cm = j; } ct += theColumns; cm = 0; } return w; } template<class T> ostream& operator<<(ostream& out,const matrix<T>& m) { int k = 0; for(int i = 0;i < m.theRows;i++) { for(int j = 0;j < m.theRows;j++) out << m.element[k++] << " "; out <<endl; } return out; } ostream& operator <<(ostream& out,const matrix<int>& m) { int k = 0; for(int i = 0;i < m.theRows;i++) { for(int j = 0;j < m.theColumns;j++) out << m.element[k++] <<" "; out <<endl; } return out; } #endif // MATRIX_H
myExceptions.h测试
/* *异常类 * myExceptions.h */ #ifndef MYEXCEPTIONS_H #define MYEXCEPTIONS_H #include<string> using namespace std; //参数值不合法 class illegalParameterValue { public: illegalParameterValue(string theMessage = "Illegal parameter value") { message = theMessage; } void outputMessage() { cout << message <<endl; } private: string message; }; //矩阵索引值越界 class matrixIndexOutOfBounds { public: matrixIndexOutOfBounds (string theMessage = "Matrix index out of bounds") { message = theMessage; } void outputMessage() { cout <<message <<endl; } private: string message; }; class matrixSizeMismatch { public: matrixSizeMismatch (string theMessage = "The size of the two matrics doesn't match") { message = theMessage; } void outputMessage() { cout << message <<endl; } private: string message; }; #endif // MYEXCEPTIONS_H