第十五周oj刷题——Problem M: C++习题 矩阵求和--重载运算符
2024-08-28 07:30:45
Description
有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加(如c=a+b)。
重载流插入运算符“<<”和流提取运算符“>>”。使之能用于该矩阵的输入和输出。
Input
两个2行3列矩阵
Output
矩阵之和
Sample Input
1 2 3
4 5 6 7 8 9
1 2 3
Sample Output
8 10 12
5 7 9
/* All rights reserved.
* 文件名:test.cpp
* 作者:陈丹妮
* 完毕日期:2015年 6 月 21 日
* 版 本 号:v1.0
*/
#include <iostream>
using namespace std;
class Matrix
{
public:
Matrix();
friend Matrix operator+(Matrix &,Matrix &);
friend ostream& operator<<(ostream&,Matrix&);
friend istream& operator>>(istream&,Matrix&);
private:
int mat[2][3];
};
Matrix::Matrix()
{int i=0,j=0;
for(;i<2;i++)
for(;j<3;j++)
mat[i][j]=0;
} Matrix operator+(Matrix &m1,Matrix &m2)
{
Matrix m;
int i,j;
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
m.mat[i][j]=m1.mat[i][j]+m2.mat[i][j];
}
return m;
}
ostream& operator<<(ostream&out,Matrix&m)
{
int i,j;
for(i=0; i<2; i++)
{for(j=0; j<3; j++)
out<<m.mat[i][j]<<" ";
cout<<endl;}
return out;
}
istream& operator>>(istream&in,Matrix&m)
{
int i,j;
for(i=0; i<2; i++)
for(j=0; j<3; j++)
in>>m.mat[i][j];
return in;
}
int main()
{
Matrix a,b,c;
cin>>a;
cin>>b;
c=a+b;
cout<<c<<endl;
return 0;
}
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbnVmYW5nZG9uZ2Rl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
学习心得:继续加油,重载非常好用的,得要总结一下了!
!!!
最新文章
- Meta标签中的viewport属性及含义
- jquery mobile 登陆后页面验证
- Oracle死锁处理
- 制作linux内核安装包
- [原创]自定义控件之AndroidSegmentControlView,仿IOS平台UISegmentControlView,继承自View
- ArcMap自定义脚本工具制作
- (转)9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路
- CentOS7配置Nodejs环境安装记录
- 一分钟告诉你究竟DevOps是什么鬼?
- 在vue 里使用腾讯ditu
- 【LOJ#3095】[SNOI2019]字符串(后缀数组)
- c博客作业--分支、顺序结构
- Coprime Arrays CodeForces - 915G (数论水题)
- adb command
- sscanf 解析字符串
- HDU3693 Math Teacher&#39;s Homework ---- 数位DP
- Delphi XE开发 Android 开机自动启动
- Java Web应用开发工具
- 【BZOJ 2946】 2946: [Poi2000]公共串 (SAM)
- android.app.FragmentManager 与 android.support.v4.app.FragmentManager带来的若干Error