已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验 5 异常处理 实验课程名:面向对象程序设计(C+方向)专业班级: 学号: 姓名: 实验时间: 实验地点: 指导教师: 一、实验目的和要求 (1)正确理解模板的概念。 (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 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;2.编写一个求任意类型数组中最大元素和最小元素的程序,要求将求最大元素和最小元素的函数设计成函数模板。程序代码:#includeusing namespace std;templateT maxin(T a,int n)T max=a0;int i;for(i=1;in;i+)if(maxai) max=ai;cout数组中最大值为:maxendl;T min=a0;for(i=1;iai) min=ai;cout数组中最小值为:minendl;return 0;int main()int a100;int i,n;coutn;for(i=0;iai;maxin(a,n);return 0;3.编写一个程序,使用类模板对数组元素进行排序、倒置、查找和求和。【提示】设计一个类模板template class Array .;具有对数组元素进行排序、倒置、查找和求和功能,然后产生类型实参分别为int型和double型的两个模板类,分别对整型数组与双精度数组完成所要求的操作。程序代码:#includeusing namespace std;templateclass Arraypublic:Array(int x)n=x;numtype sort();numtype find();numtype reserve();numtype accumulate();numtype display();numtype input();private:numtype s100;numtype n;/排序templatenumtype Array:sort()int i,j;for(i=0;in-1;i+)for(j=i+1;jsj)numtype t;t=si;si=sj;sj=t;return 0;templatenumtype Array:find()int i;numtype j;coutj;for(i=0;in;i+)if(si=j)cout所查找的元素为:siendl;return 0;cout没有找到。endl;return 0;templatenumtype Array:reserve()int i,j=0;numtype m;numtype a100;for(i=n-1;i=0;i-)m=si;aj=m;j+;for(i=0;in;i+)si=ai;return 0;templatenumtype Array:accumulate()int i;numtype max=0;for(i=0;in;i+)max=max+si;cout求和为:maxendl;return 0;templatenumtype Array:display()int i;for(i=0;in;i+)coutsi ;return 0;templatenumtype Array:input()int i;cout请输入n个数:endl;for(i=0;isi;return 0;int main()int i;Array a(5);a.input();a.sort();cout排序后为:; a.display(); coutendl;a.find();a.reserve();cout倒置后为:; a.display(); coutendl;a.accumulate();coutendl;Array b(5);b.input();b.sort();cout排序后为:; b.display(); coutendl;b.find();b.reserve();cout倒置后为:; b.display(); coutendl;b.accumulate();coutendl;return 0;4.编写一个程序,求输入数的平方根。设置异常处理,对输入负数的情况给出提示。#include#includeusing namespace std;double pfg(double a)double x;x=sqrt(a);return x;int main()double a;co
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 园艺工操作技能知识考核试卷含答案
- 钢琴键盘机械制作工常识模拟考核试卷含答案
- 炼油树脂工安全实践竞赛考核试卷含答案
- 焊接设备操作工测试验证竞赛考核试卷含答案
- 智能硬件装调员QC管理强化考核试卷含答案
- 养鸡工操作知识测试考核试卷含答案
- 沥青装置操作工岗前客户关系管理考核试卷含答案
- 金属材酸洗工岗前技能综合实践考核试卷含答案
- 水土保持监测工安全风险能力考核试卷含答案
- 选煤工安全教育考核试卷含答案
- 销售区域承包合同条款3篇
- 气胸的教学查房
- 幼儿园小班音乐歌唱《碰一碰》课件
- 2025届高考语文复习:文言文阅读 课件
- 中医诊疗技术操作规程
- CJT 340-2016 绿化种植土壤
- 2024年高考国家教育考试考务人员 数字化网上学习培训考试题库及答案 高考监考员在线考试试卷及答案
- 仓库出入库流程管理规范
- GB/T 7939.1-2024液压传动连接试验方法第1部分:管接头
- 二年级上册口算练习1000道
- 2023年11月浙江省慈溪技师学院(慈溪杭州湾中等职业学校)公开招聘1名派遣制工作人员笔试历年高频考点-难、易错点荟萃附答案带详解
评论
0/150
提交评论