Programming Practice

此程式習作將設計一個矩陣類別 (matrix class) 和它的兩個子類別:向量類別 (vector class) 和方陣類別 (square matrix class)。假設列和行的索引皆從 0 開始。矩陣類別的介面定義如下:

class Matrix {

 

friend ostream &operator<<(ostream&, const Matrix&); // friend output function
   friend istream &operator>>(istream&, Matrix&); // friend input function

 

protected:

  int row, col;    // row size and column size of a matrix

  float** m;   // a pointer to matrix elements

  void allocateMatrix();  // allocate matrix elements

  void deallocateMatrix();  // deallocate matrix elements

 

  public:

    Matrix(int=0, int=0);  // matrix constructor

    Matrix(const Matrix&);  // matrix copy constructor

    ~Matrix();  // matrix destructor

    void setSize(int, int);  // set row size and column size

    void setElement(int, int, float);  // set a matrix element via parameter

    float getElement(int, int) const;  // get a matrix element

    void printElement(int, int) const;  // print a matrix element

    int getRow() const;  // get row size

    int getCol() const;  // get column size

    Matrix operator+(const Matrix &) const;  // matrix-matrix addition

    Matrix operator-(const Matrix &) const;  // matrix-matrix subtraction

    Matrix operator*(const Matrix &) const;  // matrix-matrix multiplication

    Matrix operator+(const float &) const;  // matrix-scalar addition

    Matrix operator-(const float &) const;  // matrix-scalar subtraction

    Matrix operator*(const float &) const;  // matrix-scalar multiplication

    Matrix &operator=(const Matrix &);  // matrix assignment

};

兩個子類別 (subclasses) 的描如下:

1.   Vector: 代表行向量 (column vector),並加入一個子類別公用函式 (public function) 以計算兩個向量的內積 (inner product)。假如 XY 為兩個長度為 n 的向量,則其內 X·Y 是定義為 x0y0 + x1y1 + ¼ + xn-1yn-1,即

2.      SMatrix: 代表方陣 (square matrix),並加入一個子類別公用函式 (public function) 以計算一個方陣的行列式 (determinant)。如果 A 是一個1´1 的方陣 ,則 A 的行列式即是 A 的元素值;如果 A 是一個 n´n 的方陣和 det(A) 為方陣 A 的行列式,則其遞迴定義 (recursive definition) (依第 i 列展開):

cofactor(Ai,j ) 為在方陣A中將第 i 列及第 j 行移除後的 (n-1)´(n-1) 之子方陣。(請參考 C 語言的行列式程式 determinant.c)

在你的主程式,必需至少包含下列各種計算的示範:

1.      宣告矩陣、向量、及方陣的物件變數,

2.      設定矩陣、向量、及方陣的元素,

3.      改變矩陣的大小,並重新設定矩陣元素,

4.      矩陣、向量、及方陣的加法、減法、乘法等算術運算,並輸出其結果,

5.      計算兩個向量的內積,並輸出其結果

6.   計算一個方陣的行列式,並輸出其結果。

矩陣、向量、及方陣的頭檔 matrix_class.h, vector_subclass.h, 和 smatrix_subclass.h, 主程式 matrix_class.inheritance.cpp;請完成矩陣、向量、及方陣的實作部分:matrix_class.cpp, vector_subclass.cpp, smatrix_subclass.cpp。程式執行範例:

 

Enter the length of vector**** Input Matrix a (3x2)
Input matrix element [0,0]:
2
Input matrix element [0,1]:
4
Input matrix element [1,0]:
1
Input matrix element [1,1]:
3
Input matrix element [2,0]:
2
Input matrix element [2,1]:
3

**** Input Matrix b (2x3)
Input matrix element [0,0]:
2
Input matrix element [0,1]:
3
Input matrix element [0,2]:
4
Input matrix element [1,0]:
2
Input matrix element [1,1]:
1
Input matrix element [1,2]:
4

**** Input Matrix c (3x3)
Input matrix element [0,0]:
1
Input matrix element [0,1]:
4
Input matrix element [0,2]:
5
Input matrix element [1,0]:
3
Input matrix element [1,1]:
2
Input matrix element [1,2]:
3
Input matrix element [2,0]:
2
Input matrix element [2,1]:
1
Input matrix element [2,2]:
4

**** Compute d=((a*b)+0.5)-c*0.1
**** Output Matrix d (3x3)
Matrix element [0,0]: 12.4
Matrix element [0,1]: 10.1
Matrix element [0,2]: 24
Matrix element [1,0]: 8.2
Matrix element [1,1]: 6.3
Matrix element [1,2]: 16.2
Matrix element [2,0]: 10.3
Matrix element [2,1]: 9.4
Matrix element [2,2]: 20.1

**** Redefine Matrix b (3x3)
Input matrix element [0,0]:
3
Input matrix element [0,1]:
2
Input matrix element [0,2]:
2
Input matrix element [1,0]:
1
Input matrix element [1,1]:
4
Input matrix element [1,2]:
3
Input matrix element [2,0]:
2
Input matrix element [2,1]:
1
Input matrix element [2,2]:
3

**** Compute d=a+b
The operand matrix sizes do not match.

**** Compute d=a*b
The operand matrix sizes do not match.

Enter the length of vector u:
6
Enter the elements of vector u:
Input matrix element [0,0]:
3
Input matrix element [1,0]:
2
Input matrix element [2,0]:
1
Input matrix element [3,0]: 4
Input matrix element [4,0]:
5
Input matrix element [5,0]:
3

Enter the elements of vector v:
Input matrix element [0,0]:
2
Input matrix element [1,0]:
2
Input matrix element [2,0]:
1
Input matrix element [3,0]:
4
Input matrix element [4,0]:
5
Input matrix element [5,0]:
6

Enter the length of vector w (different from the length of vector u):
4
Enter the elements of vector w:
Input matrix element [0,0]:
3
Input matrix element [1,0]:
2
Input matrix element [2,0]:
1
Input matrix element [3,0]:
1

**** Compute the inner product of vectors u and v
The inner product of vectors u and v: 70

**** Compute the inner product of vectors u and w
Vector lengths do not match.

Enter the size of square matrix r: 3
Enter the elements of square matrix r:
Input matrix element [0,0]:
2
Input matrix element [0,1]:
1
Input matrix element [0,2]:
2
Input matrix element [1,0]:
3
Input matrix element [1,1]:
4
Input matrix element [1,2]:
5
Input matrix element [2,0]:
6
Input matrix element [2,1]:
4
Input matrix element [2,2]:
3

Enter the elements of square matrix s:
Input matrix element [0,0]:
2
Input matrix element [0,1]:
1
Input matrix element [0,2]:
3
Input matrix element [1,0]:
4
Input matrix element [1,1]:
3
Input matrix element [1,2]:
2
Input matrix element [2,0]:
4
Input matrix element [2,1]:
5
Input matrix element [2,2]:
6

**** Compute the determinant of r
The determinant of r: -19

**** Compute the determinant of s
The determinant of s: 24

**** Compute the product of determinant r and determinant s
The product of determinant r and determinant s: -456

**** Compute the determinant of r*s
The determinant of r*s: -456