018.矩阵的运算

      下面这段程序给出了矩阵的加法,减法,以及乘法的运算,并且使用了运算符重载等方式输出求解的答案,欲了解详细的过程请认真阅读代码。
#include<iostream>

using namespace std;

template<typename T>
class Matrix
{
private:
	//a[i][j]应存放在a[i*n+j-1]处
	int rows, cols;//矩阵的行数和列数

	T* element;//矩阵的元素

public:
	Matrix(int r, int c);//构造函数

	Matrix(const Matrix<T>& m);//复制构造函数

	~Matrix()
	{
		delete[]element;
	}

	int Rows() const//返回矩阵行数
	{
		return rows;
	}

	int Columns()const//返回矩阵列数
	{
		return cols;
	}

	T& operator()(int i, int j)const;//重载下标操作符()
	Matrix<T>& operator=(const Matrix<T>& m);//重载赋值运算符
	Matrix<T> operator+()const;//重载一元加法运算符
	Matrix<T> operator+(const Matrix<T>& m)const;//重载二元加法运算符
	Matrix<T> operator-()const;//重载一元减法运算符
	Matrix<T> operator-(const Matrix<T>& m)const;//重载二元减法运算符
	Matrix<T> operator*(const Matrix<T>& m)const;//重载二元乘法运算符


	void print()const
	{
		for (int i = 0; i < rows * cols; i++)
		{
			cout << element[i] << " ";
			if ((i + 1) % this->cols == 0)
			{
				cout << endl;
			}
		}
		cout << endl;

	}
};

template<typename T>
Matrix<T>::Matrix(int r, int c)
{
	if (c < 0 || r < 0)//考察矩阵下标是否合法
	{
		cout << "bad initializer" << endl;
		exit(1);
	}
	if (r == 1 && c == 1)//规定1*1的矩阵不合法
	{
		cout << "bad initializer" << endl;
		exit(1);
	}
	rows = r;
	cols = c;
	element = new T[r * c];//为矩阵申请空间
};

template<typename T>
Matrix<T>::Matrix(const Matrix<T>& m)
{
	rows = m.rows;
	cols = m.cols;
	element = new T[rows * cols];
	for (int i = 0; i < rows * cols; i++)
	{
		element[i] = m.element[i];
	}
}

//重载下标运算符
template<typename T>
T& Matrix<T>::operator()(int i, int j)const
{
	if (i<1 || i>rows || j<1 || j>cols)
	{
		cout << "Out of bounds." << endl;
		exit(1);
	}
	return element[(i - 1) * cols + j - 1];
}

template<typename T>
Matrix<T>& Matrix<T>::operator=(const Matrix<T>& m)
{
	rows = m.rows;
	cols = m.cols;
	delete[]element;
	element = new T[rows * cols];
	for (int i = 0; i < rows * cols; i++)
	{
		element[i] = m.element[i];
	}
	return *this;
};


//重载二元减法运算符
template<typename T>
Matrix<T> Matrix<T>::operator-(const Matrix<T>& m)const
{
	if (rows != m.rows || cols != m.cols)
	{
		cout << "Size not matched!" << endl;
		exit(1);
	}
	//创建一个临时矩阵,存放二维数组相减的结果
	Matrix<T>w(rows, cols);
	for (int i = 0; i < rows * cols; i++)
	{
		w.element[i] = element[i] - m.element[i];
	}
	return w;
};

//重载乘法运算符
template<typename T>
Matrix<T> Matrix<T>::operator*(const Matrix<T>& m)const
{
	if (cols != m.rows)
	{
		cout << "Size not matched" << endl;
		exit(1);
	}
	Matrix<T> w(rows, m.cols);//根据乘法的规则,创建一个适应大小是的临时矩阵


	int ct = 0, cm = 0, cw = 0;
	for (int i = 1; i <= rows; i++)
	{
		for (int j = 1; j <= m.cols; j++)
		{
			T sum = element[ct] * m.element[cm];
			for (int k = 2; k <= cols; k++)
			{
				ct++;
				cm += m.cols;
				sum += element[ct] * m.element[cm];
			}
			w.element[cw] = sum;
			cw++;
			ct -= cols - 1;
			cm = j;
		}
		ct += cols;
		cm = 0;
	}
	return w;
};

//重载二元加法运算符
template<typename T>
Matrix<T> Matrix<T>::operator+(const Matrix<T>& m)const
{
	if (rows != m.rows || cols != m.cols)
	{
		cout << "Size not matched!" << endl;
		exit(1);
	}
	//创建一个临时矩阵,存放二维数组相减的结果
	Matrix<T>w(rows, cols);
	for (int i = 0; i < rows * cols; i++)
	{
		w.element[i] = element[i] + m.element[i];
	}
	return w;
};

//重载一元加法运算符
template<typename T>
Matrix<T> Matrix<T>::operator+()const
{
	for (int i = 0; i < rows * cols; i++)
	{
		element[i] += 1;
	}
}

//重载一元减法运算符
template<typename T>
Matrix<T> Matrix<T>::operator-()const
{
	for (int i = 0; i < rows * cols; i++)
	{
		element[i] -= 1;
	}
}

int main()
{
	Matrix<int>obj1(2, 2), obj2(2, 2), obj3(2, 2);


	cout << "A矩阵:" << endl;
	obj1(1, 1) = 4;
	obj1(2, 1) = 3;
	obj1(1, 2) = 2;
	obj1(2, 2) = 1;
	obj1.print();


	cout << "B矩阵:" << endl;
	obj2(1, 1) = 1;
	obj2(2, 1) = 3;
	obj2(1, 2) = 2;
	obj2(2, 2) = 4;
	obj2.print();


	cout << "A+B矩阵:" << endl;
	obj3 = obj1 + obj2;
	obj3.print();

	cout << "A-B矩阵:" << endl;
	obj3 = obj1 - obj2;
	obj3.print();

	cout << "A*B矩阵:" << endl;
	obj3 = obj1 * obj2;
	obj3.print();

	system("pause");
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值