📄 cjj124.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>C++习题与解析(模板-01)</TITLE>
<META content="text/html; charset=gb2312" http-equiv=Content-Type>
<SCRIPT language=JavaScript>
var currentpos,timer;
function initialize()
{
timer=setInterval("scrollwindow()",50);
}
function sc(){
clearInterval(timer);
}
function scrollwindow()
{
currentpos=document.body.scrollTop;
window.scroll(0,++currentpos);
if (currentpos != document.body.scrollTop)
sc();
}
document.onmousedown=sc
document.ondblclick=initialize
</SCRIPT>
<META content="MSHTML 5.00.2614.3500" name=GENERATOR>
<link rel="stylesheet" href="body1.css" type="text/css">
</HEAD>
<BODY topMargin=0 marginheight="0" bgcolor="#CCCCCC">
<TABLE align=center border=1 cellPadding=0 cellSpacing=1
style="BORDER-COLLAPSE: collapse" width=550>
<TBODY>
<TR>
<TD bgColor=#c1c1c1 height=35> <img src="jsdd.gif" width="159" height="57" > <img src="jjdd.gif" ></TD>
</TR>
</TBODY>
</TABLE>
<TABLE align=center border=1 cellPadding=0 cellSpacing=1
style="BORDER-COLLAPSE: collapse" width=550>
<TBODY>
<TR>
<TD width="100%">
<TABLE border=0 borderColor=#e2ca9f cellPadding=0 cellSpacing=0
width="100%">
<TBODY>
<TR>
<TD align=middle vAlign=top width="95%">
<TABLE border=1 borderColor=#e2ca9f cellPadding=0 cellSpacing=0
width="100%">
<TBODY>
<TR>
<TD align=middle background=002.gif
borderColor=#e2ca9f vAlign=top width="69%">
<TABLE align=center border=0 cellPadding=0 cellSpacing=0
width="100%">
<TBODY>
<TR>
<TD height=35 width="100%"></TD>
</TR>
<TR>
<TD align=middle bgColor=#dddddd height=20
style="FONT-SIZE: 18px" vAlign=bottom
width="85%" class="body18black">C++习题与解析(模板-01)</TD>
<BR>
</TR>
<TR>
<TD align=middle width="100%"><BR>
</TD>
</TR>
<TR>
<TD align=middle width="100%">
<!--下面的这一句是设置阅读文本区的宽度-->
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width="75%">
<TBODY>
<TR>
<TD align=middle width="100%"></TD>
</TR>
<TR>
<TD class="body12black" >01.分析以下程序的执行结果</FONT><BR>#include<iostream.h><BR>template <class T><BR>T max(T x,T y)<BR>{<BR> return (x>y?x:y);<BR>}<BR>void main()<BR>{<BR> cout<<max(2,5)<<","<<max(3.5,2.8)<<endl;<BR>}<BR>解:<BR> 本题说明函数模板的使用方法。max()函数是一个函数模板,它返回两个参数中的较大者。在调用时自动联编相应的max()函数。所以输出为:5,3.5<BR>注意:T为类型形式参数,可包含基本数据类型,也可以包含类类型,不能是普通的数据变量。<BR><BR>-------------------------------------------------------<BR><BR><FONT color=#0000ff>02.分析以下程序的执行结果</FONT><BR>#include<iostream.h><BR>template <class T><BR>class Sample<BR>{<BR> T n;<BR> public:<BR> Sample(T i){n=i;}<BR> void operator++();<BR> void disp(){cout<<"n="<<n<<endl;}<BR>};<BR>template <class T><BR>void Sample<T>::operator++()<BR>{<BR> n+=1; // 不能用n++;因为double型不能用++<BR>}<BR>void main()<BR>{<BR> Sample<char> s('a');<BR> s++;<BR> s.disp();<BR>}<BR>解:<BR> 本题说明类模板的使用方法。Sample是一个类模板,由它产生模板类Sample<char>,通过构造函数给n赋初值,通过重载++运算符使n增1,这里n由'a'增1变成'b'。<BR> 所以输出为:n=b<BR><BR>-------------------------------------------------------<BR><BR><FONT color=#0000ff>03.编写一个对具有n 个元素的数组x[]求最大值的程序,要求将求最大值的函数设计成函数模板。</FONT><BR>解:<BR> 将max()函数设计成一个函数模板。<BR>本题程序如下:<BR>#include<iostream.h><BR>template <class T><BR>T max(T x[],int n)<BR>{<BR> int i;<BR> T maxv=x[0];<BR> for(i=1;i<n;i++)<BR> if(maxv<x[i])<BR> maxv=x[i];<BR> return maxv;<BR>}<BR>void main()<BR>{<BR> int a[]={4,5,2,8,9,3};<BR> double b[]={3.5,6.7,2,5.2,9.2};<BR> cout<<"a数组最大值:"<<max(a,6)<<endl;<BR> cout<<"b数组最大值:"<<max(b,5)<<endl;<BR>}<BR>本程序的执行结果如下:<BR>a 数组最大值:9<BR>b 数组最大值:9.2 <BR><BR>-------------------------------------------------<BR><BR><FONT color=#0000ff>04.编写一个使用类模板对数组进行排序、查找和求元素和的程序。</FONT><BR>解:<BR> 设计一个类模板template<class T>class Array,用于对T类型的数组进行排序、查找和求元素和,然后由此产生模板类Array<int>和Array<double>。<BR>本题程序如下:<BR>#include<iostream.h><BR>#include<iomanip.h><BR>template <class T><BR>class Array<BR>{<BR> T *set;<BR> int n;<BR> public:<BR> Array(T *data,int i){set=data;n=i;}<BR> ~Array(){}<BR> void sort(); // 排序<BR> int seek(T key); // 查找指定的元素<BR> T sum(); // 求和<BR> void disp(); // 显示所有的元素<BR>};<BR>template<class T><BR>void Array<T>::sort()<BR>{<BR> int i,j;<BR> T temp;<BR> for(i=1;i<n;i++)<BR> for(j=n-1;j>=i;j--)<BR> if(set[j-1]>set[j])<BR> {<BR> temp=set[j-1];set[j-1]=set[j];set[j]=temp;<BR> }<BR>}<BR>template <class T><BR>int Array<T>::seek(T key)<BR>{<BR> int i;<BR> for(i=0;i<n;i++)<BR> if(set[i]==key)<BR> return i;<BR> return -1;<BR>}<BR>template<class T><BR>T Array<T>::sum()<BR>{<BR> T s=0;int i;<BR> for(i=0;i<n;i++)<BR> s+=set[i];<BR> return s;<BR>}<BR>template<class T><BR>void Array<T>::disp()<BR>{<BR> int i;<BR> for(i=0;i<n;i++)<BR> cout<<set[i]<<" ";<BR> cout<<endl;<BR>}<BR>void main()<BR>{<BR> int a[]={6,3,8,1,9,4,7,5,2};<BR> double b[]={2.3,6.1,1.5,8.4,6.7,3.8};<BR> Array<int>arr1(a,9);<BR> Array<double>arr2(b,6);<BR> cout<<" arr1:"<<endl;<BR> cout<<" 原序列:"; arr1.disp();<BR> cout<<" 8在arr1中的位置:"<<arr1.seek(8)<<endl;<BR> arr1.sort();<BR> cout<<" 排序后:"; arr1.disp();<BR> cout<<"arr2:"<<endl;<BR> cout<<" 原序列:"; arr2.disp();<BR> cout<<" 8.4在arr2中的位置:"<<arr2.seek(8.4)<<endl;<BR> arr2.sort();<BR> cout<<" 排序后:"; arr2.disp();<BR>}<BR>本程序的执行结果如下:<BR>arr1:<BR> 原序列:6 3 8 1 9 4 7 5 2<BR> 8在arr1中的位置:2<BR> 排序后:1 2 3 4 5 6 7 8 9<BR>arr2:<BR> 原序列:2.3 6.1 1.5 8.4 6.7 3.8<BR> 8.4在arr2中的位置:3<BR> 排序后:1.5 2.3 3.8 6.1 6.7 8.4<br>
</TD>
</TR>
</TBODY>
</TABLE>
</TD>
</TR>
<TR>
<TD height=35 width="100%">
<DIV align=center></DIV>
</TD>
</TR>
</TBODY>
</TABLE>
</TD></TR></TBODY>
</TABLE>
</TD></TR></TBODY>
</TABLE>
</TD></TR></TBODY>
</TABLE>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width=750>
<TBODY>
<TR>
<TD bgColor=#c1c1c1 height=1></TD>
</TR>
</TBODY>
</TABLE>
<TABLE align=center border=1 cellPadding=0 cellSpacing=1
style="BORDER-COLLAPSE: collapse" width=550>
<TBODY>
<td align=center>
<div align="center">
<p><a href=mailto:jjsun36@sohu.com ><span class="body14black">mailto:jjsun36@sohu.com</span></a><span class="body12red">
热线电话:0523-4864559</span></p>
</div>
</td>
</TBODY>
</TABLE>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -