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

📄 realmatrix.cpp

📁 这是一个实数矩阵计算的源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
// SparseMatrix.cpp: implementation of the CRealMatrix class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "RealMatrix.h"
#include "afxtempl.h"
#include "math.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CRealMatrix::CRealMatrix()
{
   pHead=NULL;
   pTail=NULL;
   pCurrent=NULL;
   pData=NULL;
}



CRealMatrix::CRealMatrix(int row,int col)
{
	Create(row,col);
}

CRealMatrix::CRealMatrix(const CRealMatrix &src)
{
	Copy(src);
}

CRealMatrix::~CRealMatrix()
{
	Empty();
}

////////////////////////////////////////////////////
//////////////////////////////////////////////////
//operation
///////////////////////////////////////////////////

void CRealMatrix::SetSize(int row,int col)
{
	if(IsEmpty()) 
	{
		Create(row,col);
		return;
	}

	if(pHead->nRow==row&&pHead->nCol==col)
		return;
	CRealMatrix src;//constructor a temple matrix
	src.pHead=pHead;//copy matrix
	src.pTail=pTail;
	src.pData=pData;
	src.pCurrent=pCurrent;
	pHead=NULL;

	//the size of matrix extend,
	Create(row,col);

	//copy the value tothe new matrix
	//if the size of new matrix is bigger than primivate matrix
	//copy the all,otherwise  copy partion;

	//copy node of data
	CNode* psrcCurrent=src.pHead;
	if(psrcCurrent!=NULL)
	{
		pCurrent=pHead;
		psrcCurrent=psrcCurrent->pNext;
		while(psrcCurrent!=src.pTail&&psrcCurrent->nRow<=row)
		{
			pCurrent=pCurrent->pNext;
			if(psrcCurrent->pRight==NULL)
			{
				psrcCurrent=psrcCurrent->pNext;
				continue;
			}
			CNode *psrcData=psrcCurrent->pRight;
			pData=pCurrent;
			while(psrcData!=NULL&&psrcData->nCol<=col)
			{
				//row table
				pData->pRight=new CNode;
				pData=pData->pRight;
				pData->nRow=psrcData->nRow;
				pData->nCol=psrcData->nCol;
				pData->pRight=NULL;
				pData->pDown=NULL;
				pData->data=psrcData->data;
				
				//col table
				CNode *pTemp=pHead;
				for(int i=0;i<pData->nCol;++i)
					pTemp=pTemp->pNext;
				while(pTemp->pDown!=NULL)
					pTemp=pTemp->pDown;
				pTemp->pDown=pData;

				psrcData=psrcData->pRight;
			}
		psrcCurrent=psrcCurrent->pNext;
		}
	}
	pCurrent=pHead;
	//delete tempmatrix
	src.Empty();
}

void CRealMatrix::Create(int row,int col)
{
	if(row==0||col==0) return;

	//初始化各节点指针

	pHead=NULL;
	pTail=NULL;
	pCurrent=NULL;
	pData=NULL;

	if(row>0&&col>0)
	{
		//对矩阵的头指针进行初始化,存放矩阵的信息

		pHead=new CNode;
		pHead->nRow=row;
		pHead->nCol=col;
		pHead->pNext=NULL;
		pHead->pPrev=NULL;
		int n=(row>col)?row:col;	//判断行、列矢量存储
		pCurrent=pHead;
		for(int i=1;i<=n;++i)
		{
			pCurrent->pNext=new CNode;
			pCurrent->pNext->pPrev=pCurrent;
			pCurrent=pCurrent->pNext;
			if(row>=i) pCurrent->nRow=i;
			else pCurrent->nRow=0;
			if(col>=i) pCurrent->nCol=i;
			else pCurrent->nCol=0;
	
			pCurrent->pNext=NULL;
			pCurrent->pRight=NULL;
			pCurrent->pDown=NULL;
		}

		//初始化尾节点,并存放矩阵的信息

		pTail=new CNode;
		pTail->nRow=row;
		pTail->nCol=col;
		pTail->pNext=NULL;
		pTail->pPrev=pCurrent;
		pCurrent->pNext=pTail;

		pCurrent=pHead;
		pData=pHead;
	}
}


void CRealMatrix::Copy(const CRealMatrix &src)
{
	//初始化当前矩阵
	pHead=NULL;
	pTail=NULL;
	pCurrent=NULL;
	pData=NULL;

    //copy head pointer 
	CNode *psrcCurrent=src.pHead;//define a pointer,point the head of src
    while(psrcCurrent!=NULL)
	{
		if(pHead==NULL)			//if the matrix is null,create a head pointer
		{
			pCurrent=new CNode;
			pCurrent->pPrev=NULL;
			pHead=pCurrent;
		}
		else					//if not null
		{
			pCurrent->pNext=new CNode;
			pCurrent->pNext->pPrev=pCurrent;
			pCurrent=pCurrent->pNext;
		}
		pCurrent->nRow=psrcCurrent->nRow;
		pCurrent->nCol=psrcCurrent->nCol;
		pCurrent->pRight=NULL;
		pCurrent->pDown=NULL;
		pCurrent->pNext=NULL;
		pTail=pCurrent;

		psrcCurrent=psrcCurrent->pNext;
	}
	
	//copy node of data
	psrcCurrent=src.pHead;
	if(psrcCurrent!=NULL)
	{
		pCurrent=pHead;
		psrcCurrent=psrcCurrent->pNext;
		while(psrcCurrent!=src.pTail)	//链表是否到尾节点
		{
			pCurrent=pCurrent->pNext;
			CNode *psrcData=psrcCurrent->pRight;
			pData=pCurrent;//pData为一缓存节点
			while(psrcData!=NULL)
			{
				//row table
				pData->pRight=new CNode;
				pData=pData->pRight;
				pData->nRow=psrcData->nRow;
				pData->nCol=psrcData->nCol;
				pData->pRight=NULL;
				pData->pDown=NULL;
				pData->data=psrcData->data;
				
				//col table
				CNode *pTemp=pHead;
				for(int i=0;i<pData->nCol;++i)
					pTemp=pTemp->pNext;
				while(pTemp->pDown!=NULL)
					pTemp=pTemp->pDown;
				pTemp->pDown=pData;

				psrcData=psrcData->pRight;
			}
		psrcCurrent=psrcCurrent->pNext;//取下一节点
		}
	}
	pCurrent=pHead;
}


///////////////////////////////////////////
//Function
//////////////////////////////////////////

bool CRealMatrix::IsEmpty()
{
	if(pHead==NULL) return TRUE;
	return FALSE;
}

void CRealMatrix::operator =(const CRealMatrix &src)
{
	Empty();
	Copy(src);
}

void CRealMatrix::Empty()
{
	if(pHead!=NULL)
	{
		pCurrent=pHead->pNext;
		delete pHead;
		pHead=NULL;
		while(pCurrent!=pTail)
		{
			pData=pCurrent;
			pCurrent=pCurrent->pNext;
			while(pData!=NULL)
			{
				CNode *pTemp=pData->pRight;
				delete pData;
				pData=pTemp;
			}
		}
		delete pTail;
		pTail=NULL;
	}
}

void CRealMatrix::DeleteNode(int row,int col)
{
	if(pHead==NULL||row>pHead->nRow||col>pHead->nCol)   return;

		pData=pCurrent;

		if(pData==pHead)
		{
			for(int i=0;i<row;++i)
				pData=pData->pNext;
		}

		else if(pData->nRow<row)
		{
			while(pData->nRow<row)
				pData=pData->pNext;
		}

		else if(pData->nRow>row||pData->nRow==0||pData==pTail)
		{
			while(pData->nRow>row||pData->nRow==0||pData==pTail)
				pData=pData->pPrev;
		}

		//修改行表
		if(pData->pRight==NULL)		return;

		while(pData->pRight!=NULL&&pData->pRight->nCol<col)
			pData=pData->pRight;

		CNode* pData1=new CNode;

		if(pData->pRight==NULL||pData->pRight->nCol>col)	return;

		pData1=pData->pRight;
		if(pData1->pRight==NULL)
			pData->pRight=NULL;
		else 
			pData->pRight=pData1->pRight;

		pData1->pRight=NULL;

		pData=pCurrent;
		if(pData==pHead)
		{
			for(int i=0;i<col;++i)
				pData=pData->pNext;
		}

		else if(pData->nCol<col)
		{
			while(pData->nCol<col)
				pData=pData->pNext;
		}

		else if(pData->nCol>col||pData->nCol==0||pData==pTail)
		{
			while(pData->nCol>col||pData->nCol==0||pData==pTail)
				pData=pData->pPrev;
		}
		//修改列表

		while(pData->pDown!=NULL&&pData->pDown->nRow!=row)
			pData=pData->pDown;
		pData1=pData->pDown;
		if(pData1->pDown==NULL)
			pData->pDown=NULL;
		else
			pData->pDown=pData1->pDown;

		delete pData1;
}





void CRealMatrix::Store(int row,int col,const double newElement)
{
	if(newElement==0) 
	{
		DeleteNode(row,col);
		return;
	}
	if(pHead!=NULL&&row>0&&row<=pHead->nRow&&col>0&&col<=pHead->nCol)
	{
		pData=pCurrent;

		if(pData==pHead)
		{
			for(int i=0;i<row;++i)
				pData=pData->pNext;
		}
		else if(pData->nRow<row)
		{
			while(pData->nRow<row)
				pData=pData->pNext;
		}
		else if(pData->nRow>row||pData->nRow==0||pData==pTail)
		{
			while(pData->nRow>row||pData->nRow==0||pData==pTail)
				pData=pData->pPrev;
		}

		while(pData->pRight!=NULL&&pData->pRight->nCol<col)
			pData=pData->pRight;

		//如果矩阵有该元素,则覆盖

		if(pData->pRight!=NULL&&pData->pRight->nCol==col)
		{
			//replace an item
			pData=pData->pRight;
			pData->data=newElement;
		}
		else//否则,插入一节点
		{
			CNode *pNewNode=new CNode;
			pNewNode->nRow=row;
			pNewNode->nCol=col;
			pNewNode->data=newElement;

			//row add an item
			pNewNode->pRight=pData->pRight;
			pData->pRight=pNewNode;

			//col add an item
			pData=pCurrent;

			if(pData==pHead)
			{
				for(int i=0;i<col;++i)
					pData=pData->pNext;
			}

			else if(pData->nCol<col)
			{
				while(pData->nCol<col)
					pData=pData->pNext;
			}
			else if(pData->nCol>col||pData->nCol==0||pData==pTail)
			{
				while(pData->nCol>col||pData->nCol==0||pData==pTail)
					pData=pData->pPrev;
			}

			while(pData->pDown!=NULL/*&&pData->pDown->nRow<row*/)
				pData=pData->pDown;
			pData->pDown=pNewNode;
			pNewNode->pDown=NULL;
			//pData=pNewNode;
		}
	}
}

void CRealMatrix::Add(int row,int col,const double newElement)
{
	if(newElement==0) return;
	if(pHead!=NULL&&row>0&&row<=pHead->nRow&&col>0&&col<=pHead->nCol)
	{
		pData=pCurrent;
		if(pData==pHead)
		{
			for(int i=0;i<row;++i)
				pData=pData->pNext;
		}
		else if(pData->nRow<row)
		{
			while(pData->nRow<row)
				pData=pData->pNext;
		}
		else if(pData->nRow>row||pData->nRow==0||pData==pTail)
		{
			while(pData->nRow>row||pData->nRow==0||pData==pTail)
				pData=pData->pPrev;
		}

		while(pData->pRight!=NULL&&pData->pRight->nCol<col)
			pData=pData->pRight;
			//store newElement
		CNode *pNewNode=new CNode;
		pNewNode->nRow=row;
		pNewNode->nCol=col;
		pNewNode->data=newElement;

		//row add an item
		pNewNode->pRight=pData->pRight;
		pData->pRight=pNewNode;

		//col add an item
		pData=pCurrent;
		if(pData==pHead)
		{
			for(int i=0;i<col;++i)
				pData=pData->pNext;
		}
		else if(pData->nCol<col)
		{
			while(pData->nCol<col)
				pData=pData->pNext;
		}
		else if(pData->nCol>col||pData->nCol==0||pData==pTail)
		{
			while(pData->nCol>col||pData->nCol==0||pData==pTail)
				pData=pData->pPrev;
		}

		while(pData->pDown!=NULL/*&&pData->pDown->nRow<row*/)
			pData=pData->pDown;
		pData->pDown=pNewNode;
		pNewNode->pDown=NULL;
		//pData=pNewNode;
	}
}

void CRealMatrix::GetData(int row,int col,double &data)
{
	if(pHead!=NULL&&row>0&&row<=pHead->nRow&&col>0&&col<=pHead->nCol)
	{
		pData=pCurrent;
		if(pData==pHead)
		{
			for(int i=0;i<row;++i)
				pData=pData->pNext;
		}
		else if(pData->nRow<row)
		{
			while(pData->nRow<row)
				pData=pData->pNext;
		}
		else if(pData->nRow>row||pData->nRow==0||pData==pTail)
		{
			while(pData->nRow>row||pData->nRow==0||pData==pTail)
				pData=pData->pPrev;
		}

		while(pData->pRight!=NULL&&pData->pRight->nCol<col)
			pData=pData->pRight;
		if(pData->pRight!=NULL&&pData->pRight->nCol==col)
		{
			pData=pData->pRight;
			data=pData->data;
		}
		else
			data=0;
	}
}
/////////////////////////////////////////////////////////
// Matrix operation
///////////////////////////////////////////////////////

CRealMatrix CRealMatrix::GetRow(int row)
{
	CRealMatrix NewMatrix(1,pHead->nCol);

	if(pHead!=NULL&&row<=pHead->nRow)
	{
		pData=pCurrent;

		if(pData==pHead)
		{
			for(int i=0;i<row;++i)
				pData=pData->pNext;
		}
		else if(pData->nRow<row)
		{
			while(pData->nRow<row)
				pData=pData->pNext;
		}
		else if(pData->nRow>row||pData->nRow==0||pData==pTail)
		{
			while(pData->nRow>row||pData->nRow==0||pData==pTail)
				pData=pData->pPrev;
		}
		pData=pData->pRight;
		while(pData!=NULL)
		{
			NewMatrix.Store(1,pData->nCol,pData->data);
			pData=pData->pRight;
		}

	}
	return NewMatrix;
}

CRealMatrix CRealMatrix::GetCol(int col)
{
	CRealMatrix NewMatrix(pHead->nRow,1);
	if(pHead!=NULL&&col<=pHead->nCol)
	{

		pData=pCurrent;
		if(pData==pHead)
		{
			for(int i=0;i<col;++i)
				pData=pData->pNext;
		}
		else if(pData->nCol<col)
		{
			while(pData->nCol<col)
				pData=pData->pNext;
		}
		else if(pData->nCol>col||pData->nCol==0||pData==pTail)
		{
			while(pData->nCol>col||pData->nCol==0||pData==pTail)
				pData=pData->pPrev;
		}
		pData=pData->pDown;
		while(pData!=NULL)
		{
			NewMatrix.Store(pData->nRow,1,pData->data);
			pData=pData->pDown;
		}
	}
	return NewMatrix;
}


void CRealMatrix::GoToHead()
{
	pCurrent=pHead;
}

void CRealMatrix::GoToTail()
{
	pCurrent=pTail;
}

void CRealMatrix::GoNext()
{
	if(pCurrent->pNext!=pTail)
	{
		pCurrent=pCurrent->pNext;
		pData=pCurrent;
	}

⌨️ 快捷键说明

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