matrix.cpp

来自「大规模定制中评价系统源码!对其中配置后的产品进行优化,寻优!」· C++ 代码 · 共 104 行

CPP
104
字号
#include "Matrix.h"

template <typename T>
CMatrix<typename T>::CMatrix(int nDim, int nSize)
{
	m_nDim = nDim;
	m_nSize = nSize;
	m_pNum = new T[nSize]; 
}

template <typename T>
CMatrix<typename T>::~CMatrix(void)
{
	delete [] m_pNum;
}

template <typename T>
void CMatrix<typename T>::Init()
{
	for (int i = 0; i < nDim; i++)
	{
		for (int j = 0; j < nSize/nDim; j++)
		{
			cin >> m_pNum[i * nSize / nDim + j];
			cout << "\t";
		}
		cout << endl;
	}
}

template <typename T>
 CMatrix<typename T>::CMatrix(const CMatrix<T> &otherMat)
{
	m_nDim = otherMat.m_nDim;
	m_nSize = otherMat.m_nSize;
	m_pNum = new T[m_nSize];
	for (int i = 0; i < m_nSize; i++)
	{
		m_pNum[i] = otherMat.m_pNum[i];
	}
}

 template <typename T>
 const CMatrix<T>& CMatrix<typename T>::operator =(const CMatrix<T> &otherMat)
 {
	 if (&otherMat == this)
		 return *this;
	 m_nDim = otherMat.m_nDim;
	 m_nSize = otherMat.m_nSize;
	 m_pNum = new T[m_nSize];
	 for (int i = 0; i < m_nSize; i++)
	 {
		 m_pNum[i] = otherMat.m_pNum[i];
	 }
	 return *this;
}

template <typename T>
CMatrix<T> CMatrix<typename T>::operator +(const CMatrix<T> &otherMat)
{
	if(m_nSize != otherMat.m_nSize)
		return *this;
	CMatrix<typename T> tempmat(1,m_nSize);
	for(int i = 0; i < m_nSize; i++)
	{
		tempmat.m_pNum[i] = m_pNum[i] + otherMat.m_pNum[i];
	}
	return tempmat;

}

template <typename T>
CMatrix<T> CMatrix<typename T>::operator -(const CMatrix<T> &otherMat)
{
	if(m_nSize != otherMat.m_nSize)
		return *this;
	CMatrix<typename T> tempmat(1,m_nSize);
	for(int i = 0; i < m_nSize; i++)
	{
		tempmat.m_pNum[i] = m_pNum[i] - otherMat.m_pNum[i];
	}
	return tempmat;

}

template <typename T>
CMatrix<T> CMatrix<typename T>::operator *(const CMatrix<T> &otherMat)
{
	if(m_nSize != otherMat.m_nSize)
		return *this;
	CMatrix<typename T> tempmat(1,m_nSize);
	for(int i = 0; i < m_nSize; i++)
	{
		tempmat.m_pNum[i] = m_pNum[i] * otherMat.m_pNum[i];
	}
	return tempmat;

}





⌨️ 快捷键说明

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