ImageVerifierCode 换一换
格式:DOCX , 页数:32 ,大小:401.26KB ,
资源ID:5372010      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/5372010.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(实验五模板与异常处理.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

实验五模板与异常处理.docx

1、实验五模板与异常处理皖西学院信息工程学院School of Information and Engineering, West Anhui University面向对象方法与C+程序设计实验报告 专 业计算机科学与技术专业班 级计科1202班姓 名柯冬生学 号2012013854任课教师何富贵 实验五 模板与异常处理(2学时)学号:2012013854 姓名:柯冬生 班级:计科1202班 成绩:实验名称:模板与异常处理实验地点:综合楼207所使用的工具软件及环境:Microsoft Visual C+ 6.0一、实验目的:(1)正确理解模板的概念。(2)掌握函数模板和类模板的声明和使用方法。(

2、3)学习简单的异常处理方法。二、实验内容:1.分析并调试下列程序,写出运行结果并分析原因。 (1) /test6_1_1.cpp #include using namespace std; template T max (T x,T y) return xy? x:y;int max(int a,int b)return ab? a:b;double max (double a,double b)return ab? a:b;int main() cout”max(3,7) is “max(3,7)endl; return 0;(2)/test6_1_2.cpp #include using

3、namespace std;int max(int a,int b)return ab? a:b;double max (double a,double b)return ab? a:b;int main() cout”max(3,7) is “max(3,7)endl; return 0;代码:(1) #include using namespace std; template T max(T x,T y) return xy?x:y; int max(int a,int b) return ab?a:b; double max(double a,double b) return ab?a:

4、b; int main() coutmax(3,7) is max(3,7)endl; return 0; (2)#include using namespace std; int max(int a,int b) return ab?a:b; double max(double a,double b) return ab?a:b; int main() coutmax(3,7) is max(3,7)endl; return 0; 实验结果:(1) 定义了一个求最大值的函数模板,定义了一个求整型最大值的函数以及一个求双精度型最大值的函数,这两个同名非模板函数与函数模板重载.3和7均是字符型,

5、找不到与之匹配的函数,所以调用了函数模板,使其实例化,由T型转变为char型。(2)定义了一个求整型最大值的函数以及一个求双精度型最大值的函数,这两个同名函数重载.3和7均是字符型,找不到与之匹配的函数,所以得不出正确结果。2.编写一个求任意类型数组中最大元素和最小元素的程序,要求将求最大元素和最小元素的函数设计成函数模板。代码:#include using namespace std; template T max (T * x,int n) T max=x0; for(int i=1;ixi? max:xi; return max; template T min (T * x,int n)

6、 T min=x0; for(int i=1;in;i+) min=minxi? min:xi; return min; int main() int a5= 1,4,2,3,5 ; double b5=1.1,4.4,2.2,3.3,5.5; cout数组a5= 1,4,2,3,5的最大值is: max(a,5)endl; cout数组a5= 1,4,2,3,5的最小值is: min(a,5)endl; cout数组b5=1.1,4.4,2.2,3.3,5.5的最大值is: max(b,5)endl; cout数组b5=1.1,4.4,2.2,3.3,5.5的最小值is: min(b,5)e

7、ndl; return 0; 实验结果:3.编写一个程序,使用类模板对数组元素进行排序、倒置、查找和求和。【提示】设计一个类模板template class Array .;具有对数组元素进行排序、倒置、查找和求和功能,然后产生类型实参分别为int型和double型的两个模板类,分别对整型数组与双精度数组完成所要求的操作。代码:#include using namespace std; template class Array1 public: Array1 (Type *a,int length) len=length; for(int i=0;ilen;i+) Arrayi=ai; /te

8、mplate void sort()/排序 Type a10 ,temp; for(int n=0;nlen;n+) an=Arrayn; for(int j=0;jlen;j+) for (int i=0;iai+1) temp=ai; ai=ai+1; ai+1=temp; cout数组排序endl; for(int m=0;mlen;m+) coutam coutendl; /template void invert()/倒置 Type invert10; for(int j=0;jlen;j+) invertj=Arraylen-1-j; cout数组倒置endl; for(int m

9、=0;mlen;m+) coutinvertm coutendl; void seek(Type y)/查找 int i=0; for(int j=0;jlen;j+) if(Arrayj=y) cout查找成功!在第j位置endl; i=j; / break; if(i=0) cout查找不成功!endl; /template void sum()/求和 Type sum=Array0; for(int j=1;jlen;j+) sum=sum+Arrayj; cout数组之和:sumendl; private: int len; Type Array10; ; int main() int

10、 c5=1,8,2,7,9; double d4=3.1,8.9,56.9,2.9; Array1a(c,5); Array1b(d,4); cout*int型数组*endl; cout原函数为:endl; for(int i=0;i5;i+) coutci ; coutendl; a.sort(); a.invert(); a.seek(3); a.sum(); cout*double型数组*endl; cout原函数为:endl; for(int j=0;j4;j+) coutdj ; coutendl; b.sort(); b.invert(); b.seek(8.9); b.sum()

11、; return 0; 实验结果:4.编写一个程序,求输入数的平方根。设置异常处理,对输入负数的情况给出提示。代码:#include #include using namespace std; void main() double number; double result; cout number; try if (number 0) throw exception(输入的数是负数! ); result = sqrt(number); cout 平方根是: result endl; catch (exception e) cout e.what() endl; 实验结果:3、实验总结:1.在

12、使用非模板函数时要注意输入的语句应符合函数的类型,或者定义一个函数模板使其实例化,否则无法得出正确结论。2.对异常处理有了一些理解,但不知道异常处理(如第4题)可以循环吗?3.对用类模板实现线性表不是很懂。 任课教师签名: 实验六 C+的流类库与输入输出(2学时)学号:2012013854 姓名:柯冬生 班级:计科1202班 成绩:实验名称:C+的流类库与输入输出实验地点:综合楼207所使用的工具软件及环境:Microsoft Visual C+ 6.0一、实验目的:(1)掌握C+格式化的输入输出方法。(2)掌握重载运算符“”的方法。(3)掌握磁盘文件的输入输出方法。二、实验内容:1. 下面给

13、出的test7_1_1.cpp程序用于打印九九乘法表,但程序中存在错误。请上机调试,使得此程序运行后,能够输出如下所示的九九乘法表。* 1 2 3 4 5 6 7 8 91 12 2 43 3 6 9 4 4 8 12 165 5 10 15 20 256 6 12 18 24 30 367 7 14 21 28 35 42 498 8 16 24 32 40 48 56 649 9 18 27 36 45 54 63 72 81/test7_1_1.cpp#include #include using namespace std;int main() int i,j; cout”*”; fo

14、r(i=1;i=9;i+)couti” ”; coutendl;for(i=1;i=9;i+) couti; for(j=1;j=i;j+) Couti*j;return 0;代码:#include #include using namespace std;int main() int i,j; coutsetiosflags(ios:right)setw(4)*; for(i=1;i=9;i+) coutsetiosflags(ios:right)setw(4)i; coutendl; for(i=1;i=9;i+) coutsetiosflags(ios:right)setw(4)i; f

15、or(j=1;j=i;j+) coutsetiosflags(ios:right)setw(4)i*j; coutendl; return 0;实验结果:2.下面的程序用于统计文件xyz.txt中的字符个数,请填空完成程序。 /test7_2_1.cpp #include #include using namespace std; int main() char ch;int i=0;ifstream file;file.open(“xyz.txt”,ios:in);if( ) cout”xyz.txt cannot open”endl; abort();While (!file.eof()

16、i+; cout”文件字符个数:”iendl; return 0;代码:#include#includeusing namespace std;int main() char ch; int i=0; ifstream file; file.open(xyz.txt,ios:in); if(!file) coutxyz.txt cannot openendl; abort(); while (!file.eof() file.get(ch); if(ch=97)|(ch=65) i+; cout文件字符个数:iendl; file.close(); return 0;实验结果: 3.重载运算符

17、“”,使其能够输入一件商品的信息和输出这件商品的信息。商品的信息由编号、商品名和价格。假如商品类Merchandise的框架如下:class merchandisepublic: Merchandiss(); Merchandiss(); friend istream& operator(istream& in,Merchandiss& s); friend ostream&operatormer; coutmer; return 0;代码:#includeclass Merchandisepublic: Merchandise(); Merchandise(); friend istream

18、&operator(istream& in,Merchandise& s); friend ostream&operator(istream& in,Merchandise& s) coutinput the Merchandises:endl; couts.no; couts.name; couts.price; return in;ostream&operator(ostream& out,Merchandise& s) outMerchandise:endl; outno:s.noendl; outname:s.nameendl; outprice:s.pricemer; coutmer

19、; return 0;实验结果:4.编写一个程序,将两个文本文件连接成一个文件,然后将此文件中所有小写字母转换成大写字母,并打印出来。代码:#include#includeusing namespace std;int main() char ch; ifstream file1; ifstream file2; ofstream file3; file1.open(xyz.txt,ios:in); file2.open(qwe.txt,ios:in); file3.open(asd.txt,ios:out); if(!file1 | !file2) coutxyz.txt cannot op

20、enendl; abort(); file1.seekg(0); file2.seekg(0); while(!file1.eof() file1.get(ch); if(ch=97) ch = ch-32; file3.put(ch); coutch; while(!file2.eof() file2.get(ch); if(ch=97) ch = ch-32; file3.put(ch); coutch; file1.close(); file2.close(); file3.close(); return 0;实验结果: 3、实验总结:通过这次试验,我学习掌握了C+格式化的输入输出方法,

21、掌握了重载运算符“”的方法,掌握了磁盘文件的输入输出方法,能够更加熟悉地运用Visual C+ 6.0的系统。 任课教师签名: 实验七 Windows程序框架及MFC编程(2学时)学号:2012013854 姓名:柯冬生 班级:计科1202班 成绩:实验名称:Windows程序框架及MFC编程实验地点:综合楼207所使用的工具软件及环境:Microsoft Visual C+ 6.0一、实验目的:1学习windows基于消息事件驱动方式的程序设计2学习一个完整的窗口编制的四个操作步骤3了解Windows提供的窗口类4了解回调函数5学习MFC对话框和单文档编程的一般方法5学习使用MSDN帮助解决

22、问题二、实验内容:1写一个Win32 Application应用程序,要求当点击关闭时,弹出对话框确定关闭,否则不关闭。运行初始时,在文档窗口显示“Hello”。代码:/-初始化BOOL CTestDlg:OnInitDialog() CDialog:OnInitDialog(); / Add About. menu item to system menu. / IDM_ABOUTBOX must be in the system command range. ASSERT(IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX

23、AppendMenu(MF_SEPARATOR); pSysMenu-AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); / Set the icon for this dialog. The framework does this automatically / when the applications main window is not a dialog SetIcon(m_hIcon, TRUE); / Set big icon SetIcon(m_hIcon, FALSE); / Set small icon / TODO: Add extr

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1