数据结构-矩阵

#include <iostream>
#include <stdlib.h>
using namespace std;
//矩阵类
template <typename T>
class matrix
{
	friend ostream& operator<<(ostream& out,const matrix<T>&);
public:
	matrix(int theRows = 0, int theColumns = 0);
	matrix(const matrix<T>&);
	~matrix(){ delete[] element; }
	int rows()const{ return theRows; }
	int columns()const{ return theColumns; }
	T& operator()(int i, int j)const;
	matrix<T>& operator=(const  matrix<T>&);
	matrix<T> operator+()const;
	matrix<T> operator+(const matrix<T>&)const;
	matrix<T> operator-()const;
	matrix<T> operator-(const matrix<T>&)const;
	matrix<T> operator*(const matrix<T>&)const;
	matrix<T>& operator+=(const T&);

private:
	int theRows;
	int theColumns;
	T *element;
};

template<class T>
matrix<T>::matrix(int theRows, int theColumns)
{
	if (theRows < 0 || theColumns < 0)
	{
		cout << "行列必须大于0" << endl;
		exit(0);
	}
		
	if ((theRows == 0 || theColumns == 0) && (theRows != 0 || theColumns != 0))
	{
		cout << "行和列不能同时为0" << endl;
		exit(0);
	}
	this->theRows = theRows;
	this->theColumns = theColumns;
	element = new T[theRows*theColumns];
}
template<class T>
matrix<T>::matrix(const matrix<T>& m)
{
	theRows = m.theRows;
	theColumns = m.theColumns;
	element = new T[theRows*theColumns];

	copy(m.element, m.element + theRows*theColumns, element);
}
template<class T>
matrix<T>& matrix<T>::operator=(const matrix<T>& m)
{
	//不能自己复制自己
	if (this != &m)
	{
		delete[] element;
		theRows = m.theRows;
		theColumns = m.theColumns;
		element = new T[theRows*theColumns];
		copy(m.element, m.element + theRows*theColumns,element);
	}
	return *this;
}
template<class T>
T& matrix<T>::operator()(int i,int j)const
{
	if (i<1 || i>theRows || j < 1 || j>theColumns)
	{
		cout << "索引无效" << endl;
		exit(0);
	}
	//cout << element[(i - 1)*theColumns + j - 1];
	return element[(i - 1)*theColumns + j - 1];//行主映射
}
template<class T>
matrix<T> matrix<T>::operator+(const matrix<T>& m)const
{
	//返回矩阵w=(*this)+m
	if(theRows != m.theRows || theColumns != m.theColumns)
	{
		cout << "维数不同,不能相加" << endl;
		exit(0);
	}
	matrix<T> w(theRows, theColumns);
	for (int i = 0; i < theRows*theColumns; i++)
	{
		w.element[i] = element[i] + m.element[i];
	}
	return w;
}

template<class T>

matrix<T> matrix<T>::operator*(const matrix<T>& m)const
{
	if (theColumns != m.theRows)
	{
		cout << "无法相乘" << endl;
		exit(0);
	}
	matrix<T> w(theRows, m.theColumns);
	int ct = 0, cm = 0, cw = 0;
	for (int i = 1; i <= theRows; i++)
	{
		for (int j = 1; j <= m.theColumns;j++)
		{
			T sum = element[ct] * m.element[cm];
			for (int k = 2; k <= theColumns; k++)
			{
				ct++;
				cm += m.theColumns;
				sum += element[ct] * m.element[cm];
			}
			w.element[cw++] = sum;

			ct -= theColumns - 1;
			cm = j;
		}
		ct += theColumns;
		cm = 0; 
	}
	return w;
}



int main()
{
	matrix <int> m(1, 3);
	matrix<int> n(1, 3);
	matrix<int> L(1, 3);
	matrix<int> x(1, 1);
	matrix<int> y(1, 1);
	matrix<int> LL(1, 1);
	m(1, 1) = 1;
	m(1, 2) = 2;
	m(1, 3) = 3;
	n(1, 1) = 1;
	n(1, 2) = 2;
	n(1, 3) = 3;
	x(1, 1) = 1;
	y(1, 1) = 1;

	L = m + n;
	//cout << L;
	LL = x *y;
	cout << LL(1, 1)<<endl;
	return 0;
}

点击链接加入群获取更多精彩


猜你喜欢

转载自blog.csdn.net/ydpawx/article/details/78167326