# 一、面向对象程序设计实践(C++)实验报告
## 1.1 C++ 基础知识实验
### 1.1.1 题目要求
编写 C++ 程序完成“矩阵”以下功能:假定矩阵大小为 4×5(整型)
矩阵空间采用 new 动态申请,保存在指针中
定义矩阵初始化函数,可以从 cin 中输入矩阵元素
定义矩阵输出函数,将矩阵格式化输出到 cout
定义矩阵相加的函数,实现两个矩阵相加的功能,结果保存在另一个矩阵中
定义矩阵相减的函数,实现两个矩阵相减的功能,结果保存在另一个矩阵中
动态申请三个矩阵:A1、A2、A3
初始化 A1、A2
计算并输出 A3 = A1 加 A2,A3 = A1 减 A2
释放矩阵空间
代码
```c++
Matrix.hpp
/*
Author: Sheldon Zhang
Description: A simple support for matrix
*/
# include <vector>
# include <iostream>
# include "cmath"
# include "complex.h"
using namespace std;
# define ROW_MAX 1000
# define COL_MAX 1000
template <typename T>
class matrix;
template <typename T>
istream & operator >> (istream &is, matrix<T> & obj);
template <typename T>
ostream & operator << (ostream &os, matrix<T> const & obj);
/*
Class Description: a class provides fundamental matrix storage and calculation
supported by vector in STL
*/
template <typename T>
class matrix
{
private:
friend istream & operator >> <>(istream &is, matrix<T> & obj);
friend ostream & operator << <>(ostream &os, matrix<T> const & obj);
private:
int row, col;
vector<vector<T>> data;
private:
void zero();
public:
matrix();
matrix(int inputRow, int inputCol);
matrix(const matrix<T> &obj);
matrix(const matrix<T> *obj);
~matrix();
matrix<T> operator + (const matrix<T> &rhs);
matrix<T> operator - (const matrix<T> &rhs);
matrix<T> operator * (const matrix<T> &rhs);
matrix<T> & operator = (const matrix<T> &rhs);
void operator *= (const matrix<T> &rhs);
void operator += (const matrix<T> &rhs);
void operator -= (const matrix<T> &rhs);
void resize(int row, int col);
};
/*----------------------------------------------------*/
//IMPLEMENT
/*----------------------------------------------------*/
template <typename T>
/*
function name: istream & operator >>
description: overload operator >> (stream input)
arguments:
istream &is: input stream object from which characters are extracted
matrix<T> &obj: a referance of an object of class matrix
return value: the istream object(is)
*/
istream & operator >> (istream &is, matrix<T> &obj)
{
for(int i = 0; i < obj.row; i++)
for(int j = 0; j < obj.col; j++)
is >> obj.data[i][j];
return is;
}
template <typename T>
/*
function name: ostream & operator <<
description: overload operator <<
arguments:
ostream &os: output stream object where characters are inserted
matrix<T> const &obj: a referance of an object of class matrix
return value: the ostream object(os)
output format:
[
, ... , ]
, ... , ]
]
*/
ostream & operator << (ostream &os, matrix<T> const &obj)
{
os << "[" << endl;
for(int i = 0; i < obj.row; i++)
{
os << "[ ";
for(int j = 0; j < obj.col; j++)
os << obj.data[i][j] << " ";
os << " ]" <<endl;
}
os << "]";
return os;
}
template <typename T>
/*
function name: matrix
description: a constructor of class matrix
set varible row and col to 0
resize data to a 0 by 0 matrix
arguments: None
return value: None
*/
matrix<T>::matrix()
{
row = 4;
col = 5;
data.resize(4, vector<T>(5));
}
template <typename T>
/*
function name: matrix
description: a constructor of class matrix
check if the arguments are legal
record the values of arguments
allocate space for the matrix of the wanted dimention
set all elements to 0
arguments:
inputRow: the wanted row of the matrix
inputCol: the wanted column of the matrix
return value: None
*/
matrix<T>::matrix(int inputRow, int inputCol)
{
if(inputRow <= 0 || inputCol <= 0)
std::cerr << "Invalid Parameter" << '\n';
if(inputRow > ROW_MAX || inputCol > COL_MAX)
std::cerr << "matrix is too large" << '\n';
row = inputRow;
col = inputCol;
data.resize(row, vector<T>(col));
zero();
}
template <typename T>
/*
function name: matrix
description: a copy constructor of class matrix
arguments: a referance of an object of class matrix
return value: None
*/
matrix<T>::matrix(const matrix<T> &obj)
{
col = obj.col;
row = obj.row;
data.resize(row, vector<T>(col));
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
data[i][j] = obj.data[i][j];
}
template <typename T>
/*
function name: matrix
description: to create a copy of the matrix which the pointer points at
arguments: a pointer of class matrix
return value: None
*/
matrix<T>::matrix(const matrix<T> *obj)
{
col = obj->col;
row = obj->row;
data.resize(row, vector<T>(col));
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
data[i][j] = obj->data[i][j];
}
template <typename T>
/*
function name: ~matrix
description: a destructor of class matrix
set varible row and col to 0
delete all elements in data
arguments: None
return value: None
*/
matrix<T>::~matrix()
{
row = 0;
col = 0;
data.clear();
}
template <typename T>
/*
function name: zero
description: set all elements in data to 0
arguments: None
return value: None
*/
void matrix<T>::zero()
{
for(int i = 0; i < row; i++)
for(int j = 0; j < col ; j++)
data[i][j] = 0;
}
template <typename T>
/*
function name: matrix<T>::operator +
description: overload operator +
check if the dimension of two matrices matches
add the elements in the same localtion
(add this->data[i][j] and rhs.data[i][j])
arguments: matrix<T> &rhs: a referance of an object of class matrix
return value: an object of class matrix
*/
matrix<T> matrix<T>::operator + (const matrix<T> &rhs)
{
if(row != rhs.row || col != rhs.col)
std::cerr << "the dimension is not the same" << '\n';
matrix<T> matrixTemp(rhs.data.size(), rhs.data[0].size());
for(int i = 0; i < rhs.data.size(); i++)
for(int j = 0; j < rhs.data[0].size(); j++)
matrixTemp.data[i][j] = this->data[i][j] + rhs.data[i][j];
return matrixTemp;
}
template <typename T>
/*
function name: matrix<T>::operator -
description: overload operator -
check if the dimension of two matrices matches
subtruct this->data[i][j] by rhs.data[i][j]
arguments: matrix<T> &rhs: a referance of an object of class matrix
return value: an object of class matrix
*/
matrix<T> matrix<T>::operator - (const matrix<T> &rhs)
{
if(row != rhs.row || col != rhs.col)
std::cerr << "the dimension is not the same" << '\n';
matrix<T> matrixTemp(rhs);
for(int i = 0; i < rhs.data.size(); i++)
for(int j = 0; j < rhs.data[0].size(); j++)
matrixTemp.data[i][j] = this->data[i][j] - rhs.data[i][j];
return matrixTemp;
}
template <typename T>
/*
function name: matrix<T>::operator *
description: overload operator *
check if the dimension of two matrices matches
do the multiple operation
arguments: matrix<T> &rhs: a referance of an object of class matrix
return value: an object of class matrix
*/
matrix<T> matrix<T>::operator * (const matrix<T> &rhs)
{
if(this->data[0].size() != rhs.data.size())
std::cerr << "matrix A can not be multiplied by matrix B" << '\n';
matrix<T> matrixTemp(this->row, rhs.col);
for(int i = 0; i < this->row; i++)
for(int j = 0; j < this->col; j++)
for(int k = 0; k < rhs.col; k++)
matrixTemp.data[i][k] += this->data[i][j] * rhs.data[j][k];
return matrixTemp;
}
template <typename T>
/*
function n
没有合适的资源?快使用搜索试试~ 我知道了~
编写 C++ 程序完成“矩阵”【100011933】
共370个文件
png:112个
cpp:67个
h:57个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 71 浏览量
2023-04-19
14:37:49
上传
评论
收藏 4.94MB ZIP 举报
温馨提示
这次试验我实现了一个功能较为完备的矩阵运算库,包括基础的加减运算,元素乘法和点乘,虽然功能较为简单,但是我在其中学到了模板的使用方法并进行了大量的练习。同时对含有 class 的代码有了 debug 的经验。通过这次实验我窥见了面向对象编程的方便和先进之处,主要在于对数据和逻辑的封装。面向对象,则在基于对象的基础上增加了多态性。所谓多态,就是可以用统一的方法对不同的对象进行同样的操作。当然,这些对象不能完全不同,而需要有一些共性,只有存在了这些共性才可能用同样的方法去操作它们。
资源推荐
资源详情
资源评论
收起资源包目录
编写 C++ 程序完成“矩阵”【100011933】 (370个子文件)
sql_ops.cpp 16KB
sql_ops.cpp 16KB
sql_ops.cpp 12KB
client.cpp 9KB
client.cpp 9KB
server.cpp 8KB
player.cpp 7KB
player.cpp 7KB
setter.cpp 5KB
setter.cpp 5KB
player.cpp 5KB
player.cpp 5KB
moc_player.cpp 4KB
moc_game.cpp 4KB
moc_setter.cpp 4KB
moc_login_window.cpp 4KB
view_all_question.cpp 4KB
view_all_question.cpp 4KB
moc_register_window.cpp 3KB
moc_reset_pwd.cpp 3KB
moc_client.cpp 3KB
login_window.cpp 3KB
login_window.cpp 3KB
setter.cpp 3KB
setter.cpp 3KB
moc_view_all_question.cpp 3KB
login_window.cpp 3KB
moc_mainwindow.cpp 3KB
register_window.cpp 3KB
register_window.cpp 3KB
login_window.cpp 2KB
reset_pwd.cpp 2KB
reset_pwd.cpp 2KB
game.cpp 2KB
main.cpp 2KB
main.cpp 2KB
inheritance.cpp 2KB
game.cpp 1KB
game.cpp 1KB
game.cpp 1KB
rectangle.cpp 1KB
view_all_question.cpp 1KB
view_all_question.cpp 1KB
inheritance-II.cpp 1KB
person.cpp 1KB
person.cpp 1KB
person.cpp 1KB
main.cpp 1KB
person.cpp 1KB
main.cpp 1KB
Polymorphism.cpp 1KB
reset_pwd.cpp 942B
reset_pwd.cpp 942B
register_window.cpp 941B
register_window.cpp 941B
friend.cpp 802B
guess_number.cpp 800B
iostream.cpp 453B
unit_test.cpp 383B
matrix.cpp 351B
iostream - I.cpp 350B
main.cpp 325B
mainwindow.cpp 219B
mainwindow.cpp 219B
mainwindow.cpp 219B
mainwindow.cpp 219B
mainwindow.cpp 219B
info.db 20KB
info.db 20KB
Makefile.Debug 255KB
作业(胡博老师).doc 94KB
实验(基础题目)-面向对象程序设计与实践-2019 发布版.doc 70KB
实验报告.docx 478KB
实验(综合-单词消除游戏)-面向对象程序设计与实践-2019 发布版.docx 23KB
client.exe 551KB
matrix.exe 115KB
test.exe 50KB
inheritance-II.exe 49KB
inheritance.exe 48KB
iostream - I.exe 47KB
iostream.exe 47KB
rectangle.exe 46KB
Polymorphism.exe 45KB
guess_number.exe 45KB
friend.exe 44KB
ui_game.h 59KB
ui_player.h 44KB
ui_setter.h 44KB
ui_reset_pwd.h 42KB
ui_register_window.h 41KB
ui_login_window.h 41KB
ui_view_all_question.h 41KB
sql_ops.h 3KB
matrix.h 2KB
ui_mainwindow.h 2KB
game.h 1KB
ui_client.h 1KB
sql_ops.h 1KB
player.h 1KB
sql_ops.h 1KB
共 370 条
- 1
- 2
- 3
- 4
资源评论
神仙别闹
- 粉丝: 2668
- 资源: 7640
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功