2640: 编程题:运算符重载---矩阵求和

时间限制: 1 Sec  内存限制: 128 MB

提交: 484  解决: 190

题目描述

/*
有两个矩阵a和b,均为2行3列。求两个矩阵之和。
重载运算符“+”,使之能用于矩阵相加(如c=a+b)。
重载流插入运算符“<<”和流提取运算符“>>”,使之能用于该矩阵的输入和输出。
请在下面的程序段基础上完成设计,只提交begin到end部分的代码 */
#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,j;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
mat[i][j]=0; 
}
istream & operator>>(istream &  input,Matrix & m)
{
    int i,j;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
input>>m.mat[i][j];
return input;
}
//将程序需要的其他成份写在下面,只提交begin到end部分的代码
//******************** begin ********************
//********************* end ********************
int main()
{
    Matrix a,b,c;
    cin>>a;
    cin>>b;
    c=a+b;
    cout<<c<<endl;
    return 0;
}

输入

两个2行3列矩阵

输出

矩阵之和

样例输入

1 2 3
4 5 6 7 8 9
0 1 2

样例输出

8 10 12
4 6 8

提示

只提交begin到end部分的代码

迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……

#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,j;
for(i=0; i<2; i++)
for(j=0; j<3; j++)
mat[i][j]=0;
}
istream & operator>>(istream & input,Matrix & m)
{
int i,j;
for(i=0; i<2; i++)
for(j=0; j<3; j++)
input>>m.mat[i][j];
return input;
}
Matrix operator+(Matrix &m1,Matrix &m2)
{
Matrix m;
for(int i=0; i<2; ++i)
for(int j=0; j<3; ++j)
{
m.mat[i][j]=m1.mat[i][j]+m2.mat[i][j];
}
return m;
}
ostream& operator <<(ostream &output,Matrix &m)
{
int i,j;
for(i=0; i<2; ++i)
{
for( j=0; j<3; ++j)
{
output<<m.mat[i][j]<<" ";
}
output<<endl;
}
return output;
}
int main()
{
Matrix a,b,c;
cin>>a;
cin>>b;
c=a+b;
cout<<c<<endl;
return 0;
}

最新文章

  1. iOS之转场动画
  2. python 日期计算案例
  3. [DFNews] Fire-Eye与Fox IT联合推出Cryptolocker解锁网站
  4. Winform 窗体单例
  5. iOS之下拉放大,上推缩小,一个方法搞定
  6. Swift3.0语言教程获取字符串长度
  7. pouchdb sync
  8. mac下SVN上传.a静态库文件
  9. 179. Largest Number
  10. codeforces Codeforces Round #345 (Div. 1) C. Table Compression 排序+并查集
  11. keybd_event函数用法
  12. Android新浪微博客户端(五)——主界面的TabHost和WeiboUtil
  13. MySQL所有函数及操作符
  14. Python网络编程学习_Day11
  15. plus调用android原生页面
  16. Plctext 如何发送默认的模式
  17. 使用node.js &amp; live server在移动端测试网站
  18. spring cloud: zuul(五): prefix访问前缀, ignoredServices粗粒度访问, yml不起作用
  19. 31 Godoc: documenting Go code 编写良好的文档关于godoc
  20. Maven中&lt;dependencies&gt;节点和&lt;dependencyManagement&gt;节点的区别

热门文章

  1. assert.ifError()函数详解
  2. poj 1088 滑雪 DP(dfs的记忆化搜索)
  3. [java基础原理] 数字类型原理
  4. web前端开发——HTML
  5. cadence中元件所在库
  6. es6(var,let,const,set,map,Array.from())
  7. Web框架django进阶篇
  8. [luoguP1077] 摆花(DP)
  9. POJ 1988相对偏移
  10. POJ 1741 Tree【Tree,点分治】