5544、
題目內容:
P232 例8.3
定义函数模板求未知类型一维数组的最大值。
主函数将函数模板实例化求出 int int_array[]={11,22,33,44,55,66,77,88,99,1010};
double double_array[]={11.1,22.2,33.3,44.4,55.5,66.6,77.7,88.8,99.9,100.10};两个数组的最大值并输出。输入输出说明:
输出: The int_max is 1010 The double_max is 100.1#include<iostream> #include<cmath> using namespace std; template <class T>T getmax(T a[]){ for(int i=0;i<10;i++){ if(a[0]<a[i]){ a[0]=a[i]; } } return a[0]; } int main(){ int int_array[]={11,22,33,44,55,66,77,88,99,1010}; double double_array[]={11.1,22.2,33.3,44.4,55.5,66.6,77.7,88.8,99.9,100.10}; cout<<"The int_max is "<<getmax(int_array)<<endl; cout<< "The double_max is "<<getmax(double_array)<<endl; }
5545、
題目內容:
P232 例8.4
定义带有两个参数的函数模板,实现这两个不同类型数据的输出
输入输出说明:
输出: 99 zhang 123.45 888#include<iostream> using namespace std; template <class T1, class T2>void output(T1 a,T2 b){ cout<<a<<" "<<b<<endl; } int main(){ output(99,"zhang"); output(123.45,888); }
5546、
題目內容:
P236 例8.8
定义含有三个相同类型私有数据成员的类模板,类模版中有成员函数实现三个数据的求和运算。此成员函数要求在类外进行定义。
定义模板类对象求两组数据的和。分别为(3,5,7)和(12.34,34.56,56.78)。
输入输出说明:
输出: The three_int sum is 15 The three_double sum is 103.68
#include<iostream> using namespace std; template <class T> class sum{ private: T a,b,c; public: sum(T a,T b,T c){ this->a=a; this->b=b; this->c=c; } T getsum(); }; template<class T> T sum<T>::getsum(){ return this->a+this->b+this->c; } int main(){ int intsum=0; double doublesum=0; sum<int> s1(3,5,7); intsum=s1.getsum(); cout<<"The three_int sum is "<<intsum<<endl; sum<double> s2(12.34,34.56,56.78); doublesum=s2.getsum(); cout<<"The three_double sum is "<<doublesum<<endl; }
5536、
題目內容:
写一个函数模板,求数组中的最大元素,并且使得函数调用时,数组的类型和返回类型可以是整数也可以是双精度类型。
输入输出说明:
主函数中定义2个一维数组,一个为整型的,初值为2,4,7,1,9,4,2,6,3,1 另一个为双精度浮点类型的,初值为2.2,4.4,7.8,1.4,9.9,4.6,2.3,6.6,3.1,1.3 int max= 9 double max= 9.9#include<iostream> #include<cmath> using namespace std; template <class T>T getmax(T a[]){ for(int i=0;i<10;i++){ if(a[0]<a[i]){ a[0]=a[i]; } } return a[0]; } int main(){ int int_array[]={2,4,7,1,9,4,2,6,3,1}; double double_array[]={2.2,4.4,7.8,1.4,9.9,4.6,2.3,6.6,3.1,1.3}; cout<<"int max= "<<getmax(int_array)<<endl; cout<<"double max= "<<getmax(double_array)<<endl; }
5537、
題目內容:
写一个函数模板,使用冒泡排序将数组内容由小到大排列,并且使得函数调用时,数组的类型可以是整数也可以是双精度型。
输入输出说明:
整型一维数组的初值为2,4,7,10,9,5,8,6,3,1 双精度浮点类型数组的初值为2.2,4.4,7.8,1.4,9.9,4.6,2.3,6.6,3.1,1.3 输出: 1 2 3 4 5 6 7 8 9 10 1.3 1.4 2.2 2.3 3.1 4.4 4.6 6.6 7.8 9.9
#include<iostream> using namespace std; template<class T> void sort(T a[]){ for(int i=0;i<10;i++){ for(int j=0;j<9;j++){ if(a[j]>a[j+1]){ T t=0; t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } for(int i=0;i<10;i++){ cout<<a[i]<<" "; } cout<<endl; } int main(){ int a[]={2,4,7,10,9,5,8,6,3,1}; double b[]={2.2,4.4,7.8,1.4,9.9,4.6,2.3,6.6,3.1,1.3}; sort(a); sort(b); }
5538、
題目內容:
建立类模板input,在调用构造函数时,完成以下工作:
(1) 提示用户输入;
(2) 让用户输入数据;
(3) 如果数据不在预定范围内,重新提示输入。
input型的对象应当按以下形式定义:
input ob("promput message”,min_value,max_value)
其中,promput message是提示输入信息。可接受的最小值和最大值分别由min_value与max_value指定。
输入输出说明:
如果数据在给定的范围之内,则正确输出数据,否则提示重新输入 输入: 1 9 3 a z f 输出: 3 f
#include<iostream> using namespace std; template <class T> class input{ T min_value,max_value; public: input(T min_value,T max_value){ this->max_value=max_value; this->min_value=min_value; } void output(T a){ if(min_value<=a&&a<=max_value){ cout<<a<<endl; } else{ cout<<"数据不符合范围,请重新输入。"; } } }; int main(){ int x,y,z; cin>>x>>y>>z; input< int> in1(x,y); in1.output(z); char a,b,c; cin>>a>>b>>c; input < char> in2(a,b); in2.output(c); }