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

📄 matrix.cpp

📁 自己写的矩阵类
💻 CPP
字号:
#ifndef DLL_FILE_MATRIX
#define DLL_FILE_MATRIX
#endif

#include <iostream.h>
#include "Matrix.h"
#include <exception>
using namespace std;

Matrix& Matrix::operator=(const Matrix &mat)
{
	if (this == &mat)
	{
		return *this;
	}
	//如果= 号前后的对象大小不一样 清除本身对象 
	if ((m_row != mat.m_row)||(m_col != mat.m_col) )//不同大小的矩阵相等是个错误
	{
		if (m_data != NULL)
		{
			for (int i = 0; i < m_row;i++)
			{
				delete[] m_data[i];
			}
			delete[] m_data;
		}
		m_data = NULL;
	}
	
	m_row = mat.m_row;
	m_col = mat.m_col;
	int i, j;
	
	if (NULL == m_data)// 如果数据没有分配存储
	{
		m_data = new MATRIXTYPE*[m_row];		
		for (i = 0 ; i < m_row ;++i)
		{
			m_data[i] = new MATRIXTYPE[m_col];
		}
		for (i = 0 ; i < m_row ; ++i)
		{
			for (j = 0 ; j < m_col ; ++j)
			{
				m_data[i][j] = (MATRIXTYPE)0;
			}
		}
	}
	//数据拷贝
	for (i = 0; i < m_row ; ++i)
	{
		for (j = 0 ; j < m_col; ++j)
		{
			m_data[i][j] = mat.m_data[i][j];
		}
	}
	return *this;
}

//+号运算符号 重载为
const Matrix operator+(const Matrix mat1,const Matrix mat2)
{
	int i,j;	
	if ((mat1.m_row != mat2.m_row)||(mat1.m_col != mat2.m_col) )//不同大小的矩阵相加是个错误
	{
		terminate();
	}
	
	Matrix res(mat1.m_row,mat1.m_col);
	
	for(i = 0 ; i < mat1.m_row ; i++)
		for (j = 0 ; j < mat1.m_col;j++)
		{
			res.m_data[i][j] = mat1.m_data[i][j] + mat2.m_data[i][j];
		}
	return res;
}
// * 号运算符号 重载为
const Matrix operator*(const Matrix mat1,const Matrix mat2)
{
	int i,j;	
	if (mat1.m_col != mat2.m_row )//不同大小的矩阵相乘是个错误
	{
		terminate();
	}
	
	Matrix res(mat1.m_row,mat2.m_col);
	int k;
	for(i = 0 ; i < mat1.m_row ; i++)
		for (j = 0 ; j < mat2.m_col;j++)
		{
			res.m_data[i][j] = 0;
			for (k = 0 ; k < mat1.m_col ; k++)
			{
				res.m_data[i][j] += mat1.m_data[i][k] * mat2.m_data[k][j];
			}
		}
		return res;
}

//  - 号运算符号 重载为
const Matrix operator-(const Matrix mat1,const Matrix mat2)
{
	int i,j;	
	if ((mat1.m_row != mat2.m_row)||(mat1.m_col != mat2.m_col) )//不同大小的矩阵相加是个错误
	{
		terminate();
	}
	
	Matrix res(mat1.m_row,mat1.m_col);
	
	for(i = 0 ; i < mat1.m_row ; i++)
		for (j = 0 ; j < mat1.m_col;j++)
		{
			res.m_data[i][j] = mat1.m_data[i][j] - mat2.m_data[i][j];
		}
		return res;
}

Matrix& Matrix::operator+=(const Matrix &mat)
{
	if (m_row == 0 && m_col == 0)
	{
		m_row = mat.m_row;
		m_col = mat.m_col;		
	}
	if ((m_row != mat.m_row)||(m_col != mat.m_col) )//不同大小的矩阵+=是个错误
		terminate();
	
	int i, j;
	
	if (NULL == m_data)// 如果数据没有分配存储
	{
		m_data = new MATRIXTYPE*[m_row];		
		for (i = 0 ; i < m_row ;++i)
		{
			m_data[i] = new MATRIXTYPE[m_col];
		}
		for (i = 0 ; i < m_row ; ++i)
		{
			for (j = 0 ; j < m_col ; ++j)
			{
				m_data[i][j] = (MATRIXTYPE)0;
			}
		}
	}
	

	for(i = 0 ; i < m_row ; i++)
		for (j = 0 ; j < m_col;j++)
		{
			m_data[i][j] += mat.m_data[i][j]; 
		}
		return *this;		
}
Matrix& Matrix::operator-=(const Matrix &mat)
{
	if (m_row == 0 && m_col == 0)
	{
		m_row = mat.m_row;
	m_col = mat.m_col;
		
	}
	if ((m_row != mat.m_row)||(m_col != mat.m_col) )//不同大小的矩阵+=是个错误
		terminate();
	
	int i, j;
	
	if (NULL == m_data)// 如果数据没有分配存储
	{
		m_data = new MATRIXTYPE*[m_row];		
		for (i = 0 ; i < m_row ;++i)
		{
			m_data[i] = new MATRIXTYPE[m_col];
		}
		for (i = 0 ; i < m_row ; ++i)
		{
			for (j = 0 ; j < m_col ; ++j)
			{
				m_data[i][j] = (MATRIXTYPE)0;
			}
		}
	}


	for(i = 0 ; i < m_row ; i++)
		for (j = 0 ; j < m_col;j++)
		{
			m_data[i][j] -= mat.m_data[i][j]; 
		}
		return *this;		
}

⌨️ 快捷键说明

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