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