#include <iostream> using namespace std;
class Matrix { public: friend Matrix operator +(Matrix m1,Matrix m2); //定义重载函数,重载加法运算
friend istream & operator >> (istream &,Matrix &);//定义重载函数,重载流提取运算符
friend ostream & operator <<(ostream &,Matrix &); //定义重载函数,重载流插入运算符
private: int data[2][3] ; //定义矩阵使用的数组,私有成员
};
istream & operator >> (istream & input,Matrix &m) { int (*p)[3]; //定义指向一维数组的指针变量
int *q; //定义int 类型指针
for(p = m.data;p < m.data + 2;p++) { for (q = *p; q < *p + 3;q++) { input >> *q; } } }
ostream & operator << (ostream & output,Matrix &m) { int (*p)[3]; int *q; for(p = m.data;p < m.data + 2;p++) { for (q = *p; q < *p + 3;q++) { output << *q << " "; } output << endl; } }
Matrix operator +(Matrix m1,Matrix m2) { Matrix result; int (*p)[3]; int *q; int (*pm1)[3],(*pm2)[3]; int *pm11,*pm12; for(pm1 = m1.data,pm2 = m2.data,p = result.data;p < result.data + 2;p++,pm1++,pm2++) { for (pm11 = *pm1,pm12 = *pm2,q = *p; q < *p + 3;q++,pm11++,pm12++) { *q = *pm11 + *pm12; } } return result; }
int main() { Matrix m1,m2,m3; cin >> m1; cin >> m2; m3 = m1 + m2; cout << m3; system("pause"); return 0; }
|