实验五模板与异常处理

上传人:枫** 文档编号:487426517 上传时间:2023-03-15 格式:DOC 页数:23 大小:515.42KB
返回 下载 相关 举报
实验五模板与异常处理_第1页
第1页 / 共23页
实验五模板与异常处理_第2页
第2页 / 共23页
实验五模板与异常处理_第3页
第3页 / 共23页
实验五模板与异常处理_第4页
第4页 / 共23页
实验五模板与异常处理_第5页
第5页 / 共23页
点击查看更多>>
资源描述

《实验五模板与异常处理》由会员分享,可在线阅读,更多相关《实验五模板与异常处理(23页珍藏版)》请在金锄头文库上搜索。

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

2、理方法。二、实验内容: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 namespace

3、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:b; int mai

4、n() 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) T min=x0;

6、 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)endl; retur

7、n 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; /template vo

8、id 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=0;mlen;m

9、+) 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 c5=1,8,2

10、,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(); return

11、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; 实验结果:三、 实验总结:1.在使用非模板函数时要注意输入的语句应符合函数的类型

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 试题/考题 > 初中试题/考题

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号