中国地质大学通信工程C++上机实习报告全.docx
- 文档编号:8167900
- 上传时间:2023-01-29
- 格式:DOCX
- 页数:43
- 大小:212.19KB
中国地质大学通信工程C++上机实习报告全.docx
《中国地质大学通信工程C++上机实习报告全.docx》由会员分享,可在线阅读,更多相关《中国地质大学通信工程C++上机实习报告全.docx(43页珍藏版)》请在冰豆网上搜索。
中国地质大学通信工程C++上机实习报告全
计算机高级语言(C++)实习报告
题目
学生姓名:
班学号:
指导老师:
实验一矩阵操作
一、需求规格说明
要求实现:
矩阵的创建,输出,复制,转置,加法,减法,乘以一个数,通过行列坐标设置和获取矩阵该位置元素的值。
通过该题目,掌握
(1)动态数组和指针的使用
(2)二维数组和一维数组的关系
(3)传引用调用参数
(4)函数重载
二、算法设计
设计思想:
1、首先建立矩阵结构体
structMatrix
{
introws,cols;//矩阵的行数和列数
int*data;//指向一个容纳矩阵的动态数组的指针变量,使用上等同于数组名。
};
2、要求实现以下函数:
矩阵的创建,写3个重载的函数CreateMat
第1个函数,有2个参数,分别是矩阵的行数和列数,初始化矩阵,分配内存空间,不赋初值。
MatrixCreateMat(introws,intcols);
第2个函数,有3个参数,分别是矩阵的行数,列数,和矩阵各元素的值(是一个定值,也就是用一个值对矩阵初始化)MatrixCreateMat(introws,intcols,constintscalar);
第3个函数,有3个参数,分别是矩阵的行数,列数,和矩阵各元素的值(用数组对矩阵初始化,采用数组参数)MatrixCreateMat(introws,intcols,constintdata[]);
矩阵的输出,OutputMat函数,矩阵的引用作为参数。
voidOutputMat(constMatrix&mat);
注意:
要求输出为矩阵的形式,例如一个3*3矩阵的输出格式为(前后有中括号,数字对齐):
矩阵的复制,CopyMat函数。
voidCopyMat(constMatrix&source,Matrix&target);//把矩阵source复制到矩阵target
矩阵的转置,TransMat函数。
MatrixTransMat(constMatrix&mat);
矩阵的加法,AddMat函数。
MatrixAddMat(constMatrix&mat1,constMatrix&mat2);
注意,应该/首先判断mat1和mat2的大小是否一致,如果不一致,报错,终止。
报错采用函数exit
(1),需要#include
矩阵的减法,SubMat函数.MatrixSubMat(constMatrix&mat1,constMatrix&mat2);
矩阵的数乘,MulMat函数,矩阵mat乘以一个数scalar
MatrixMulMat(constMatrix&mat,intscalar);
获取一个位置的矩阵元素的值,GetMatValue函数
intGetMatValue(constMatrix&mat,introw,intcol);//获取行列坐标为row和col位置的矩阵元素的值。
11.设置一个位置的矩阵元素的值,SetMatValue函数
intSetMatValue(constMatrix&mat,introw,intcol,intscalar);//将行列坐标为row和col位置的矩阵元素的值设置为scalar。
12.释放矩阵元素做占的freestore的空间。
voidReleaseMat(Matrix&m);
(2)设计表示:
intmain()
{
cout<<"Createa5*5matrixmat1withoutinitialization:
\n";
Matrixmat1=CreateMat(5,5);//调用两个参数的重载函数
OutputMat(mat1);//调用输出函数
ReleaseMat(mat1);//调用release函数
CopyMat(mat3,mat1);//调用矩阵的复制函数
OutputMat(mat1);
cout< \n"; mat2=TransMat(mat1);//调用矩阵的转置函数 OutputMat(mat2); Matrixmat4; cout< \n"; mat4=AddMat(mat1,mat3);//调用矩阵的加法函数 OutputMat(mat4); cout< \n"; mat4=SubMat(mat1,mat3);//调用矩阵的减法函数 OutputMat(mat4); cout< \n"; mat4=MulMat(mat1,2);//调用矩阵的乘法函数 OutputMat(mat4); //设置和获取1个位置的矩阵元素值 intvalue=GetMatValue(mat1,1,2);。 cout<<"\nthevalueofcoordinates(2,3)is: "< SetMatValue(mat1,1,2,100); value=GetMatValue(mat1,1,2); cout<<"\nthevalueofcoordinates(2,3)becomes: "< ReleaseMat(mat1);//释放mat1所占的freestore空间 ReleaseMat(mat2);//释放mat2所占的freestore空间 ReleaseMat(mat3);//释放mat3所占的freestore空间 ReleaseMat(mat4);//释放mat4所占的freestore空间 cout< "< return0; } (3)详细设计表示: 头文件: #ifndefUSEMATRIX_H #defineUSEMATRIX_H #include #include usingnamespacestd; structMatrix { introws,cols;//矩阵的行数和列数 int*data;//指向一个容纳矩阵的动态数组的指针变量,使用上等同于数组名。 }; MatrixCreateMat(introws,intcols);//矩阵的行数和列数,初始化矩阵,分配内存空间,不赋初值 MatrixCreateMat(introws,intcols,constintscalar);//矩阵的行数,列数,和矩阵各元素的值,用一个值对矩阵初始化,是一个定值 MatrixCreateMat(introws,intcols,constintdata[]);//矩阵的行数,列数,和矩阵各元素的值,采用数组参数对矩阵初始化 voidOutputMat(constMatrix&mat);//矩阵的输出函数,矩阵的引用作为参数 voidCopyMat(constMatrix&source,Matrix&target);//把矩阵source复制到矩阵target MatrixTransMat(constMatrix&mat);//矩阵的转置函数 MatrixAddMat(constMatrix&mat1,constMatrix&mat2);//矩阵的加法函数 MatrixSubMat(constMatrix&mat1,constMatrix&mat2);//矩阵的加法函数 MatrixMulMat(constMatrix&mat,intscalar);//矩阵的数乘函数,使矩阵mat乘以一个数scalar intGetMatValue(constMatrix&mat,introw,intcol);//获取行列坐标为row和col位置的矩阵元素的值。 intSetMatValue(Matrix&mat,introw,intcol,intscalar);//设置一个位置的矩阵元素的值,将行列坐标为row和col位置的矩阵元素的值设置为scalar voidReleaseMat(Matrix&m);//释放矩阵元素做占的freestore的空间 #endif 三、调试报告 问题1: 在矩阵的转置函数定义时,由于行数和列数的固定规律及公式没有及时找出来,使得设计的程序无法正确输出 四、用户手册 先根据头文件 #ifndefUSEMATRIX_H #defineUSEMATRIX_H #include #include usingnamespacestd; structMatrix { introws,cols;//矩阵的行数和列数 int*data;//指向一个容纳矩阵的动态数组的指针变量,使用上等同于数组名。 }; MatrixCreateMat(introws,intcols);//矩阵的行数和列数,初始化矩阵,分配内存空间,不赋初值 MatrixCreateMat(introws,intcols,constintscalar);//矩阵的行数,列数,和矩阵各元素的值,用一个值对矩阵初始化,是一个定值 MatrixCreateMat(introws,intcols,constintdata[]);//矩阵的行数,列数,和矩阵各元素的值,采用数组参数对矩阵初始化 voidOutputMat(constMatrix&mat);//矩阵的输出函数,矩阵的引用作为参数 voidCopyMat(constMatrix&source,Matrix&target);//把矩阵source复制到矩阵target MatrixTransMat(constMatrix&mat);//矩阵的转置函数 MatrixAddMat(constMatrix&mat1,constMatrix&mat2);//矩阵的加法函数 MatrixSubMat(constMatrix&mat1,constMatrix&mat2);//矩阵的加法函数 MatrixMulMat(constMatrix&mat,intscalar);//矩阵的数乘函数,使矩阵mat乘以一个数scalar intGetMatValue(constMatrix&mat,introw,intcol);//获取行列坐标为row和col位置的矩阵元素的值。 intSetMatValue(Matrix&mat,introw,intcol,intscalar);//设置一个位置的矩阵元素的值,将行列坐标为row和col位置的矩阵元素的值设置为scalar voidReleaseMat(Matrix&m);//释放矩阵元素做占的freestore的空间 #endif 参考应用文件实现程序功能: #include #include"UseMatrix.h" #include usingnamespacestd; intmain() { //第一种创建方式 cout<<"Createa5*5matrixmat1withoutinitialization: \n"; Matrixmat1=CreateMat(3,3);//创建5*5的矩阵mat1,不设初值。 OutputMat(mat1);//输出该矩阵 //第二种创建方式 cout< cout<<"Createa4*4matrixmat2withvaluebeing7: \n"; Matrixmat2=CreateMat(3,3,7);//创建4*4的矩阵mat2,各元素初值为7。 OutputMat(mat2);//输出该矩阵 //第三种创建方式 cout< \n"; intdata[]={1,2,3, 4,5,6, 7,8,9}; Matrixmat3=CreateMat(3,3,data);//创建3*3的矩阵mat3,用data赋初值。 OutputMat(mat3); CopyMat(mat3,mat1); OutputMat(mat1);//输出该矩阵mat1,和mat3相同,3*3矩阵。 //实现矩阵转置(ok) cout< \n"; mat2=TransMat(mat3); OutputMat(mat2); //使用加法,减法,数乘 Matrixmat4; cout< \n"; mat4=AddMat(mat1,mat3); OutputMat(mat4); cout< \n"; mat4=SubMat(mat1,mat3); OutputMat(mat4); cout< \n"; mat4=MulMat(mat1,2); OutputMat(mat4); //设置和获取1个位置的矩阵元素值 intvalue=GetMatValue(mat1,1,2);//得到mat1第2行第3列的元素值。 cout<<"\nthevalueofcoordinates(2,3)is: "< SetMatValue(mat1,1,2,100); value=GetMatValue(mat1,1,2);//得到mat1第2行第3列的新的元素值。 cout<<"\nthevalueofcoordinates(2,3)becomes: "< ReleaseMat(mat1);//释放mat1所占的freestore空间 ReleaseMat(mat2);//释放mat2所占的freestore空间 ReleaseMat(mat3);//释放mat3所占的freestore空间 ReleaseMat(mat4);//释放mat4所占的freestore空间 cout< "< return0; } 五、附录 头文件: #ifndefUSEMATRIX_H #defineUSEMATRIX_H #include #include usingnamespacestd; structMatrix { introws,cols;//矩阵的行数和列数 int*data;//指向一个容纳矩阵的动态数组的指针变量,使用上等同于数组名。 }; MatrixCreateMat(introws,intcols);//矩阵的行数和列数,初始化矩阵,分配内存空间,不赋初值 MatrixCreateMat(introws,intcols,constintscalar);//矩阵的行数,列数,和矩阵各元素的值,用一个值对矩阵初始化,是一个定值 MatrixCreateMat(introws,intcols,constintdata[]);//矩阵的行数,列数,和矩阵各元素的值,采用数组参数对矩阵初始化 voidOutputMat(constMatrix&mat);//矩阵的输出函数,矩阵的引用作为参数 voidCopyMat(constMatrix&source,Matrix&target);//把矩阵source复制到矩阵target MatrixTransMat(constMatrix&mat);//矩阵的转置函数 MatrixAddMat(constMatrix&mat1,constMatrix&mat2);//矩阵的加法函数 MatrixSubMat(constMatrix&mat1,constMatrix&mat2);//矩阵的加法函数 MatrixMulMat(constMatrix&mat,intscalar);//矩阵的数乘函数,使矩阵mat乘以一个数scalar intGetMatValue(constMatrix&mat,introw,intcol);//获取行列坐标为row和col位置的矩阵元素的值。 intSetMatValue(Matrix&mat,introw,intcol,intscalar);//设置一个位置的矩阵元素的值,将行列坐标为row和col位置的矩阵元素的值设置为scalar voidReleaseMat(Matrix&m);//释放矩阵元素做占的freestore的空间 #endif 实现文件: #include"UseMatrix.h" #include #include usingnamespacestd; MatrixCreateMat(introws,intcols)//矩阵的行数和列数,初始化矩阵,分配内存空间,不赋初值 { Matrixm; m.rows=rows; m.cols=cols; m.data=newint[rows*cols]; for(inti=0;i returnm; } MatrixCreateMat(introws,intcols,constintscalar)/*矩阵的行数,列数,和矩阵各元素的值 用一个值对矩阵初始化,是一个定值*/ { Matrixm; m.rows=rows; m.cols=cols; m.data=newint[rows*cols]; for(inti=0;i m.data[i]=scalar; returnm; } MatrixCreateMat(introws,intcols,constintdata[])/*矩阵的行数,列数,和矩阵各元素的值 采用数组参数对矩阵初始化*/ { Matrixm; m.rows=rows; m.cols=cols; m.data=newint[rows*cols]; for(inti=0;i m.data[i]=data[i]; returnm; } voidOutputMat(constMatrix&mat)//矩阵的输出函数,矩阵的引用作为参数 { cout<<"["; inti; for(i=0;i { cout< if((i==mat.rows*mat.cols-1)) cout<<"]"< else if((i+1)%mat.rows==0) cout< else cout<<""; } } voidCopyMat(constMatrix&source,Matrix&target)//把矩阵source复制到矩阵target { if(! (source.rows==target.rows&&source.cols==target.cols)) { cout<<"error! "< exit (1); } target.rows=source.rows; target.cols=source.cols; for(inti=0;i target.data[i]=source.data[i]; } MatrixTransMat(constMatrix&mat)//矩阵的转置函数 { Matrixm; m.rows=mat.rows; m.cols=mat.cols; m.data=newint[mat.rows*mat.cols]; for(introw=0;row for(intcol=0;col m.data[col*mat.rows+row]=mat.data[row*mat.rows+col]; returnm; } MatrixAddMat(constMatrix&mat1,constMatrix&mat2)//矩阵的加法函数 { Matrixm; if(! (mat1.rows==mat2.rows&&mat1.cols==mat2.cols)) { cout<<"error! "< exit (1); } else { m.rows=mat1.rows; m.cols=mat1.cols; m.data=newint[mat1.rows*mat1.cols]; for(inti=0;i m.data[i]=mat1.data[i]+mat2.data[i]; } returnm; } MatrixSubMat(constMatrix&mat1,constMatrix&mat2)///矩阵的减法函数 { Matrixm; if(! (mat1.rows==mat2.rows&&mat1.cols==mat2.cols)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 中国地质大学 通信工程 C+ 上机 实习 报告