C++实验五参考模板_第1页
C++实验五参考模板_第2页
C++实验五参考模板_第3页
C++实验五参考模板_第4页
C++实验五参考模板_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

1、实验 5 异常处理 实验课程名:面向对象程序设计(C+方向)专业班级: 学号: 姓名: 实验时间: 实验地点: 指导教师: 一、实验目的和要求 (1)正确理解模板的概念。 (2)掌握函数模板和类模板的声明和使用方法。 (3)学习简单的异常处理方法。二、实验内容 1.分析并调试下列程序,写出运行结果并分析原因。 (1) /test6_1_1.cpp #include <iostream> using namespace std; template<typename T> T max (T x,T y) return x>y? x:y;int max(int a,in

2、t b)return a>b? a:b;double max (double a,double b)return a>b? a:b;int main() cout<<”max(3,7) is “<<max(3,7)<<endl; return 0;(2)/test6_1_2.cpp #include <iostream>using namespace std;int max(int a,int b)return a>b? a:b;double max (double a,double b)1 / 7return a>b?

3、a:b;int main() cout<<”max(3,7) is “<<max(3,7)<<endl; return 0;2.编写一个求任意类型数组中最大元素和最小元素的程序,要求将求最大元素和最小元素的函数设计成函数模板。程序代码:#include<iostream>using namespace std;template<typename T>T maxin(T a,int n)T max=a0;int i;for(i=1;i<n;i+)if(max<ai) max=ai;cout<<"数组中最

4、大值为:"<<max<<endl;T min=a0;for(i=1;i<n;i+)if(min>ai) min=ai;cout<<"数组中最小值为:"<<min<<endl;return 0;int main()int a100;int i,n;cout<<"请输入数组个数:"cin>>n;for(i=0;i<n;i+)cin>>ai;maxin(a,n);return 0;3.编写一个程序,使用类模板对数组元素进行排序、倒置、查找

5、和求和。【提示】设计一个类模板template <class Type>class Array .;具有对数组元素进行排序、倒置、查找和求和功能,然后产生类型实参分别为int型和double型的两个模板类,分别对整型数组与双精度数组完成所要求的操作。程序代码:#include<iostream>using namespace std;template<class numtype>class Arraypublic:Array(int x)n=x;numtype sort();numtype find();numtype reserve();numtype a

6、ccumulate();numtype display();numtype input();private:numtype s100;numtype n;/排序template<class numtype>numtype Array<numtype>:sort()int i,j;for(i=0;i<n-1;i+)for(j=i+1;j<n;j+)if(si>sj)numtype t;t=si;si=sj;sj=t;return 0;template<class numtype>numtype Array<numtype>:fin

7、d()int i;numtype j;cout<<"请输入要查找的元素:"cin>>j;for(i=0;i<n;i+)if(si=j)cout<<"所查找的元素为:"<<si<<endl;return 0;cout<<"没有找到。"<<endl;return 0;template<class numtype>numtype Array<numtype>:reserve()int i,j=0;numtype m;numtyp

8、e a100;for(i=n-1;i>=0;i-)m=si;aj=m;j+;for(i=0;i<n;i+)si=ai;return 0;template<class numtype>numtype Array<numtype>:accumulate()int i;numtype max=0;for(i=0;i<n;i+)max=max+si;cout<<"求和为:"<<max<<endl;return 0;template<class numtype>numtype Array<

9、numtype>:display()int i;for(i=0;i<n;i+)cout<<si<<" "return 0;template<class numtype>numtype Array<numtype>:input()int i;cout<<"请输入"<<n<<"个数:"<<endl;for(i=0;i<n;i+)cin>>si;return 0;int main()int i;Array<in

10、t> a(5);a.input();a.sort();cout<<"排序后为:" a.display(); cout<<endl;a.find();a.reserve();cout<<"倒置后为:" a.display(); cout<<endl;a.accumulate();cout<<endl;Array<double> b(5);b.input();b.sort();cout<<"排序后为:" b.display(); cout<&l

11、t;endl;b.find();b.reserve();cout<<"倒置后为:" b.display(); cout<<endl;b.accumulate();cout<<endl;return 0;4.编写一个程序,求输入数的平方根。设置异常处理,对输入负数的情况给出提示。#include<iostream>#include<cmath>using namespace std;double pfg(double a)double x;x=sqrt(a);return x;int main()double a;cout<<"请输入一个数:"cin>>a;trywhile(a>=0)cout<<"平方根为:"<<pfg(a)<<endl;cout<<"请输入一个数:&

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论