版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Practice Report for The C+ Programming language C+ Course ReportCandidate: Student Number: Major : C+Supervisor : Wu rangzhongChina University of Geosciences(Wuhan)Wuhan, Hubei , P. R. ChinaApril 13, 2015 China University of Geosciences, Faculty of Mecha nics and Electronic Information矩阵的操作一、设计要求(实现
2、以下功能)要求用c+实现:矩阵的创建,输出,复制,转置,加法,减法,乘以一个数,通过行列坐标设置和获取矩阵该位置元素的值。/三个重载函数Matrix CreateMat (int rows, int cols);Matrix CreateMat (int rows, int cols, const int scalar); Matrix CreateMat (int rows, int cols, const int data);void OutputMat(const Matrix & mat);/矩阵的输出void CopyMat(const Matrix & source, Matrix
3、 & target);/把矩阵source复制到矩阵targetMatrix TransMat (const Matrix & mat); /矩阵的转置Matrix AddMat (const Matrix & mat1, const Matrix & mat2); /矩阵的加法Matrix SubMat (const Matrix & mat1, const Matrix & mat2); /矩阵的减法Matrix MulMat (const Matrix & mat, int scalar);/矩阵的数乘int GetMatValue (const Matrix & mat, int ro
4、w, int col); /获取行列坐标为row和col位置的矩阵元素的值int SetMatValue (const Matrix & mat, int row, int col, int scalar); /将行列坐标为row和col位置的矩阵元素的值设置为scalarvoid ReleaseMat(Matrix & m);/释放矩阵元素的空间二、算法分析1.建立结构体struct Matrixint rows, cols; /矩阵的行数和列数int* data; /指向一个容纳矩阵的动态数组的指针变量,使用上等同于数组名。;二维数组的实质为一维数组,因此可以当做一维数组处理更简便。2.函
5、数的实现矩阵的创建(三个重载函数)Matrix CreateMat (int rows, int cols)/分配内存空间Matrix m;m.rows = rows;m.cols = cols;m.data = new int rows * cols;return m;Matrix CreateMat (int rows, int cols, const int scalar)Matrix m;m.rows = rows;m.cols = cols;m.data = new int rows * cols;for(int i = 0; i rows * cols; i+)m.datai =
6、scalar;return m; Matrix CreateMat (int rows, int cols, const int data)Matrix m;m.rows = rows;m.cols = cols;m.data = new int rows * cols;for(int i = 0; i rows * cols; i+)m.datai = datai;return m;矩阵的输出void OutputMat(const Matrix & mat)/矩阵的输出for (int i=0;i mat.rows; i+)for (int j=0;jmat.cols;j+)cout ma
7、t.datai*mat.cols+j ;cout endl;矩阵的复制void CopyMat(const Matrix & source, Matrix & target)/把矩阵source复制到矩阵targetif(target.data=NULL)target.rows = source.rows;target.cols = source.cols;target.data = new int source.rows *source.cols;for (int i=0;i source.rows *source.cols; i+) target.datai= source.datai;e
8、lsedelete target.data;target.data=NULL;target.rows = source.rows;target.cols = source.cols;target.data = new int source.rows *source.cols;for (int i=0;i source.rows *source.cols; i+) target.datai= source.datai;矩阵的转置Matrix TransMat (const Matrix & mat)/矩阵的转置Matrix m;m.rows = mat.cols;m.cols = mat.row
9、s;m.data = new int mat.rows * mat.cols;int i,j; for(i=0;imat.rows;i+) for(j=0;jmat.cols;j+)m.datai*mat.rows+j=mat.dataj*mat.cols+i; return m;矩阵的加法Matrix AddMat (const Matrix & mat1, const Matrix & mat2)/矩阵的加法Matrix m;m.rows = mat1.cols;m.cols = mat1.rows;m.data = new int mat1.rows * mat1.cols;for (i
10、nt i=0;imat1.rows*mat1.cols;i+)m.datai=mat1.datai+mat2.datai;return m;矩阵的减法Matrix SubMat (const Matrix & mat1, const Matrix & mat2)/矩阵的减法Matrix m;m.rows = mat1.cols;m.cols = mat1.rows;m.data = new int mat1.rows * mat1.cols;if (mat1.rows!=mat2.rows)|(mat1.cols!=mat2.cols)exit(1);elsefor (int i=0;imat
11、1.rows*mat1.cols;i+)m.datai=mat1.datai-mat2.datai;return m;矩阵的数乘Matrix MulMat (const Matrix & mat, int scalar)/矩阵的数乘Matrix m;m.rows = mat.cols;m.cols = mat.rows;m.data = new int mat.rows * mat.cols;for (int i=0;imat.rows*mat.cols;i+)m.datai=mat.datai*scalar;return m;获取指定行列的元素值int GetMatValue (const Matrix & mat, int row, int col) /获取行列坐标为row和col位置的矩阵元素的值return mat.datarow*mat.cols+col;将指定行列的元素赋值int SetMatValue (Matrix & mat, int row, int col, int scalar) /将行列坐标为row和col位置的矩阵元素的值设置为scalarmat.datarow*mat.cols+col=s
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026云南省农业科学院质量标准与检测技术研究所科研助理招聘1人笔试模拟试题及答案解析
- 2026国家文化和旅游部直属事业单位招聘社会人员25人(一)笔试备考试题及答案解析
- 2026中国职工服务(集团)有限责任公司面向社会招聘4人笔试模拟试题及答案解析
- 2026年吉林省松原市高职单招职业技能考试题库有答案详细解析
- 2026山东威海市荣成市面向村(社区)党组织书记招聘事业单位人员3人笔试备考题库及答案解析
- 2026浙江事业单位统考台州市天台县招聘42人笔试模拟试题及答案解析
- 浙江省温州瑞安市新纪元实验校2026届初三冲刺压轴卷(四)数学试题试卷含解析
- 湖北省襄樊市重点达标名校2026届初三下学期第一次月考(4月)语文试题试卷含解析
- 2026年安徽省黄山市渔亭中学初三1月调研(期末)测试英语试题含解析
- 辽宁省抚顺市顺城区2026年初三第一次中考适应性统考英语试题试卷(理工类)含解析
- 2026年安庆医药高等专科学校单招职业技能考试题库带答案详解(a卷)
- 2025福建福州市鼓楼区国有资产投资发展集团有限公司公开招聘笔试参考题库附带答案详解
- 2026年安徽审计职业学院单招职业倾向性考试题库含答案详解(基础题)
- 斜坡硬化施工方案(3篇)
- 2026四川成都新都区集中招聘编外人员10考试备考题库及答案解析
- 建筑施工安全设施设计落实情况报告
- GB/T 47067-2026塑料模塑件公差和验收条件
- (2026年)粮油质量检验员考核试题(+答案)
- 2024年通用技术集团招聘笔试参考题库含答案解析
- 《建筑环境与能源应用工程专业概论》配套教学课件
- DB4403-T 87-2020 园林绿化管养规范-(高清现行)
评论
0/150
提交评论