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

📄 matrixclass.h

📁 在C++ 提供的MFC平台
💻 H
字号:
#include<iostream.h>
#include<stdlib.h>
#include "stdafx.h"
#include "Matrix.h"
#include "MatrixDoc.h"


template<class T>
class Matrix{
public:
	int RowMax,ColMax;
	T *pArray;   

	Matrix();
	Matrix(int,int);
	Matrix(Matrix<T> &);
	~Matrix();
	int SetValue(int, int,T);
	Matrix operator=(Matrix &);
	Matrix operator+(Matrix &);
	Matrix operator-(Matrix &);
	Matrix operator *(Matrix &);
	Matrix &operator*(T,Matrix &);
	Matrix transpose(Matrix<T> &m);
	CString Print();
};


template<class T>
Matrix<T>::Matrix()
{
}

template<class T>
Matrix<T> Matrix<T>::operator=(Matrix &m)
{
	if(&m==this)
	{
		return *this;
	}
	else
	{
		RowMax=m.RowMax;
		ColMax=m.ColMax;
		pArray=new T[RowMax*ColMax];
	    for(int a=0;a<RowMax*ColMax;a++)
		{
			pArray[a]=m.pArray[a];
		}
		return *this;
	}

}

template<class T>
Matrix<T>::Matrix( int x, int y)
{
	RowMax=x;
	ColMax=y;
	pArray=new T[RowMax*ColMax];
	for( int i=0;i<x*y;i++)
	{
		pArray[i]=0;
	}
}

template<class T>
Matrix<T>::Matrix(Matrix<T> &m)
{
	RowMax=m.RowMax;
	ColMax=m.ColMax;
	pArray=new T[m.RowMax*m.ColMax];
	for(int j=0;j<m.RowMax*m.ColMax;j++)
	{
		pArray[j]=m.pArray[j];
	}
}

template<class T>
int Matrix<T>::SetValue( int x, int y,T value)
{
	if(x>RowMax||y>ColMax)
	{
		return -1;
	}
	else
	{
		pArray[x*ColMax+y]=value;
		return 0;
	}
}

template<class T>
Matrix<T>::~Matrix<T>()
{
	delete pArray;
}

template<class T>
Matrix<T> Matrix<T>::operator+(Matrix &m)
{
	if(this->RowMax!=m.RowMax||this->ColMax!=m.ColMax)
	{
		return *this;
	}
	else
	{
		Matrix r(m.RowMax,m.ColMax);
		for(int i=0;i<m.RowMax;i++)
		{
			for(int j=0;j<m.ColMax;j++)
			{
				r.pArray[i*m.ColMax+j]=this->pArray[i*m.ColMax+j]+m.pArray[i*m.ColMax+j];
			}
		}
		return r;
	}
}

template<class T>
Matrix<T> Matrix<T>::operator-(Matrix<T> &m)
{
	if(this->RowMax!=m.RowMax||this->ColMax!=m.ColMax)
	{
		return *this;
	}
	else
	{
		Matrix r(m.RowMax,m.ColMax);
		for(int i=0;i<m.RowMax;i++)
		{
			for(int j=0;j<m.ColMax;j++)
			{
				r.pArray[i*m.ColMax+j]=this->pArray[i*m.ColMax+j]-m.pArray[i*m.ColMax+j];
			}
		}
		return r;
	}
}

template<class T>
Matrix<T> Matrix<T>::operator *(Matrix<T> &m)
{
	if(this->ColMax!=m.RowMax)
	{
		return *this;
	}
	else
	{
		Matrix r(this->RowMax,m.ColMax);
		for(int i=0;i<this->RowMax;i++)
		{
			for(int j=0;j<m.ColMax;j++)
			{
				r.pArray[i*r.ColMax+j]=0;
				for(int k=0;k<this->ColMax;k++)
				{
					r.pArray[i*r.ColMax+j]+=this->pArray[i*this->ColMax+k]*m.pArray[k*m.ColMax+j];
				}
			}
		}
		return r;
	}
}

template<class T>
Matrix<T> operator*(T number,Matrix<T> &m)
{
	Matrix<T> r(m.RowMax,m.ColMax);
	for(int i=0;i<m.RowMax;i++)
	{
		for(int j=0;j<m.ColMax;j++)
		{
			r.pArray[i*m.ColMax+j]=number*m.pArray[i*m.ColMax+j];
		}
	}
	return r;
}

template<class T>
CString Matrix<T>::Print()
{
	CString string,temp;
	for(int i=0;i<RowMax;i++)
	{
		for(int j=0;j<ColMax;j++)
		{
            temp.Format("%d",pArray[i*ColMax+j]);
			string+=temp;
			string+="   ";
		}
		string+="\r\n";
	}
	return string;
}

template<class T>
Matrix<T> transpose(Matrix<T> &m)
{
	Matrix<T> mc(m.ColMax,m.RowMax);
	for(int i=0;i<m.RowMax;i++)
	{
		for(int j=0;j<m.ColMax;j++)
		{
			mc.pArray[j*mc.ColMax+i]=m.pArray[i*mc.RowMax+j];
		}
	}
	return mc;
}

extern Matrix<int> *p1=NULL,*p2=NULL;

⌨️ 快捷键说明

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