矩陣是數學里的一種抽象物件,可以用C++提供的靜態陣列來表示矩陣,其大小在編譯時就已經確定,在運行時無法修改,而且不檢查下標是否越界。可以利用教材本章提供的向量類Vector,用向量的方式實作矩陣,用一個指標向量來表示矩陣,其中的每個指標又各指向一個向量,用它們來表示矩陣的行向量。矩陣的邏輯結構如下圖所示。

組合向量類Vector,宣告并實作一個Matrix類模板,表示矩陣。Matrix類模板包括:
·Vector型別的私有資料成員rows,存放矩陣元素。
·建構式,將矩陣的行、列大小設定為給定的引數。
·建構式,將矩陣的行、列大小設定為給定的引數,給矩陣元素賦相同的初始值。
·多載下標運算子[]。
·訪問器函式getRows,用于獲取矩陣行數。
·訪問器函式getColumns,用于獲取矩陣列數。
Matrix類模板如下:
template <typename T>
class Matrix {
public:
Matrix(int row, int column);
Matrix(int row, int column, const T &value);
Vector<T> &operator[](int index);
Vector<T> operator[](int index) const;
int getRows() const;
int getColumns() const;
private:
Vector<Vector<T>*> rows; // 存放矩陣元素
};
以普通函式的形式多載*運算子函式,實作矩陣乘法。
template <typename T>
Matrix<T> operator*(const Matrix<T> &lhs, const Matrix<T> &rhs);
printMatrix函式輸出矩陣的值。
template <typename T>
void printMatrix(const Matrix<T> &m);
【輸入】
輸入3×3矩陣a和矩陣b。
【輸出】
矩陣a乘以矩陣b的結果。每個元素輸出寬度為4。
【輸入示例】
1 1 7
7 5 6
9 6 1
6 1 6
4 1 5
1 5 1
【輸出示例】
17 37 18
68 42 73
79 20 85
#include <iostream>
#include <iomanip>
#include <stdexcept>
using namespace std;
template <typename T>
class Vector {
public:
Vector(int size); // 建構式
Vector(int size, const T& value); // 建構式
Vector(const Vector<T>& v); // 拷貝建構式
virtual ~Vector(); // 解構式
const Vector<T>& operator=(const Vector<T>& right); // 多載賦值運算子
T& operator[](int index); // 多載下標運算子
T operator[](int index) const; // 多載下標運算子
int getSize() const;
void resize(int size);
private:
T* pVector; // 指標,指向存放陣列元素的動態分配記憶體空間
int size; // 陣列長度
};
template <typename T>
Vector<T>::Vector(int size) {
if (size > 0)
this->size = size;
else
throw invalid_argument("陣列長度必須是正整數!");
pVector = new T[size];
}
template <typename T>
Vector<T>::Vector(int size, const T& value) {
if (size > 0)
this->size = size;
else
throw invalid_argument("陣列長度必須是正整數!");
pVector = new T[size];
for (int i = 0; i < size; ++i)
pVector[i] = value;
}
template <typename T>
Vector<T>::Vector(const Vector<T>& v) {
size = v.size;
pVector = new T[size];
for (int i = 0; i < size; ++i)
pVector[i] = v.pVector[i];
}
template <typename T>
Vector<T>::~Vector() {
delete[] pVector;
}
template <typename T>
const Vector<T>& Vector<T>::operator=(const Vector<T>& right) {
if (this != &right) {
if (size != right.size) {
delete[] pVector;
size = right.size;
pVector = new T[size];
}
for (int i = 0; i < size; ++i)
pVector[i] = right.pVector[i];
}
return *this;
}
template <typename T>
T& Vector<T>::operator[](int index) {
if (index < 0 || index > size - 1)
throw out_of_range("陣列下標超出允許范圍!");
return pVector[index];
}
template <typename T>
T Vector<T>::operator[](int index) const {
if (index < 0 || index > size - 1)
throw out_of_range("陣列下標超出允許范圍!");
return pVector[index];
}
template <typename T>
int Vector<T>::getSize() const {
return size;
}
template <typename T>
void Vector<T>::resize(int size) {
if (size > 0) {
if (this->size != size) {
T* old = pVector;
pVector = new T[size];
int newSize = (this->size > size) ? size : this->size;
for (int i = 0; i < newSize; ++i)
pVector[i] = old[i];
this->size = size;
delete[] old;
}
}
else
throw invalid_argument("陣列長度必須是正整數!");
}
/* 請在此處分別撰寫Matrix類、多載operator*函式、printMatrix函式 */
int main() {
const int ROW_SIZE = 3;
const int COLUMN_SIZE = 3;
Matrix<int> a(ROW_SIZE, COLUMN_SIZE);
Matrix<int> b(ROW_SIZE, COLUMN_SIZE);
for (int i = 0; i < ROW_SIZE; ++i) {
for (int j = 0; j < COLUMN_SIZE; ++j) {
cin >> a[i][j];
}
}
for (int i = 0; i < ROW_SIZE; ++i) {
for (int j = 0; j < COLUMN_SIZE; ++j) {
cin >> b[i][j];
}
}
printMatrix(a * b);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/55033.html
標籤:C++ 語言
下一篇:c語言
