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

📄 matrix.cpp

📁 在VC环境下采用希尔密码体制加密、解密和破译。该软件实现了输入任意长度的密钥对文件进行加密和解密
💻 CPP
字号:
#include "stdafx.h"
#include "Matrix.h"
#include "stdlib.h"

inline CMatrix::CMatrix()
: m_pMatrix(NULL), m_nRow(0), m_nCol(0)
{
}

CMatrix::CMatrix(const CMatrix & matrix)
{
	if(&matrix == this)
	{
		return;
	}
	m_pMatrix = NULL;
	InitMatrix(matrix.m_nRow, matrix.m_nCol);
	
	memcpy(m_pMatrix, (const char *)matrix.m_pMatrix, sizeof(long)*m_nRow*m_nCol);
}

CMatrix::~CMatrix()
{
	if(NULL != m_pMatrix)
	{
		delete m_pMatrix;
		m_pMatrix = 0;
	}
}

void CMatrix::InitMatrix(int nRows, int nCols)
{
	if(nRows <= 0 || nCols <= 0)
	{
		return;
	}
	if(NULL != m_pMatrix)
	{
		delete m_pMatrix;
		m_pMatrix = NULL;
	}
	
	m_pMatrix = new long[nRows * nCols];
	if(NULL == m_pMatrix)
	{
		exit(1);
	}

	m_nRow = nRows;
	m_nCol = nCols;
}

CMatrix & CMatrix::operator = (const CMatrix & matrix)
{
	if(&matrix == this)
	{
		return (*this);
	}

	InitMatrix(matrix.m_nRow, matrix.m_nCol);
	
	memcpy(m_pMatrix, (const char *)matrix.m_pMatrix, sizeof(long)*m_nRow*m_nCol);

	return (*this);
}	


void CMatrix::SetElement(int nRow, int nCol, long lValue)
{
	if(nRow >= m_nRow || nCol >= m_nCol)
	{
		return;
	}

	m_pMatrix[nRow*m_nCol+nCol] = lValue;
}

int CMatrix::GetElement(int nRow, int nCol, long & lValue) const
{
	if(nRow >= m_nRow || nCol >= m_nCol)
	{
		return 0;
	}

	lValue = *(m_pMatrix+nRow*m_nCol+nCol);

	return 1;
}

int CMatrix::GetRows() const
{
	return m_nRow;
}

int CMatrix::GetCols() const
{
	return m_nCol;
}

CMatrix CMatrix::operator * (const CMatrix & right)
{
	if(m_nCol != right.m_nRow)
	{
		return CMatrix();
	}

	CMatrix result;
	result.InitMatrix(m_nRow, right.m_nCol);
	memset((char *)result.m_pMatrix, 0, sizeof(long)*result.m_nRow*result.m_nCol);
	
	int j = 0;
	int k = 0;
	long lValue = 0;
	for(int i=0; i<result.m_nRow; i++)
	{
		for(j=0; j<result.m_nCol; j++)
		{
			for(k=0,lValue = 0; k<m_nCol; k++)
			{
				lValue += 
					((long)m_pMatrix[i*m_nCol+k] * (long)right.m_pMatrix[k*right.m_nCol+j]);
			}
			result.m_pMatrix[i*result.m_nCol+j] = lValue;
		}
	}

	return result;
}

void CMatrix::Mod(int nModulus)
{
	if(nModulus <= 0)
	{
		return;
	}

	int j = 0;
	for(int i=0; i<m_nRow; i++)
	{
		for(j=0; j<m_nCol; j++)
		{
			m_pMatrix[i*m_nCol+j] %= nModulus;
			if(0 > m_pMatrix[i*m_nCol+j])
			{
				m_pMatrix[i*m_nCol+j] += nModulus;
			}
		}
	}
}

long CMatrix::GetPhalanxValue()
{
	if(m_nRow != m_nCol)
	{
		return 0;
	}

	return GetPhalanxValue((const long *)m_pMatrix, m_nRow);
}

long CMatrix::GetPhalanxValue(const long *pPhalanx, const int RANGE)
{
	if(1 == RANGE)
	{
		return (*pPhalanx);
	}
	else
	{
		const int LINE = 0;
		long lPhalanxValue = 0;
		long lCoef = 1;
		for(int nCol=0; nCol<RANGE; nCol++)
		{
			long *pSubPhalanx = NULL;
			if((LINE+nCol+2)%2)
			{
				lCoef = -1;
			}
			else
			{
				lCoef = 1;
			}
			pSubPhalanx = GetSubPhalanx(pPhalanx, RANGE, RANGE, LINE, nCol);
			lPhalanxValue += (lCoef * (long)pPhalanx[nCol] * GetPhalanxValue(pSubPhalanx, RANGE - 1));
			if(0 != pSubPhalanx)
			{
				delete pSubPhalanx;
				pSubPhalanx = NULL;
			}
		}
		
		return lPhalanxValue;
	}
}

long * CMatrix::GetSubPhalanx(const long *pPhalanx, const int ROW, const int COL, 
							  const int I, const int J)
{
	long *pSubPhalanx = NULL;
	pSubPhalanx = new long[(ROW-1)*(COL-1)];
	if(0 == pSubPhalanx)
	{
		exit(1);
	}
	int i = 0; //子阵的下标(行)
	int j = 0; //子阵的下标(列)
	int nRow = 0; //原矩阵的下标(行)
	int nCol = 0; //原矩阵的下标(列)

	for(i=0,nRow=0; (i<ROW-1)&&(nRow<ROW); i++,nRow++)
	{
		if(I == nRow)
		{
			nRow++;
		}
		for(j=0,nCol=0; (j<COL-1)&&(nCol<COL); j++,nCol++)
		{
			if(J == nCol)
			{
				nCol++;
			}
			pSubPhalanx[i*(COL-1)+j] = pPhalanx[nRow*COL+nCol];
		}
	}

	return pSubPhalanx;
}

CMatrix::CMatrix(const long *pMatrix, const unsigned int ROW, const unsigned int COL)
{
	m_nRow = ROW;
	m_nCol = COL;
	m_pMatrix = NULL;
	m_pMatrix = (long *)new long[ROW*COL];
	if(0 == m_pMatrix)
	{
		exit(1);
	}
	memcpy(m_pMatrix, pMatrix, ROW*COL*sizeof(long));
}

ostream & operator<< (ostream &out, const CMatrix &matrix)
{
	int i = 0;
	int j = 0;
	long lValue = 0;

	for(i=0; i<matrix.m_nRow; i++)
	{
		for(j=0; j<matrix.m_nCol; j++)
		{
			matrix.GetElement(i, j, lValue);
			out<<lValue<<",\t";
		}
		out<<endl;
	}

	return out;
}

void CMatrix::Transpose()
{
	CMatrix matrixTrans;
	matrixTrans.InitMatrix(m_nCol, m_nRow);
	
	int j = 0;
	for(int i=0; i<m_nRow; i++)
	{
		for(j=0; j<m_nCol; j++)
		{
			matrixTrans.m_pMatrix[j*m_nRow+i] = m_pMatrix[i*m_nCol+j];
		}
	}
	InitMatrix(m_nCol, m_nRow);
	*this = matrixTrans;
}

CMatrix & CMatrix::Adjoint()
{
	CMatrix matrixAdjoint;
	matrixAdjoint.InitMatrix(m_nRow, m_nCol);
	
	if(1 == m_nRow || 1 == m_nCol)
	{
		matrixAdjoint = *this;
		matrixAdjoint.Transpose();
		*this = matrixAdjoint;

		return *this;
	}

	int i = 0;
	int j = 0;
	long lCoef = 1;
	long * pSubPhalanx = NULL;
	
	for(i=0; i<m_nRow; i++)
	{
		for(j=0; j<m_nCol; j++)
		{
			if((i+j)%2)
			{
				lCoef = -1;
			}
			else
			{
				lCoef = 1;
			}
			pSubPhalanx = GetSubPhalanx(m_pMatrix, m_nRow, m_nCol, i, j);
			matrixAdjoint.m_pMatrix[i*m_nCol+j] = lCoef * GetPhalanxValue(pSubPhalanx, m_nRow-1);
			if(0 != pSubPhalanx)
			{
				delete pSubPhalanx;
				pSubPhalanx = NULL;
			}
		}
	}

	matrixAdjoint.Transpose();
	*this = matrixAdjoint;

	return *this;
}

CMatrix & CMatrix::operator / (const long lDivisor)
{
	if(!lDivisor)
	{
		return *this;
	}

	int i = 0;
	int j = 0;
	for(i=0; i<m_nRow; i++)
	{
		for(j=0; j<m_nCol; j++)
		{
			m_pMatrix[i*m_nCol+j] /= lDivisor;
		}
	}

	return *this;
}

CMatrix CMatrix::operator * (const long right)
{
	int i = 0;
	int j = 0;

	for(i=0; i<m_nRow; i++)
	{
		for(j=0; j<m_nCol; j++)
		{
			m_pMatrix[i*m_nCol+j] *= right;
		}		
	}

	return *this;
}

long Reciprocal(int nSrc)
{
	nSrc =nSrc % MAPLEN;
	if(nSrc < 0) 
	{
		nSrc += MAPLEN;
	}

	for(int i=1; i<MAPLEN; i++)
	{
		if(((i*nSrc)%MAPLEN) == 1)
		{
			return i;
		}
	}
		return 0;
}

const long * CMatrix::GetMatrix() const
{
	return m_pMatrix;
}

BOOL CMatrix::IsEmpty()
{
	return !m_pMatrix;
}

BOOL CMatrix::operator ==(const CMatrix &right)
{
	if(&right == this)
	{
		return TRUE;
	}
	if(m_nRow != right.m_nRow || m_nCol != right.m_nCol)
	{
		return FALSE;
	}

	int i = 0;
	int j = 0;
	for(i=0; i<m_nRow; i++)
	{
		for(j=0; j<m_nCol; j++)
		{
			if(m_pMatrix[i*m_nCol+j] != right.m_pMatrix[i*m_nCol+j])
			{
				return FALSE;
			}
		}
	}

	return TRUE;
}

⌨️ 快捷键说明

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