编程题:运算符重载---矩阵求和
时间: 1ms 内存:128M
描述:
/*有两个矩阵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
提示:
参考答案:
解锁文章
文章评论