📄 cjj126.htm
字号:
width="85%" class="body18black">C++习题与解析(模板-03)</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" >
题 5. 一个Sample类模板的私有数据成员为T n,在该类模板中设计一个operator==重载运算符函数 ,用于比较各对象的n数据是否相等。<BR></FONT>解:<BR> 本题程序如下:<BR>#include<iostream.h><BR>template <class T><BR>class Sample<BR>{<BR> T n;<BR> public:<BR> Sample(T i){n=i;}<BR> int operator==(Sample &);<BR>};<BR>template <class T><BR>int Sample<T>::operator==(Sample &s)<BR>{<BR> if(n==s.n)<BR> return 1;<BR> else<BR> return 0;<BR>}<BR>void main()<BR>{<BR> Sample<int> s1(2),s2(3);<BR> cout<<"s1与s2的数据成员"<<(s1==s2?"相等":"不相等")<<endl;<BR> Sample<double>s3(2.5),s4(2.5);<BR> cout<<"s3与s4的数据成员"<<(s3==s4?"相等":"不相等")<<endl;<BR>}<BR>本程序的运行结果如下:<BR>s1与s2的数据成员不相等<BR>s3与S4的数据成员相等<BR><BR>----------------------------------------------------<BR><BR><FONT color=#0000ff>题 6. 对第3章的例3.5进行修改,只设计一个Sample类,其数据和方法均包含在该类中,而且使用类模板的方式实现。<BR></FONT>#include<iostream.h><BR>#define Max 100<BR>template <class T><BR>class Sample<BR>{<BR> T A[Max];<BR> int n;<BR> void qsort(int l,int h); // 私有成员,由quicksort()成员调用<BR> public:<BR> Sample(){n=0;}<BR> void getdata(); // 获取数据<BR> void insertsort(); // 插入排序<BR> void Shellsort(); // 希尔排序<BR> void bubblesort(); // 冒泡排序<BR> void quicksort(); // 快速排序<BR> void selectsort(); // 选择排序<BR> void disp();<BR>};<BR>template <class T><BR>void Sample<T>::getdata()<BR>{<BR> cout<<"元素个数:";<BR> cin>>n;<BR> for(int i=0;i<n;i++)<BR> {<BR> cout<<"输入第"<<i+1<<"个数据:";<BR> cin>>A[i];<BR> }<BR>}<BR>template <class T><BR>void Sample<T>::insertsort() // 插入排序<BR>{<BR> int i,j;<BR> T temp;<BR> for(i=1;i<n;i++)<BR> {<BR> temp=A[i];<BR> j=i-1;<BR> while(temp<A[j])<BR> {<BR> A[j+1]=A[j];<BR> j--;<BR> }<BR> A[j+1]=temp;<BR> }<BR>}<BR>template <class T><BR>void Sample<T>::Shellsort() // 希尔排序<BR>{<BR> int i,j,gap;<BR> T temp;<BR> gap=n/2;<BR> while(gap>0)<BR> {<BR> for(i=gap;i<n;i++)<BR> {<BR> j=i-gap;<BR> while(j>=gap)<BR> if(A[j]>A[j+gap])<BR> {<BR> temp=A[j];<BR> A[j]=A[j+gap];<BR> A[j+gap]=temp;<BR> j=j-gap;<BR> }<BR> else j=0;<BR> }<BR> gap=gap/2;<BR> }<BR>}<BR>template <class T><BR>void Sample<T>::bubblesort() // 冒泡排序<BR>{<BR> int i,j;<BR> T temp;<BR> for(i=0;i<n;i++)<BR> for(j=n-1;j>=i+1;j--)<BR> if(A[j]<A[j-1])<BR> {<BR> temp=A[j];<BR> A[j]=A[j-1];<BR> A[j-1]=temp;<BR> }<BR>}<BR>template <class T><BR>void Sample<T>::quicksort() // 快速排序<BR>{<BR> qsort(0,n-1);<BR>}<BR>template<class T><BR>void Sample<T>::qsort(int l,int h)<BR>{<BR> int i=l,j=h;<BR> T temp;<BR> if(l<h)<BR> {<BR> temp=A[l];<BR> do{<BR> while(j>i&&A[j]>=temp)<BR> j--;<BR> if(i<j)<BR> {<BR> A[i]=A[j];<BR> i++;<BR> }<BR> while(i<j&&A[i]<=temp)<BR> i++;<BR> if(i<j)<BR> {<BR> A[j]=A[i];<BR> j--;<BR> }<BR> }while(i<j);<BR> A[i]=temp;<BR> qsort(1,j-1);<BR> qsort(j+1,h);<BR> }<BR>}<BR>template <class T><BR>void Sample<T>::selectsort() // 选择排序<BR>{<BR> int i,j,k;<BR> T temp;<BR> for(i=0;i<n;i++)<BR> {<BR> k=i;<BR> for(j=i+1;j<=n-1;j++)<BR> if(A[j]<A[k])<BR> k=j;<BR> temp=A[i];<BR> A[i]=A[k];<BR> A[k]=temp;<BR> }<BR>}<BR>template <class T><BR>void Sample<T>::disp()<BR>{<BR> for(int i=0;i<n;i++)<BR> cout<<A[i]<<" ";<BR> cout<<endl;<BR>}<BR>void main()<BR>{<BR> int sel=0;<BR> Sample<char> s; // 由类模板产生char型的模板类<BR> s.getdata();<BR> cout<<"原来序列:";<BR> s.disp();<BR> cout<<"0:插入排序 1:希尔排序 2:冒泡排序 3:快速排序\n 4:选择排序 其它退出"<<endl;<BR> cout<<"选择排序方法:";<BR> cin>>sel;<BR> switch(sel)<BR> {<BR> case 0:<BR> s.insertsort();<BR> cout<<"插入排序结果";<BR> break;<BR> case 1:<BR> s.Shellsort();<BR> cout<<"希尔排序结果:";<BR> break;<BR> case 2:<BR> s.bubblesort();<BR> cout<<"冒泡排序结果:";<BR> break;<BR> case 3:<BR> s.quicksort();<BR> cout<<"快速排序结果:";<BR> break;<BR> case 4:<BR> s.selectsort();<BR> cout<<"选择排序结果:";<BR> break;<BR> }<BR> s.disp();<BR>}<BR>程序运行结果如下:</DIV><BR>
<P align=left><P align=center><IMG onload='javascript:if(this.width>screen.width-333)this.width=screen.width-333' src="http://www.cstudyhome.com/wenzhang06/uploadpic/20034201043189404.gif"></P>
<DIV> <BR>----------------------------------------------------<BR><BR><FONT color=#0000ff>题 7. 设计一个模板类Sample,用于对一个有序数组采用二分法查找元素下标。</FONT><BR>解:<BR>#include<iostream.h><BR>#define Max 100<BR>template <class T><BR>class Sample<BR>{<BR> T A[Max];<BR> int n;<BR> public:<BR> Sample(){}<BR> Sample(T a[],int i);<BR> int seek(T c);<BR> void disp()<BR> {<BR> for(int i=0;i<n;i++)<BR> cout<<A[i]<<" ";<BR> cout<<endl;<BR> }<BR>};<BR>template <class T><BR>Sample<T>::Sample(T a[],int i)<BR>{<BR> n=i;<BR> for(int j=0;j<i;j++)<BR> A[j]=a[j];<BR>}<BR>template <class T><BR>int Sample<T>::seek(T c)<BR>{<BR> int low=0,high=n-1,mid;<BR> while(low<=high)<BR> {<BR> mid=(low+high)/2;<BR> if(A[mid]==c)<BR> return mid;<BR> else if(A[mid]<c) low=mid+1;<BR> else high=mid-1;<BR> }<BR> return -1;<BR>}<BR>void main()<BR>{<BR> char a[]="acegkmpwxz";<BR> Sample<char>s(a,10);<BR> cout<<"元素序列:"; s.disp();<BR> cout<<"'g'的下标:"<<s.seek('g')<<endl;<BR>}<BR>程序运行结果如下:<BR>元素序列:a c e g k m p w x z<BR>元素'g'的下标: 3</DIV><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 + -