⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 my_matrix.cpp

📁 本人用C++编的矩阵类
💻 CPP
字号:
#include <iostream.h>
#include <iomanip.h>
#include <stdio.h>

typedef double	ELEMTYPE;

class CMatrix  
{
private:	
	int		row,col;
	ELEMTYPE** pArray;
public:
	CMatrix  ();
	CMatrix  (int x,int y);
	CMatrix  (const CMatrix  &rhs);		
	CMatrix  (const ELEMTYPE* array,int x,int y);	
	~CMatrix  ();
	
	ELEMTYPE	getv(int x, int y);
	void	setv(int x,int y,const ELEMTYPE v);	
	int		getrow();
	int		getcol();	
	CMatrix		t();								// 矩阵转置,产生新的矩阵	
	void		display();							// 显示矩阵元素
	CMatrix		operator=(const CMatrix  & m);		// 将一个矩阵赋值给另一个矩阵
	CMatrix		operator*(const CMatrix  & m);		// 矩阵相乘,产生新的矩阵
	CMatrix		operator+(const CMatrix  & m);		// 矩阵相加,产生新的矩阵
	CMatrix		operator-(const CMatrix  & m);		// 矩阵相减,产生新的矩阵	
	
};


int		CMatrix::getrow()						{	return row; }
int		CMatrix::getcol()						{	return col; }
ELEMTYPE	CMatrix::getv(int x,int y)				{	return pArray[x][y];}
void	CMatrix::setv(int x,int y,const ELEMTYPE v)	{	pArray[x][y]=v;}

void	CMatrix::display()	
{
	for(int i=0;i<row;i++)
	{
		cout<<endl;
		for(int j=0;j<col;j++)
		{
			cout<<setw(8)<<pArray[i][j];
		}
	}
}

CMatrix::CMatrix  ()        //x 行  y:列
: row(1),col(1),pArray(NULL)
{
	pArray = new ELEMTYPE*[row];
	for(int i=0;i<row;i++)
		pArray[i] = new ELEMTYPE[col];
}

CMatrix::CMatrix  (int x,int y=1)        //x 行  y:列
:row(1),col(1),pArray(NULL)
{
	if(x<1||y<1)
		x=y=1;
	row=x;
	col=y;
	pArray = new ELEMTYPE*[row];
	for(int i=0;i<row;i++)
		pArray[i] = new ELEMTYPE[col];
}

CMatrix::CMatrix   (const ELEMTYPE* array,int x,int y)
: row(1),col(1),pArray(NULL)
{
	row=x;
	col=y;
	pArray = new ELEMTYPE*[row];
	for(int i=0;i<row;i++)
		pArray[i] = new ELEMTYPE[col];
	for(i=0;i<row;i++)
		for(int j=0;j<col;j++)
		{
			pArray[i][j] =*((ELEMTYPE *)array+i*y+j);
		}
}

//*
CMatrix::~CMatrix  ()
{
	for(int i=0;i<row;i++)
	{
		delete[] pArray[i];
	}
	delete[] pArray;
}
//

CMatrix::CMatrix(const CMatrix  & m) 
{ 
	row =m.row;
	col =m.col;
	pArray = new ELEMTYPE*[row];
	for(int i=0;i<row;i++)
		pArray[i] = new ELEMTYPE[col];
	for( i=0; i<row; i++) 
		for(int j=0; j<col; j++)
		{	
			setv(i,j,m.pArray[i][j]);
		}
}

CMatrix   CMatrix::operator=(const CMatrix  & m) 
{ 
	if(row !=m.row||col !=m.col)
	{
		for(int i=0;i<row;i++)
		{
			delete[] pArray[i];
		}
		delete[] pArray;
		row =m.row;
		col =m.col;
		pArray = new ELEMTYPE*[row];
		for(i=0;i<row;i++)
			pArray[i] = new ELEMTYPE[col];
	}
	for(int i=0; i<row; i++) 
		for(int j=0; j<col; j++)
		{	
			setv(i,j,m.pArray[i][j]);
		}
		return *this;
}

CMatrix   CMatrix::operator+(const CMatrix  & m)// 矩阵相加,原矩阵内容不变,产生一新矩阵
{
	if(row!= m.row||col!= m.col) // 必须满足相加条件
	{
		throw int(0);
	}
	CMatrix  temp(row, col); // 计算并产生一合要求的矩阵放
	ELEMTYPE a;
	
	for(int i=0; i< row; i++) // 计算
		for(int j=0; j< col; j++)
		{
			a = pArray[i][j] + m.pArray[i][j];
			temp.pArray[i][j]=a;;
		}
	return temp; // 返回
}


CMatrix   CMatrix::operator*(const CMatrix  & m) // 矩阵相乘,原矩阵内容不变,产生一新矩阵
{
	if(col!= m.row) // 必须满足相乘条件
	{
		throw int(0);
	}
	CMatrix  mp(row, m.col); // 计算并产生一合要求的矩阵放乘积
	ELEMTYPE a;
	for(int i=0; i<row; i++) // 计算乘积
		for(int j=0; j<m.col; j++)
		{
			a = 0.0;
			for(int k=0; k<col; k++)
				a += pArray[i][k]* m.pArray[k][j];
			mp.setv(i,j,a);
			a =0.0;
		}
		return mp; // 返回乘积
}



int main()
{
	CMatrix   mm(1,4),nn(4,1),mn(1,1),nm(4,4),tee;
	CMatrix   *prnew =new	CMatrix  (1,4);
	ELEMTYPE	   arr[][4]=
	{
		{1,2,3,4},
		{1,2,3,4},
		{1,2,3,4},
		{1,2,3,4}
	};
	ELEMTYPE	 ba1[]={3,20,3.09,4.87, 9,8,3,4, 5,6,6,6, 7,7,7,3};	
	
	CMatrix	 amm((ELEMTYPE*)arr,4,4);	
	CMatrix	 bmm((ELEMTYPE*)ba1,4,4);
	
	CMatrix	 cmm((ELEMTYPE*)ba1,12,1);
	amm.display();
	bmm.display();
	cmm.display();
	cin.get();
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -