C++矩阵类.docx
- 文档编号:3233697
- 上传时间:2022-11-20
- 格式:DOCX
- 页数:84
- 大小:36.14KB
C++矩阵类.docx
《C++矩阵类.docx》由会员分享,可在线阅读,更多相关《C++矩阵类.docx(84页珍藏版)》请在冰豆网上搜索。
C++矩阵类
NO01
#include
usingnamespacestd;
classCMatrix
{
public:
CMatrix(unsignedlongm,unsignedlongn);
CMatrix(CMatrix&A);//COPY构造函数,必须要。
CMatrix(){_m=0;_n=0;_data=NULL;}//无参数构造函数,必须要。
~CMatrix(){if(_data!
=NULL)delete_data;}
voidDisplayOnCMD();
boolSetCell(unsignedlongx,unsignedlongy,doubledata);
doubleGetCell(unsignedlongx,unsignedlongy);
unsignedlongGetM(){return_m;}
unsignedlongGetN(){return_n;}
CMatrixoperator+(constCMatrix&A);
CMatrixoperator=(constCMatrix&A);
private:
unsignedlong_m,_n;
double*_data;
};
CMatrix:
:
CMatrix(unsignedlongm,unsignedlongn)
{
_m=m;_n=n;
_data=newdouble[_m*_n];
for(inti=0;i<_m*_n;i++)
_data[i]=0;
}
CMatrix:
:
CMatrix(CMatrix&A)
{
_m=A._m;_n=A._n;
_data=newdouble[_m*_n];
for(inti=0;i<_m*_n;i++)
_data[i]=A._data[i];
}
boolCMatrix:
:
SetCell(unsignedlongx,unsignedlongy,doubledata)
{
if(_data==NULL)throw;
_data[x*_n+y]=data;
returntrue;
}
doubleCMatrix:
:
GetCell(unsignedlongx,unsignedlongy)
{
if(_data==NULL)throw;
return_data[x*_n+y];
}
CMatrixCMatrix:
:
operator+(constCMatrix&A)
{
CMatrixCMTemp(_m,_n);
for(inti=0;i<_m*_n;i++)
CMTemp._data[i]=_data[i]+A._data[i];
returnCMTemp;
}
CMatrixCMatrix:
:
operator=(constCMatrix&A)
{
if(&A==this)return*this;
if(_data==NULL)CMatrix(A);
for(inti=0;i<_m*_n;i++)
_data[i]=A._data[i];
return*this;
}
voidCMatrix:
:
DisplayOnCMD()
{
if(_data==NULL)return;
for(inti=0;i<_m*_n;i++)
{
if(i%_n==0)cout< cout<<_data[i]<<""; } cout< } intmain(void) { unsignedlongm,n; cout<<"m=";cin>>m; cout<<"n=";cin>>n; CMatrixA(m,n); CMatrixB(m,n); CMatrixC(m,n); for(inti=0;i for(intk=0;k { A.SetCell(i,k,i+k); B.SetCell(i,k,i+k*2); } cout<<"AutoTest: "< cout<<"A: ";A.DisplayOnCMD(); cout<<"B: ";B.DisplayOnCMD(); C=A+B; cout<<"A+B=C,C: ";C.DisplayOnCMD(); cout<<"A: ";A.DisplayOnCMD(); cout<<"B: ";B.DisplayOnCMD(); A.SetCell(1,2,100); A.DisplayOnCMD(); cout< return0; } NO02 ifndef_MATRIX_H #define_MATRIX_H classMatrix { private: introw;//矩阵的行数 intcol;//矩阵的列数 intn;//矩阵元素个数 double*mtx;//动态分配用来存放数组的空间 public: Matrix(introw=1,intcol=1);//带默认参数的构造函数 Matrix(introw,intcol,doublemtx[]);//用数组创建一个矩阵 Matrix(constMatrix&obj);//copy构造函数 ~Matrix(){delete[]this->mtx;} voidprint()const;//格式化输出矩阵 intgetRow()const{returnthis->row;}//访问矩阵行数 intgetCol()const{returnthis->col;}//访问矩阵列数 intgetN()const{returnthis->n;}//访问矩阵元素个数 double*getMtx()const{returnthis->mtx;}//获取该矩阵的数组 //用下标访问矩阵元素 doubleget(constinti,constintj)const; //用下标修改矩阵元素值 voidset(constinti,constintj,constdoublee); //重载了一些常用操作符,包括+,-,x,=,负号,正号, //A=B Matrix&operator=(constMatrix&obj); //+A Matrixoperator+()const{return*this;} //-A Matrixoperator-()const; //A+B friendMatrixoperator+(constMatrix&A,constMatrix&B); //A-B friendMatrixoperator-(constMatrix&A,constMatrix&B); //A*B两矩阵相乘 friendMatrixoperator*(constMatrix&A,constMatrix&B); //a*B实数与矩阵相乘 friendMatrixoperator*(constdouble&a,constMatrix&B); //A的转置 friendMatrixtrv(constMatrix&A); //A的行列式值,采用列主元消去法 //求行列式须将矩阵化为三角阵,此处为了防止修改原矩阵,采用传值调用 frienddoubledet(MatrixA); //A的逆矩阵,采用高斯-若当列主元消去法 friendMatrixinv(MatrixA); }; #endif //=================================== #include #include #include #include #include"matrix.h" //带默认参数值的构造函数 //构造一个row行col列的零矩阵 Matrix: : Matrix(introw,intcol) { this->row=row;this->col=col; this->n=row*col;this->mtx=newdouble[n]; for(inti=0;i this->mtx[i]=0.0; } //用一个数组初始化矩阵 Matrix: : Matrix(introw,intcol,doublemtx[]) { this->row=row;this->col=col; this->n=row*col;this->mtx=newdouble[n]; for(inti=0;i this->mtx[i]=mtx[i]; } //拷贝构造函数,因为成员变量含有动态空间,防止传递参数 //等操作发生错误 Matrix: : Matrix(constMatrix&obj) { this->row=obj.getRow(); this->col=obj.getCol(); this->n=obj.getN(); this->mtx=newdouble[n]; for(inti=0;i this->mtx[i]=obj.getMtx()[i]; } //格式化输出矩阵所有元素 voidMatrix: : print()const { for(inti=0;i for(intj=0;j if(fabs(this->get(i,j))<=1.0e-10) cout< : left)< else cout< : left)< cout< } } //获取矩阵元素 //注意这里矩阵下标从(0,0)开始 doubleMatrix: : get(constinti,constintj)const { returnthis->mtx[i*this->col+j]; } //修改矩阵元素 voidMatrix: : set(constinti,constintj,constdoublee) { this->mtx[i*this->col+j]=e; } //重载赋值操作符,由于成员变量中含有动态分配
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 矩阵