第3关:矩阵运算

本文介绍了如何在C++中创建一个Matrix类,包括构造函数、成员变量、赋值函数、获取元素和打印功能。同时,重载了加法、减法和乘法运算符,通过示例展示了如何进行矩阵运算并提供测试用例。
摘要由CSDN通过智能技术生成
任务描述

本关任务:设计一个矩阵类,并实现简单的矩阵运算。

相关知识

完成本关需要具备的知识介绍请参见第一、二关卡。

矩阵运算参考资料 

编程要求

在右侧编辑器中的Begin-End之间补充代码,设计一个矩阵类( Matrix ),并实现矩阵的简单运算,具体要求如下:

  1. 成员变量:这一部分学员可以自由发挥,但要求都是私有成员。

  2. 成员函数:

    • 构造函数:Matrix(int r,int c),参数 r 和 c 分别代表矩阵的行和列。

    • 全部设值函数:void Fill(int value),函数将矩阵内所有的元素都设置为参数 value 的值。

    • 指定位置设值函数:void Set(int r,int c,int value),函数将矩阵第 r 行 c 列的元素设置为 value 的值。

    • 获取元素函数:int Get(int r,int c)函数,函数返回矩阵第 r 行 c 列的元素。

    • 打印函数:void Print(),函数按照矩阵的形状打印出矩阵内容,每一个值后跟着一个空格。比如一个2x4元素全为1的矩阵,打印结果为(更明显表示格式,空格均用下划线_代替):

       
          
      1. 1_1_1_1_
      2. 1_1_1_1_
  3. 普通函数:

    • Matrix operator+(Matrix &m1,Matrix &m2)函数,重载Matrix类的加法运算符,实现矩阵的加法运算。

    • Matrix operator-(Matrix &m1,Matrix &m2)函数,重载Matrix类的减法运算符,实现矩阵的减法运算。

    • Matrix operator*(Matrix &m1,Matrix &m2)函数,重载Matrix类的乘法运算符,实现矩阵的乘法运算。

测试说明

平台会对你编写的代码进行测试,比对你输出的数值与实际正确数值,只有所有数据全部计算正确才能通过测试:

注意:对于输入数据的具体测试,请查看右侧 step3/run.cpp 文件

测试输入:1 1 预期输出:

 
  1. m1 + m2 :
  2. 3
  3. m1 - m2 :
  4. -1
  5. m1 * m3 :
  6. 1

测试输入:2 2 预期输出:

 
  1. m1 + m2 :
  2. 3 3
  3. 3 3
  4. m1 - m2 :
  5. -1 -1
  6. -1 -1
  7. m1 * m3 :
  8. 1 2
  9. 1 2

#include <iostream>
#include <string>
using namespace std;

/********* Begin *********/
class Matrix
{
	//矩阵类的声明
public:
    Matrix(int r,int c);
	Matrix();
    void Fill(int value);
    void Set(int r,int c,int value);
    int Get(int r,int c);
    void Print();

	friend Matrix operator + (Matrix& m1,Matrix &m2);    
    friend Matrix operator - (Matrix& m1,Matrix &m2);   
    friend Matrix operator * (Matrix& m1,Matrix &m2); 

private:
    int row, column;     
	int pt[10][10]; 
    
};

Matrix::Matrix()
{

}
Matrix::Matrix(int r,int c)
{
    row = r;
    column = c;
}

void Matrix::Print(){     
	for(int i = 0; i < row; i++)
	{
		for (int j = 0; j < column; j++)
			cout << this->pt[i][j] << " ";
		cout << endl;
	}
}
void Matrix::Fill(int value)
{
    for (int i = 0; i < row; i++)
    {
		for (int j = 0; j < column; j++)
			this->pt[i][j] =value;
    }
}
void Matrix::Set(int r,int c,int value)
{
    this->pt[r][c] =value;
}
int Matrix::Get(int r,int c)
{
    return this->pt[r-1][c-1];
}


Matrix operator+(Matrix& m1,Matrix &m2)
{
   
    Matrix temp(m1.row, m1.column);
	for (int i = 0; i < m1.row; i++){
		for (int j = 0; j < m1.column; j++)
			temp.pt[i][j]  = m1.pt[i][j] + m2.pt[i][j];
	}
	return temp;
   
    
}

Matrix operator-(Matrix& m1,Matrix &m2)
{
   
    Matrix temp(m1.row, m1.column);
	for (int i = 0; i < m1.row; i++){
		for (int j = 0; j < m1.column; j++)
			temp.pt[i][j] = m1.pt[i][j] - m2.pt[i][j];
	}
	return temp;
    
    
}

Matrix operator*(Matrix& m1,Matrix &m2)
{
 
    Matrix temp(m1.row, m2.column);
	for (int i = 0; i < m1.row; i++) {
			for (int j = 0; j < m2.column; j++) {
				long sum=0;
				for (int k = 0; k < m1.column; k++) {
					sum += m1.pt[i][k] * m2.pt[k][j];    
				}
				temp.pt[i][j] = sum;   
			}
		}
		return temp;
    
}
/********* End *********/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵌入式Dora

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值