您好,欢迎访问三七文档
当前位置:首页 > 财经/贸易 > 资产评估/会计 > 第十章类模板课后编程题目答案
1。设计一个类模板,其中包含数据成员Ta[n]以及对其进行排序的成员函数Sort(),模板参数T可实例化成字符串。解;如果T仅仅可以实例化成基本数据类型与char*类型的字符串,可通过重载sort()实现,程序如下:#includeiostream.h#includestring.htemplateclassT,intnclassArray{private:Ta[n];public:voidsort(double){for(inti=0;in-1;i++)for(intj=i+1;jn;j++)if(a[i]a[j]){Tt;t=a[i];a[i]=a[j];a[j]=t;}}voidsort(char*){for(inti=0;in-1;i++)for(intj=i+1;jn;j++)if(a[j]==NULL||(a[i]!=NULL&&strcmp(a[i],a[j])0)){Tt;t=a[i];a[i]=a[j];a[j]=t;}}voiddisp(){for(inti=0;in-1;i++)if(a[i]!=NULL)couta[i],\t;elsecout,\t;if(a[i]!=NULL)couta[i]endl;elsecoutendl;}Array(Ta[]){for(inti=0;in;i++)this-a[i]=a[i];}};voidmain(){floatf[]={1.2,2.3,7.7,4,3,4,2.3};Arrayfloat,6a1(f);a1.sort(f[0]);a1.disp();char*a[6]={a,abc,ABC,abcd};Arraychar*,6a2(a);a2.sort(a[0]);a2.disp();charc[]={'a','b','C','2'};Arraychar,6a3(c);a3.sort(c[0]);a3.disp();}如果T的类型可以实例化成自定义的字符串类String类型,需要重载赋值运算符=,插入运算符,比较运算符,分别用于进行字符串赋值,输出,比较。程序如下:#includeiostream#includestringusingnamespacestd;classString{private:char*Str;intlen;public:String(intn=0){Str=NULL;len=n;}String(constchar*p){len=strlen(p);Str=NULL;if(len!=0){Str=newchar[len+1];strcpy(Str,p);}}String(String&r){len=r.len;if(len0){Str=newchar[len+1];strcpy(Str,r.Str);}}~String(){if(len0)delete[]Str;}String&operator=(constString&r){if(len0)delete[]Str;Str=NULL;len=r.len;if(len0){Str=newchar[len+1];strcpy(Str,r.Str);}return*this;}booloperator(constString&r){if(Str==NULL)returnfalse;if(Str!=NULL&&r.Str==NULL)returntrue;intn=(lenr.len?len:r.len);for(inti=0;in;i++){if(Str[i]r.Str[i])returnfalse;if(Str[i]r.Str[i])returntrue;}returnfalse;}friendostream&operator(ostream&output,constString&s){if(s.Str!=NULL)outputs.Str;returnoutput;}};templateclassT,intnclassArray{private:Ta[n];public:voidsort(){for(inti=0;in-1;i++)for(intj=i+1;jn;j++)if(a[i]a[j]){Tt;t=a[i];a[i]=a[j];a[j]=t;}}voiddisp(){for(inti=0;in-1;i++)couta[i],\t;couta[i]endl;}Array(Ta[]){for(inti=0;in;i++)this-a[i]=a[i];}};voidmain(){floatf[]={1.2,2.3,7.7,4,3,4,2.3};Arrayfloat,6a1(f);a1.sort();a1.disp();Stringa[6]={a,abc,ABC,abcd};ArrayString,6a2(a);a2.sort();a2.disp();}2.设计一个类模板,其中包括数据成员Ta[n]以及在其中进行查找数据元素的函数intsearch(T)模板参数T可实例化成字符串。#includeiostream#includestringusingnamespacestd;classString{private:char*Str;intlen;public:String(intn=0){Str=NULL;len=n;}String(constchar*p){len=strlen(p);Str=NULL;if(len!=0){Str=newchar[len+1];strcpy(Str,p);}}String(String&r){len=r.len;if(len0){Str=newchar[len+1];strcpy(Str,r.Str);}}~String(){if(len0)delete[]Str;}String&operator=(constString&r){if(len0)delete[]Str;Str=NULL;len=r.len;if(len0){Str=newchar[len+1];strcpy(Str,r.Str);}return*this;}booloperator==(constString&r){if(len!=r.len)returnfalse;if(len==0)returntrue;for(inti=0;ilen;i++)if(Str[i]!=r.Str[i])returnfalse;returntrue;}friendostream&operator(ostream&output,constString&s){if(s.Str!=NULL)outputs.Str;returnoutput;}};templateclassT,intnclassArray{private:Ta[n];public:intsearch(Tkey){for(inti=0;in;i++)if(a[i]==key)returni;return-1;}voiddisp(){for(inti=0;in-1;i++)couta[i],\t;couta[i]endl;}Array(Ta[]){for(inti=0;in;i++)this-a[i]=a[i];}};voidmain(){floatf[]={1.2,2.3,7.7,4,3,4,2.3};Arrayfloat,6a1(f);couta1.search(2.3)endl;Stringa[6]={a,abc,ABC,abcd};ArrayString,6a2(a);couta2.search();}3.完善本章的Student类模板,使之可以添加,删除,查询学生记录,对学生成绩进行排序。#includeiostream#includestringusingnamespacestd;classString{private:char*Str;intlen;public:String(intn=0){Str=NULL;len=n;}String(constchar*p){len=strlen(p);Str=NULL;if(len!=0){Str=newchar[len+1];strcpy(Str,p);}}String(String&r){len=r.len;if(len0){Str=newchar[len+1];strcpy(Str,r.Str);}}~String(){if(len0)delete[]Str;}String&operator=(constString&r){if(len0)delete[]Str;Str=NULL;len=r.len;if(len0){Str=newchar[len+1];strcpy(Str,r.Str);}return*this;}booloperator==(constString&r){if(len!=r.len)returnfalse;if(len==0)returntrue;for(inti=0;ilen;i++)if(Str[i]!=r.Str[i])returnfalse;returntrue;}friendostream&operator(ostream&output,constString&s){if(s.Str!=NULL)outputs.Str;returnoutput;}};templateclassTNO,classTScore,intnumclassStudent{private:intn;TNOStudentID[num];TScorescore[num];public:voidappend(TNOID,TScores);voidDelete(TNOID);intsearch(TNO);voidsort();voidDispAll();Student(){n=0;}};templateclassTNO,classTScore,intnumvoidStudentTNO,TScore,num::append(TNOID,TScores){if(nnum){StudentID[n]=ID;score[n]=s;n++;}}templateclassTNO,classTScore,intnumvoidStudentTNO,TScore,num::Delete(TNOID){for(inti=0;in;i++)if(StudentID[i]==ID){for(intj=i;jn;j++){StudentID[j]=StudentID[j+1];score[j]=score[j+1];}n--;}}templateclassTNO,classTScore,intnumintStudentTNO,TScore,num::search(TNOno){for(inti=0;in;i++)if(StudentID[i]==no)returni+1;return0;}templateclassTNO,classTScore,intnumvoidStudentTNO,TScore,num::sort(){for(inti=0;in-1;i++)for(intj=i+1;jn;j++)if(score[i]score[j]){TScorets;TNOtn;ts=score[i];tn=StudentID[i];score[i]=score[j];StudentID[i]=StudentID[j];score[j]=ts;StudentID[j]=tn;}}templateclassTNO,classTScore,intnumvoidStudentTNO,TScore,num::DispA
本文标题:第十章类模板课后编程题目答案
链接地址:https://www.777doc.com/doc-5178175 .html