【C++练习题】求任意行列的两个矩阵的乘积(普通函数实现+类实现)

一、题目

学弟问了一道题,想想好久没有写C++的题了,想着练练手,也把这道题分享给大家。

我们知道用matlab实现这个代码是很容易的,毕竟玩矩阵,matlab是专业的,C++就不一样了,考虑的问题有好多,实现方法也有很多,我就把自己写的分享给大家。

我们用一个两行两列的矩阵A和两行三列的矩阵B做测试:

二、普通函数实现

1、代码


#include<iostream>
using namespace std;

int rowsA;
int colsA;
int rowsB;
int colsB;
int ** matrixA = new int *[rowsA];
int ** matrixB = new int *[rowsB];
int ** matrixAB = new int *[rowsA];

void ProductOfMatrix(int ** matrixA, int ** matrixB) {
	for (int i = 0; i < rowsA; i++)
	{
		matrixAB[i] = new int[colsB];
		for (int j = 0; j < colsB; j++)
		{
			matrixAB[i][j] = 0;
			for (int m = 0; m < colsA; m++)
			{
				matrixAB[i][j] += matrixA[i][m] * matrixB[m][j];
			}
		}
	}
}

void OutputMatrix(int ** matrix,int rows,int cols) {
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < cols; j++)
			cout << matrix[i][j] << "\t";
		cout << endl;
	}
	cout << endl;
}

void main() {
	cout << "输入矩阵A的行:";
	cin >> rowsA;
	cout << "输入矩阵A的列:";
	cin >> colsA;
	rowsB = colsA;
	cout << "输入矩阵B的列:";
	cin >> colsB;

	for (int i = 0; i < rowsA; i++)
	{
		matrixA[i] = new int[colsA];
		cout << "\n输入矩阵A的第"<<i+1<<"行数据(该行共有" << colsA << "个数据) : \n";
		for (int j = 0; j < colsA; j++)
		{
			cin >> matrixA[i][j];
		}
	}

	for (int i = 0; i < rowsB; i++)
	{
		matrixB[i] = new int[colsB];
		cout << "\n输入矩阵B的第"<<i+1<<"行数据(该行共有" << colsB << "个数据) : \n";
		for (int j = 0; j < colsB; j++)
		{
			cin >> matrixB[i][j];
		}
	}

	ProductOfMatrix(matrixA, matrixB);
	cout << "【矩阵A】\n";
	OutputMatrix(matrixA, rowsA, colsA);
	cout << "【矩阵B】\n";
	OutputMatrix(matrixB, rowsB, colsB);
	cout << "【A × B】\n";
	OutputMatrix(matrixAB, rowsA, colsB);
}

2、运行结果

三、类实现

1、代码


#include<iostream>
using namespace std;

class Matrix
{
public:
	Matrix(int rows,int cols);
	~Matrix();
	void ProductOfMatrix(Matrix MA, Matrix MB);
	void Assignment();
	void OutputMatrix();
private:
	int rows;
	int cols;
	int **data;
};

Matrix::Matrix(int rows, int cols)
{
	this->rows = rows;
	this->cols = cols;
	this->data = new int *[rows];
	for (int i = 0; i < rows; i++)
	{
		this->data[i] = new int[cols];
	}
}

Matrix::~Matrix()
{
}

void Matrix::Assignment() {
	for (int i = 0; i < this->rows; i++)
	{
		cout << "\n输入矩阵的第" << i + 1 << "行数据(该行共有" << this->cols << "个数据) : \n";
		for (int j = 0; j < this->cols; j++)
		{
			cin >> this->data[i][j];
		}
	}
}

void Matrix::ProductOfMatrix(Matrix MA, Matrix MB) {
	for (int i = 0; i < MA.rows; i++)
	{
		for (int j = 0; j < MB.cols; j++)
		{
			this->data[i][j] = 0;
			for (int m = 0; m < MA.cols; m++)
			{
				this->data[i][j] += MA.data[i][m] * MB.data[m][j];
			}
		}
	}
}

void Matrix::OutputMatrix() {
	for (int i = 0; i < this->rows; i++)
	{
		for (int j = 0; j < this->cols; j++)
			cout << this->data[i][j] << "\t";
		cout << endl;
	}
	cout << endl;
}

void main() {
	int rowsA;
	int colsA;
	int rowsB;
	int colsB;
	cout << "输入矩阵A的行:";
	cin >> rowsA;
	cout << "输入矩阵A的列:";
	cin >> colsA;
	rowsB = colsA;
	cout << "输入矩阵B的列:";
	cin >> colsB;
	Matrix MA(rowsA, colsA);
	Matrix MB(rowsB, colsB);
	Matrix MAB(rowsA, colsB);
	cout << "\n【矩阵A赋值】";
	MA.Assignment();
	cout << "\n【矩阵B赋值】";
	MB.Assignment();
	MAB.ProductOfMatrix(MA, MB);
	cout << "\n【矩阵A】\n";
	MA.OutputMatrix();
	cout << "\n【矩阵B】\n";
	MB.OutputMatrix();
	cout << "\n【A × B】\n";
	MAB.OutputMatrix();
}

2、运行结果

大家还可以使用结构体,还可以加上数据结构中的矩阵,总之方法很多,希望大家能多多思考。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值