#include <iomanip>
#include <string.h>
using namespace std;
//m*n 階矩陣
class M
{
public:
M(int m, int n);
M(M& m);
virtual ~M();
int m;
int n;
private:
int *a;
friend istream& operator>>( istream& in, M& m );
friend ostream& operator<<( ostream& out, M& m );
friend M& operator+(M& a, M& b);
};
M::M(int m=1, int n=1) : m(m), n(n)
{
a = new int[n*m];
}
//拷貝構造
M::M(M& m) : m(m.m), n(m.n)
{
a = new int[m.n*m.m];
memcpy(a, m.a, m.n*m.m*sizeof(int));
}
M::~M()
{
delete a;
}
//輸入矩陣
istream& operator>>( istream& in, M& m )
{
cout<<"input "<<m.m<<"*"<<m.n<<" matrix"<<endl;
for( int i=0; i<m.m; i++ )
{
cout<<"input line "<<i+1<<":";//輸入第i+1 行
for( int j=0; j<m.n; j++ )
{
in >> (*(m.a+i*m.n+j));
}
}
return in;
}
//輸出矩陣
ostream& operator<<( ostream& out, M& m )
{
for( int i=0; i<m.m; i++ )
{
for( int j=0; j<m.n; j++ )
{
out<<setw(5)<<(*(m.a+i*m.n+j));
}
out<<endl;
}
return out;
}
//矩陣加法
M& operator+(M& a, M& b)
{
if( !( a.m==b.m && a.n==b.n ) )
{
cout<<"Error: can not +"<<endl;
}
M m(a);
for( int i=0; i<m.m; i++ )
{
for( int j=0; j<m.n; j++ )
{
*(m.a+i*m.n+j) += *(b.a+i*m.n+j);
}
}
M* x = new M(m);
return *x;
}
int main(int argc, char *argv[])
{
M a(2, 3), b(2, 3);
cin>>a>>b;
M c = a + b;
cout<<a<<"--------"<<endl<<b<<"--------"<<endl<<c<<"--------"<<endl;
return 0;
}