#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_;
}
答案
最新推荐文章于 2022-12-21 16:41:09 发布