C++矩阵类

2019-02-14 21:55

NO 01

#include

using namespace std;

class CMatrix { public:

CMatrix(unsigned long m,unsigned long n);

CMatrix(CMatrix& A); // COPY构造函数,必须要。 CMatrix(){_m=0;_n=0;_data=NULL;}//无参数构造函数,必须要。 ~CMatrix(){if(_data!=NULL)delete _data;}

void DisplayOnCMD();

bool SetCell(unsigned long x,unsigned long y,double data); double GetCell(unsigned long x,unsigned long y); unsigned long GetM(){return _m;} unsigned long GetN(){return _n;} CMatrix operator+(const CMatrix &A); CMatrix operator=(const CMatrix &A); private:

unsigned long _m,_n; double* _data; };

CMatrix::CMatrix(unsigned long m,unsigned long n) {

_m=m;_n=n;

_data=new double[_m*_n]; for(int i=0;i<_m*_n;i++) _data[i]=0; }

CMatrix::CMatrix(CMatrix& A) {

_m=A._m;_n=A._n; _data=new double[_m*_n]; for(int i=0;i<_m*_n;i++) _data[i]=A._data[i]; }

bool CMatrix::SetCell(unsigned long x,unsigned long y,double data) {

if(_data==NULL)throw; _data[x*_n+y]=data;

return true; }

double CMatrix::GetCell(unsigned long x,unsigned long y) {

if(_data==NULL)throw; return _data[x*_n+y]; }

CMatrix CMatrix::operator+(const CMatrix &A) {

CMatrix CMTemp(_m,_n);

for(int i=0;i<_m*_n;i++)

CMTemp._data[i]=_data[i]+A._data[i];

return CMTemp; }

CMatrix CMatrix::operator=(const CMatrix &A) {

if(&A==this)return *this; if(_data==NULL)CMatrix(A);

for(int i=0;i<_m*_n;i++) _data[i]=A._data[i]; return *this; }

void CMatrix::DisplayOnCMD() {

if(_data==NULL)return; for(int i=0;i<_m*_n;i++) {

if(i%_n==0)cout<

}

cout<

int main(void) {

unsigned long m,n; cout<<\ cout<<\

CMatrix A(m,n); CMatrix B(m,n); CMatrix C(m,n);

for(int i=0;i

A.SetCell(i,k,i+k); B.SetCell(i,k,i+k*2); }

cout<<\ cout<<\ cout<<\ C=A+B;

cout<<\

cout<<\ cout<<\ A.SetCell(1,2,100); A.DisplayOnCMD(); cout<

NO 02

ifndef _MATRIX_H #define _MATRIX_H

class Matrix {

private:

int row; // 矩阵的行数 int col; // 矩阵的列数 int n; // 矩阵元素个数

double* mtx; // 动态分配用来存放数组的空间 public:

Matrix(int row=1, int col=1); // 带默认参数的构造函数 Matrix(int row, int col, double mtx[]); // 用数组创建一个矩阵 Matrix(const Matrix &obj); // copy构造函数 ~Matrix() { delete[] this->mtx; }

void print()const; // 格式化输出矩阵 int getRow()const { return this->row; } // 访问矩阵行数 int getCol()const { return this->col; } // 访问矩阵列数

int getN()const { return this->n; } // 访问矩阵元素个数 double* getMtx()const { return this->mtx; } // 获取该矩阵的数组

// 用下标访问矩阵元素

double get(const int i, const int j)const; // 用下标修改矩阵元素值

void set(const int i, const int j, const double e);

// 重载了一些常用操作符,包括 +,-,x,=,负号,正号, // A = B

Matrix &operator= (const Matrix &obj); // +A

Matrix operator+ ()const { return *this; } // -A

Matrix operator- ()const; // A + B

friend Matrix operator+ (const Matrix &A, const Matrix &B); // A - B

friend Matrix operator- (const Matrix &A, const Matrix &B); // A * B 两矩阵相乘

friend Matrix operator* (const Matrix &A, const Matrix &B); // a * B 实数与矩阵相乘

friend Matrix operator* (const double &a, const Matrix &B); // A 的转置

friend Matrix trv(const Matrix &A); // A 的行列式值,采用列主元消去法

// 求行列式须将矩阵化为三角阵,此处为了防止修改原矩阵,采用传值调用 friend double det(Matrix A);

// A 的逆矩阵,采用高斯-若当列主元消去法 friend Matrix inv(Matrix A); };

#endif

//===================================

#include #include #include #include #include\

// 带默认参数值的构造函数 // 构造一个row行col列的零矩阵 Matrix::Matrix(int row, int col) { this->row = row; this->col = col; this->n = row * col; this->mtx = new double[n]; for(int i=0; imtx[i] = 0.0; }

// 用一个数组初始化矩阵 Matrix::Matrix(int row, int col, double mtx[])

{ this->row = row; this->col = col; this->n = row * col; this->mtx = new double[n]; for(int i=0; imtx[i] = mtx[i]; }

// 拷贝构造函数,因为成员变量含有动态空间,防止传递参数 // 等操作发生错误 Matrix::Matrix(const Matrix &obj) { this->row = obj.getRow(); this->col = obj.getCol(); this->n = obj.getN(); this->mtx = new double[n]; for(int i=0; imtx[i] = obj.getMtx()[i];

} // 格式化输出矩阵所有元素 void Matrix::print()const


C++矩阵类.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:天津重点中学2010年八年级数学下学期期末测试题人教版[1]

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: