C++矩阵的基本运算方法

继续学习数据结构。

矩阵的基本操作
使用动态数组存储数据。

// matrix.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include 

using namespace std;

template
class matrix
{
public:
	matrix(int theRows = 0, int theCols = 0);
	matrix(const matrix&);
	~matrix() { delete[]element; };
	int getRows() const { return theRows; }
	int getCols() const { return theCols; }

	T& operator()(int i, int j) const;
	matrix operator+(const matrix&) const;
	matrix operator-(const matrix&) const;
	matrix operator*(const matrix&) const;
	matrix operator=(const matrix&);

	void init(T m[]) const;
	void print() const;
	void initFlag(int flag);
private:
	int theRows, theCols;
	int flag;
	T *element;
};

template
matrix::matrix(int theRows, int theCols)
{
	if ((theRows < 0) || (theCols < 0))
		cout << "rows and cols must be >= 0" << endl;
	if ((theRows == 0 || theCols == 0) && (theRows != 0 || theCols != 0))
		cout << "both rows and cols should be 0" << endl;
	this->theRows = theRows;
	this->theCols = theCols;
	element = new T[theRows * theCols]();
}

template
matrix::matrix(const matrix& m)
{
	theRows = m.theRows;
	theCols = m.theCols;
	flag = m.flag;
	element = new T[theRows * theCols];
	copy(m.element, m.element + theRows * theCols, element);
}

template
T & matrix::operator()(int i, int j) const
{
	if (i < 0 || (i > theRows - 1) || j < 0 || (j > theCols - 1))
		cout << "index out of bounds" << endl;
	return element[i * theCols + j];
}

template
matrix matrix::operator+(const matrix& m) const
{
	if (theRows != m.theRows || theCols != m.theCols) {
		cout << "can not plus" << endl;
		return NULL;
	}

	matrix w(theRows, theCols);
	for (int i = 0; i < theRows * theCols; i++)
		w.element[i] = element[i] + m.element[i];
	w.flag = flag + m.flag;
	return w;
}

template
matrix matrix::operator-(const matrix& m) const
{	
	if (theRows != m.theRows || theCols != m.theCols) {
		cout << "can not plus" << endl;
		return NULL;
	}
	matrix w(theRows, theCols);
	for (int i = 0; i < theRows * theCols; i++)
		w.element[i] = element[i] - m.element[i];
	return w;
}

template
matrix matrix::operator*(const matrix& m) const
{
	if (theCols != m.theRows) {
		cout << "cols must be = rows" << endl;
		return NULL;
	}

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

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

template
matrix matrix::operator=(const matrix& m)
{
	if (this != &m) {
		delete[] element;
		theRows = m.theRows;
		theCols = m.theCols;
		element = new T[theRows * theCols];
		copy(m.element, m.element + theRows * theCols, element);
	}
	return *this;
}

template
void matrix::init(T m[]) const
{
	copy(m, m + 6, element);
}

template
void matrix::print() const
{
	for (int i = 0; i < theRows; i++) {
		for (int j = 0; j < theCols; j++) {
			cout << element[i * theCols + j] << ",";
		}
		cout << endl;
	}
}

template
void matrix::initFlag(int flag)
{
	this->flag = flag;
}

int main()
{
	int matrix_A[6] = {
		1, 2, 3,
		4, 5, 6
	};

	int matrix_B[6] = {
		1, 4, 
		2, 5, 
		3, 6
	};

	matrix *m1 = new matrix(2, 3);
	m1->init(matrix_A);
	m1->print();
	cout << endl;

	matrix *m2 = new matrix(3, 2);
	m2->init(matrix_B);
	m2->print();
	cout << endl;

	matrix r;
	r = *m1 + *m2;
	r.print();

	cout << endl;

	r = *m1 * (*m2);
	r.print();

	system("pause");
	return 0;
}

运行结果如下:
C++矩阵的基本运算方法_第1张图片

你可能感兴趣的:(C++版数据结构)