//文件名:CMatrix.h
//功 能:矩阵类CMatrix的声明
//作 者:01计机(1)班 何海强 200124151109
#ifndef _CMATRIX_H_
#define _CMATRIX_H_
class CMatrix //矩阵类
{
public:
//构造函数、析构函数、拷贝构造函数、赋值运算符
CMatrix(int row, int col);
~CMatrix();
CMatrix(const CMatrix &rhs);
CMatrix& operator=(const CMatrix &rhs);
//填充矩阵
bool setMatrix(const double *array, int size);
//获取矩阵中的所有元素
bool getMatrix(double *array);
//获取矩阵的行数
void getRow(int &row);
//获取矩阵的列数
void getCol(int &col);
//显示矩阵
bool display(void);
//矩阵相加
CMatrix operator+(const CMatrix &rhs);
//矩阵相减
CMatrix operator-(const CMatrix &rhs);
//矩阵相乘
CMatrix operator*(const CMatrix &rhs);
//常数与矩阵相乘
CMatrix operator*(double constant);
private:
double *m_matrix; //指向矩阵的指针
int m_row; //矩阵的行数
int m_col; //矩阵的列数
};
#endif
//文件名:CMatrix.cpp
//功 能:矩阵类CMatrix的实现
//作 者:01计机(1)班 何海强 200124151109
#include <iostream>
#include "CMatrix.h"
using namespace std;
//一般构造函数
CMatrix::CMatrix(int row, int col)
:m_matrix(NULL), m_row(0), m_col(0)
{
int length = 0;
//判断行数与列数是否合乎规定
if ((row < 1) || (col < 1))
{
return;
}
length = row * col;
m_matrix = new double[length]; //为矩阵分配内存空间
if (NULL == m_matrix)
{
return;
}
else
{
m_row = row;
m_col = col;
//首先用0来填充矩阵里面的所有元素
for (int index = 0; index < length; ++index)
{
m_matrix[index] = 0;
}
}
}
//析构函数
CMatrix::~CMatrix()
{
delete [] m_matrix; //释放矩阵所占用的内存
}
//拷贝构造函数
CMatrix::CMatrix(const CMatrix &rhs)
:m_matrix(NULL), m_row(rhs.m_row), m_col(rhs.m_col)
{
int length = 0;
if (NULL == rhs.m_matrix) //矩阵rhs为空
{
return;
}
length = m_row * m_col;
m_matrix = new double[length]; //为矩阵分配内存空间
if (NULL == m_matrix)
{
m_row = 0;
m_col = 0;
return;
}
else
{
//用rhs矩阵里面的元素来填充本矩阵
for (int index = 0; index < length; ++index)
{
m_matrix[index] = rhs.m_matrix[index];
}
}
}
//赋值运算符
CMatrix& CMatrix::operator=(const CMatrix &rhs)
{
int length = 0;
//判断是否自赋值
if (this != &rhs)
{
//释放原矩阵所占用的内存
delete [] m_matrix;
m_matrix = NULL;
m_row = 0;
m_col = 0;
//矩阵rhs是否为空
if (rhs.m_matrix != NULL)
{
length = rhs.m_row * rhs.m_col;
m_matrix = new double[length]; //为矩阵分配内存空间
if (m_matrix != NULL)
{
m_row = rhs.m_row;
m_col = rhs.m_col;
//用rhs矩阵里面的元素来填充本矩阵
for (int index = 0; index < length; ++index)
{
m_matrix[index] = rhs.m_matrix[index];
}
}
} //if ((rhs.m_row > 0) && (rhs.m_col > 0))
}
return *this; //返回本对象的引用
}
//填充矩阵
bool CMatrix::setMatrix(const double *array, int size)
{
if ((NULL == m_matrix) || (NULL == array))
{
return false;
}
if (size != (m_row * m_col)) //长度不相符
{
return false;
}
else
{
//用数组array里面的值来填充本矩阵
for (int index = 0; index < size; ++index)
{
m_matrix[index] = array[index];
}
return true;
}
}
//获取矩阵中的所有元素
bool CMatrix::getMatrix(double *array)
{
int length = 0;
if ((NULL == m_matrix) || (NULL == array))
{
return false;
}
else
{
length = m_row * m_col;
//用数组array来返回本矩阵中所有元素值
for (int index = 0; index < length; ++index)
{
array[index] = m_matrix[index];
}
return true;
}
}
//获取矩阵的行数
void CMatrix::getRow(int &row)
{
row = m_row;
}
//获取矩阵的列数
void CMatrix::getCol(int &col)
{
col = m_col;
}
//显示矩阵
bool CMatrix::display(void)
{
if (NULL == m_matrix) //本矩阵为空
{
return false;
}
else
{
//按行输出矩阵
for (int row = 0; row < m_row; ++row)
{
for (int col = 0; col < m_col; ++col)
{
cout << m_matrix[row * m_col + col] << '/t'; //同一行中的矩阵元素之间用一个tab 隔开
}
cout << '/n'; //准备输出下一行
}
return true;
}
}
//矩阵相加
CMatrix CMatrix::operator+(const CMatrix &rhs)
{
int length = 0;
//判断两个矩阵的行数与列数是否分别相等
if ((m_row == rhs.m_row) && (m_col == rhs.m_col))
{
if (NULL == m_matrix)
{
return *this; //返回一个空矩阵
}
else
{
CMatrix tempMatrix(m_row, m_col);
length = m_row * m_col;
for (int index = 0; index < length; ++index)
{
tempMatrix.m_matrix[index] = m_matrix[index] + rhs.m_matrix[index]; //相加
}
return tempMatrix; //返回相加后所得的矩阵
} //if ((m_row < 1) || (m_col < 1))
}
else
{
//定义一个空矩阵并返回它
CMatrix tempMatrix(0, 0);
return tempMatrix;
}
}
//矩阵相减
CMatrix CMatrix::operator-(const CMatrix &rhs)
{
int length = 0;
//判断两个矩阵的行数与列数是否分别相等
if ((m_row == rhs.m_row) && (m_col == rhs.m_col))
{
if (NULL == m_matrix)
{
return *this; //返回一个空矩阵
}
else
{
CMatrix tempMatrix(m_row, m_col);
length = m_row * m_col;
for (int index = 0; index < length; ++index)
{
tempMatrix.m_matrix[index] = m_matrix[index] - rhs.m_matrix[index]; //相减
}
return tempMatrix; //返回相减后所得的矩阵
} //if ((m_row < 1) || (m_col < 1))
}
else
{
//定义一个空矩阵并返回它
CMatrix tempMatrix(0, 0);
return tempMatrix;
}
}
//矩阵相乘
CMatrix CMatrix::operator*(const CMatrix &rhs)
{
if (m_col == rhs.m_row) //第一个矩阵的列数与第二个矩阵的行数相等
{
if (NULL == m_matrix)
{
return *this; //返回一个空矩阵
}
else
{
CMatrix tempMatrix(m_row, rhs.m_col);
for (int row = 0; row < m_row; ++row) //行
{
for (int col = 0; col < rhs.m_col; ++col) //列
{
for (int index = 0; index < m_col; ++index)
{
tempMatrix.m_matrix[row*rhs.m_col+col] +=
(m_matrix[row*m_col+index] * rhs.m_matrix[index*rhs.m_col+col]);
}
}
}
return tempMatrix;
} //if (NULL == m_matrix)
}
else
{
//定义一个空矩阵并返回它
CMatrix tempMatrix(0, 0);
return tempMatrix;
}
}
//常数与矩阵相乘
CMatrix CMatrix::operator*(double constant)
{
int length = 0;
//本矩阵是否为空
if (NULL == m_matrix)
{
return *this; //返回一个空矩阵
}
else
{
CMatrix tempMatrix(m_row, m_col);
length = m_row * m_col;
for (int index = 0; index < length; ++index)
{
tempMatrix.m_matrix[index] = m_matrix[index] * constant; //矩阵元素与常数相乘
}
return tempMatrix; //返回相乘后所得的矩阵
}
}