中国地质大学C++上机实习报告_第1页
中国地质大学C++上机实习报告_第2页
中国地质大学C++上机实习报告_第3页
中国地质大学C++上机实习报告_第4页
中国地质大学C++上机实习报告_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论