求助:组合向量类Vector,声明并实现一个Matrix类模板,表示矩阵。

2/3? 2020-05-30 04:54:29
矩阵是数学里的一种抽象对象,可以用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;
}
...全文
367 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
任万万万 2022-05-20
  • 打赏
  • 举报
回复

会写了吗?

源代码大师 2021-05-06
  • 打赏
  • 举报
回复
希望对你有帮助:https://blog.csdn.net/it_xiangqiang/category_10581430.html 希望对你有帮助:https://blog.csdn.net/it_xiangqiang/category_10768339.html

64,687

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧