本文实现的函数列表
本文先将简单的函数进行实现,因为这些函数相对来说较为简单,故不做描述,本文所列函数以及其重载如下:
- 默认构造函数
- 拷贝构造函数
- 接受两个整型变量(行,列)的构造函数
- 接受两个整型变量(行,列)和一个双精度浮点型变量(值)的构造函数
- 得到X行Y列的值
- 设置X行Y列的值
- 获取行数
- 获取列数
- 某一行乘以某一个数
- 某一列乘以某一个数
- 交换某两行
- 交换某两列
- 将矩阵变为零矩阵
- 析构函数
默认构造函数
本文的默认构造函数,默认构造出一行一列的矩阵
Matrix::Matrix() {
rowCount = 1, columnCount = 1;
r = new double*[1];
for (int i = 0; i < rowCount; i++)
{
r[i] = new double[columnCount];
}
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < columnCount; j++)
{
r[i][j] = 0;
}
}
}
拷贝构造函数
Matrix::Matrix(Matrix& m) {
rowCount = m.GetRowCount();
columnCount = m.GetColumnCount();
r = new double*[rowCount];
for (int i = 0; i < rowCount; i++)
{
r[i] = new double[columnCount];
}
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < columnCount; j++)
{
r[i][j] = m.GetItem(i, j);
}
}
}
接受两个整型变量(行,列)的构造函数
Matrix::Matrix(int row, int column) {
if (row < 0 || column < 0) { row = column = 1; }
rowCount = row, columnCount = column;
r = new double*[rowCount];
for (int i = 0; i < rowCount; i++)
{
r[i] = new double[columnCount];
}
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < columnCount; j++)
{
r[i][j] = 0;
}
}
}
接受两个整型变量(行,列)和一个双精度浮点型变量(值)的构造函数
Matrix::Matrix(int row, int column, double value) {
if (row < 0 || column < 0) { row = column = 1; }
rowCount = row, columnCount = column;
r = new double*[rowCount];
for (int i = 0; i < rowCount; i++)
{
r[i] = new double[columnCount];
}
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < columnCount; j++)
{
r[i][j] =value;
}
}
}
得到X行Y列的值
double Matrix::GetItem(int row, int column) {
if (row >= rowCount || column >= columnCount) return -1;
if (row < 0 || column < 0) return -1;
return r[row][column];
}
设置X行Y列的值
bool Matrix::SetItem(int row, int column, double value) {
if (row >= rowCount || column >= columnCount) return false;
if (row < 0 || column < 0) return false;
r[row][column] = value;
return true;
}
获取行数与获取列数
int Matrix::GetRowCount() { return rowCount; }
int Matrix::GetColumnCount() { return columnCount; }
某一行乘以某一个数
void Matrix::RowMultiply(int row, double time) {
if (row < 0 || row >= rowCount) return;
for (int i = 0; i < columnCount; i++)
{
r[row][i] *= time;
}
}
某一列乘以某一个数
void Matrix::ColumnMultiply(int column, double time) {
if (column < 0 || column >= columnCount) return;
for (int i = 0; i < rowCount; i++)
{
r[i][column] *= time;
}
}
交换某两行
void Matrix::SwapRows(int row1, int row2) {
if (row1 < 0 || row2 < 0) return;
if (row2 >= rowCount || row1 >= rowCount) return;
for (int i = 0; i < columnCount; i++)
{
double temp = r[row1][i];
r[row1][i] = r[row2][i];
r[row2][i] = temp;
}
}
交换某两列
void Matrix::SwapColumns(int column1, int column2) {
if (column1 < 0 || column2 < 0) return;
if (column1 >= columnCount || column2 >= columnCount) return;
for (int i = 0; i < rowCount; i++)
{
double temp = r[i][column1];
r[i][column1] = r[i][column2];
r[i][column2] = temp;
}
}
将矩阵变为零矩阵
void Matrix::SetToZeroMatrix() {
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < columnCount; j++)
{
r[i][j] = 0;
}
}
}
析构函数
Matrix::~Matrix() {
for (int i = 0; i < rowCount; i++)
{
delete[] r[i];
}
delete[] r;
}
作者的源码
https://github.com/PureZhao/BaseMathLib
项目里面找两个文件:
Matrix.h
Matrix.cpp
版权声明:本文为weixin_40885067原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。