答案

#include <iostream>

#include "Matrix.h"

using namespace std;

Matrix::Matrix()
	: dimension_(0),
	  matrix_(nullptr)
{
}

Matrix::Matrix(unsigned int dimension, unsigned int val)
	: dimension_(dimension),
	  matrix_(new unsigned int* [dimension])
{
	for (unsigned int i = 0 ; i < dimension_; ++i) {
		matrix_[i] = new unsigned int [dimension_];
		for (unsigned int j = 0; j < dimension_; ++j)
			matrix_[i][j] = val;
	}
}

Matrix::Matrix(unsigned int dimension, unsigned int** data)
	: dimension_(dimension),
	  matrix_(new unsigned int* [dimension])
{
	for (unsigned int i = 0 ; i < dimension_; ++i) {
		matrix_[i] = new unsigned int [dimension_];
		for (unsigned int j = 0; j < dimension_; ++j)
			matrix_[i][j] = data[i][j];
	}	
}

Matrix::Matrix(const Matrix& other)
	: dimension_(other.dimension_),
	  matrix_(new unsigned int* [other.dimension_])
{
	for (unsigned int i = 0 ; i < dimension_; ++i) {
		matrix_[i] = new unsigned int [dimension_];
		for (unsigned int j = 0; j < dimension_; ++j)
			matrix_[i][j] = other.matrix_[i][j];
	}
}

Matrix::Matrix(Matrix&& other)
	: dimension_(other.dimension_),
	  matrix_(other.matrix_)
{
	other.dimension_ = 0;
	other.matrix_ = nullptr;
}

Matrix::~Matrix() {
	if (matrix_) {
		for (unsigned int i = 0 ; i < dimension_; ++i)
			delete [] matrix_[i];
		delete [] matrix_;
	}
}

Matrix& Matrix::operator=(const Matrix& other) {
	if (this != &other) {
		if (matrix_ && dimension_ != other.dimension_) {
			/* free self first */
			for (unsigned int i = 0 ; i < dimension_; ++i)
				delete [] matrix_[i];
			delete [] matrix_;
			/* realloc memory */
			dimension_ = other.dimension_;
			matrix_ = new unsigned int* [dimension_];
			for (unsigned int i = 0 ; i < dimension_; ++i)
				matrix_[i] = new unsigned int [dimension_];
		}
		
		/* copy data from other */
		for (unsigned int i = 0 ; i < dimension_; ++i)
			for (unsigned int j = 0; j < dimension_; ++j)
				matrix_[i][j] = other.matrix_[i][j];
	}
	
	return *this;
}

Matrix& Matrix::operator=(Matrix && other) {
	if (this != &other) {
		if (matrix_) {
			/* free self first */
			for (unsigned int i = 0 ; i < dimension_; ++i)
				delete [] matrix_[i];
			delete [] matrix_;
		}
		
		/* streal data from other */
		dimension_ = other.dimension_;
		matrix_ = other.matrix_;
		other.dimension_ = 0;
		other.matrix_ = nullptr;
	}
	
	return *this;
}

bool Matrix::valid() const {
	return (dimension_ != 0);
}

unsigned int Matrix::operator()(unsigned int x,unsigned int y) const {
	return matrix_[x][y];
}

unsigned int& Matrix::operator()(unsigned int x, unsigned int y) {
	return matrix_[x][y];
}

Matrix::operator bool() const {
	for (unsigned int i = 0 ; i < dimension_; ++i)
		for (unsigned int j = 0; j < dimension_; ++j)
			if (matrix_[i][j] != 0)
				return true;
				
	return false;
}

bool Matrix::operator==(const Matrix& other) const {
	for (unsigned int i = 0 ; i < dimension_; ++i)
		for (unsigned int j = 0; j < dimension_; ++j)
			if (matrix_[i][j] != other.matrix_[i][j])
				return false;
				
	return true;
}

Matrix Matrix::operator+(const Matrix& other) const {
	Matrix temp(*this);
	
	if (dimension_ == other.dimension_) {
		for (unsigned int i = 0 ; i < dimension_; ++i)
			for (unsigned int j = 0; j < dimension_; ++j)
				temp.matrix_[i][j] += other.matrix_[i][j];
	}
	
	return temp;
}

Matrix Matrix::operator*(unsigned int scalar) const {
	for (unsigned int i = 0 ; i < dimension_; ++i)
		for (unsigned int j = 0; j < dimension_; ++j)
			matrix_[i][j] *= scalar;
			
	return *this;
}

Matrix Matrix::operator*(const Matrix& other) const {
	Matrix temp(*this);
	
	if (dimension_ == other.dimension_) {
		for (unsigned int i = 0 ; i < dimension_; ++i)
			for (unsigned int j = 0; j < dimension_; ++j) {
					temp.matrix_[i][j] = 0;
			}
	}
	
	return temp;
}

std::ostream& operator<<(std::ostream& out, const Matrix& m) {
	for (unsigned int i = 0; i < m.dimension_; ++i) {
		for (unsigned int j = 0; j < m.dimension_; ++j)
			cout << (j > 0 ? " ": "") << m.matrix_[i][j];
		cout << endl;
	}
	return out;
}

// Do not change these two functions
void Matrix::set(unsigned int dimension, unsigned int** matrix) {
	// DO NOT TOUCH
	dimension_ = dimension;
	matrix_ = matrix;
}

unsigned int** Matrix::get(unsigned int& dimension) const {
	// DO NOT TOUCH
	dimension = dimension_;
	return matrix_;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值