Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // ---------- 提交區塊 ----------
  5. class Matrix
  6. {
  7.     public:
  8.         int matrix[3][3];
  9.         Matrix(int m[3][3])
  10.         {
  11.             for(int i=0;i<3;i++)// 印出矩陣
  12.             {
  13.                 for(int j=0;j<3;j++)
  14.                 {
  15.                     matrix[i][j]=m[i][j];
  16.                 }
  17.             };
  18.             // 建構子,設定matrix內的值
  19.         }
  20.  
  21.         void show()
  22.         {
  23.             for(int i=0;i<3;i++)// 印出矩陣
  24.             {
  25.  
  26.                 for(int j=0;j<3;j++)
  27.                 {
  28.                     cout<< matrix[i][j]<<" ";
  29.                 }
  30.                  cout<< endl;
  31.             };
  32.         }
  33.  
  34.         void operator+(Matrix &m)
  35.         {
  36.  
  37.             // 定義運算子+
  38.              for(int i=0;i<3;i++) // 印出矩陣
  39.             {
  40.                 for(int j=0;j<3;j++)
  41.                 {
  42.                    cout << matrix[i][j]+m.matrix[i][j]<<" ";
  43.                 }
  44.                 cout<< endl;
  45.             };
  46.             // 矩陣相加
  47.             // 直接將結果在此cout 出來
  48.         }
  49.  
  50.         void operator-(const Matrix &m)
  51.         {
  52.             // 定義運算子-
  53.            for(int i=0;i<3;i++) // 印出矩陣
  54.             {
  55.                 for(int j=0;j<3;j++)
  56.                 {
  57.                    cout << matrix[i][j]-m.matrix[i][j]<<" ";
  58.                 }
  59.                 cout<< endl;
  60.             };
  61.             // 矩陣相減
  62.             // 直接將結果在此cout 出來
  63.         }
  64. };
  65. // ----------------------------
  66.  
  67. int main(void)
  68. {
  69.     // 矩陣內的值自訂
  70.     int a[3][3] = {{1,2,3},
  71.                    {4,5,6},
  72.                    {7,8,9}};
  73.     int b[3][3] = {{9,8,7},
  74.                    {6,5,4},
  75.                    {3,2,1}};
  76.     Matrix m1(a), m2(b);
  77.     cout << "m1:" << endl;
  78.      m1.show();
  79.     // 印出m1矩陣
  80.     cout << "m2:" << endl;
  81.     m2.show();
  82.     // 印出m2矩陣
  83.     cout << "m1+m2:" << endl;
  84.     (m1+m2);
  85.     // 使用多載後的運算子+
  86.     cout << "m1-m2:" << endl;
  87.    (m1-m2);
  88.     // 使用多載後的運算子-
  89.     return 0;
  90. }
  91.             // 直接將結果在此cout 出來
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement