Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- // ---------- 提交區塊 ----------
- class Matrix
- {
- public:
- int matrix[3][3];
- Matrix(int m[3][3])
- {
- for(int i=0;i<3;i++)// 印出矩陣
- {
- for(int j=0;j<3;j++)
- {
- matrix[i][j]=m[i][j];
- }
- };
- // 建構子,設定matrix內的值
- }
- void show()
- {
- for(int i=0;i<3;i++)// 印出矩陣
- {
- for(int j=0;j<3;j++)
- {
- cout<< matrix[i][j]<<" ";
- }
- cout<< endl;
- };
- }
- void operator+(Matrix &m)
- {
- // 定義運算子+
- for(int i=0;i<3;i++) // 印出矩陣
- {
- for(int j=0;j<3;j++)
- {
- cout << matrix[i][j]+m.matrix[i][j]<<" ";
- }
- cout<< endl;
- };
- // 矩陣相加
- // 直接將結果在此cout 出來
- }
- void operator-(const Matrix &m)
- {
- // 定義運算子-
- for(int i=0;i<3;i++) // 印出矩陣
- {
- for(int j=0;j<3;j++)
- {
- cout << matrix[i][j]-m.matrix[i][j]<<" ";
- }
- cout<< endl;
- };
- // 矩陣相減
- // 直接將結果在此cout 出來
- }
- };
- // ----------------------------
- int main(void)
- {
- // 矩陣內的值自訂
- int a[3][3] = {{1,2,3},
- {4,5,6},
- {7,8,9}};
- int b[3][3] = {{9,8,7},
- {6,5,4},
- {3,2,1}};
- Matrix m1(a), m2(b);
- cout << "m1:" << endl;
- m1.show();
- // 印出m1矩陣
- cout << "m2:" << endl;
- m2.show();
- // 印出m2矩陣
- cout << "m1+m2:" << endl;
- (m1+m2);
- // 使用多載後的運算子+
- cout << "m1-m2:" << endl;
- (m1-m2);
- // 使用多載後的運算子-
- return 0;
- }
- // 直接將結果在此cout 出來
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement