第9章习题答案讲课教案.docx

上传人:b****3 文档编号:3847630 上传时间:2023-05-06 格式:DOCX 页数:16 大小:18.87KB
下载 相关 举报
第9章习题答案讲课教案.docx_第1页
第1页 / 共16页
第9章习题答案讲课教案.docx_第2页
第2页 / 共16页
第9章习题答案讲课教案.docx_第3页
第3页 / 共16页
第9章习题答案讲课教案.docx_第4页
第4页 / 共16页
第9章习题答案讲课教案.docx_第5页
第5页 / 共16页
第9章习题答案讲课教案.docx_第6页
第6页 / 共16页
第9章习题答案讲课教案.docx_第7页
第7页 / 共16页
第9章习题答案讲课教案.docx_第8页
第8页 / 共16页
第9章习题答案讲课教案.docx_第9页
第9页 / 共16页
第9章习题答案讲课教案.docx_第10页
第10页 / 共16页
第9章习题答案讲课教案.docx_第11页
第11页 / 共16页
第9章习题答案讲课教案.docx_第12页
第12页 / 共16页
第9章习题答案讲课教案.docx_第13页
第13页 / 共16页
第9章习题答案讲课教案.docx_第14页
第14页 / 共16页
第9章习题答案讲课教案.docx_第15页
第15页 / 共16页
第9章习题答案讲课教案.docx_第16页
第16页 / 共16页
亲,该文档总共16页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

第9章习题答案讲课教案.docx

《第9章习题答案讲课教案.docx》由会员分享,可在线阅读,更多相关《第9章习题答案讲课教案.docx(16页珍藏版)》请在冰点文库上搜索。

第9章习题答案讲课教案.docx

第9章习题答案讲课教案

 

第9章习题答案

第九章模板

1选择题

(1).关于函数模板,描述错误的是(A)。

(A)函数模板必须由程序员实例化为可执行的函数模板

(B)函数模板的实例化由编译器实现

(C)一个类定义中,只要有一个函数模板,则这个类是类模板

(D)类模板的成员函数都是函数模板,类模板实例化后,成员函数也随之实例化

(2).下列的模板说明中,正确的是(c)。

(A)template

(B)template

(C)template

(D)template(typedefT1,typedefT2)

(3).假设有函数模板定义如下:

 template

 Max(Ta,Tb,T&c)

{c =a+b;}

下列选项正确的是(B)。

(A)intx,y;charz;(B)doublex,y,z;

Max(x,y,z);Max(x,y,z);

(C)intx,y;floatz;(D)floatx;doubley,z;

Max(x,y,z);Max(x,y,z);

(4).关于类模板,描述错误的是(A)。

(A)一个普通基类不能派生类模板

(B)类模板可以从普通类派生,也可以从类模板派生

(C)根据建立对象时的实际数据类型,编译器把类模板实例化为模板类

(D)函数的类模板参数需生成模板类并通过构造函数实例化

(5).类模板的使用实际上是将类模板实例化成一个(C)。

A.函数B.对象C.类D.抽象类

(6).类模板的模板参数(D)。

A.只能作为数据成员的类型B.只可作为成员函数的返回类型

C.只可作为成员函数的参数类型D.以上三种均可

(7).类模板的实例化(A)。

A.在编译时进行B.属于动态联编

C.在运行时进行D.在连接时进行

(8).以下类模板定义正确的为(A)。

A.templateB.template

C.templateD.template

2阅读下列程序,写出执行结果

(1).

#include

usingnamespacestd;

template

voidfun(T&x,T&y)

{Ttemp;

temp=x;x=y;y=temp;

}

intmain()

{inti,j;

i=10;j=20;

fun(i,j);

cout<<"i="<

doublea,b;

a=1.1;b=2.2;

fun(a,b);

cout<<"a="<

}

【解答】

i=20j=10

a=2.2b=1.1

(2).

#include

usingnamespacestd;

template

classBase

{public:

Base(Ti,Tj){x=i;y=j;}

Tsum(){returnx+y;}

private:

Tx,y;

};

intmain()

{Baseobj2(3.3,5.5);

cout<

Baseobj1(3,5);

cout<

}

【解答】

8.8

8

3.抽象类和类模板都是提供抽象的机制,请分析它们的区别和应用场合。

【解答】

抽象类至少包含一个纯虚函数,纯虚函数抽象了类体系中一些类似操作的公共界面,它不依赖于数据,也没有操作定义。

派生类必须定义实现版本。

抽象类用于程序开发时对功能的统一策划,利用程序运行的多态性自动匹配实行不同版本的函数。

类模板抽象了数据类型,称为类属参数。

成员函数描述了类型不同,逻辑操作相同的功能集。

编译器用建立对象的数据类型参数实例化为模板类,生成可运行的实体。

类模板用于抽象数据对象类型不同,逻辑操作完全相同类定义。

这种数据类型的推导必须在语言功能的范畴之内的。

4.类模板能够声明什么形式的友员?

当类模板的友员是函数模板时,它们可以定义不同形式的类属参数吗?

请你写个验证程序试一试。

【解答】

类模板可以声明的友员形式有:

普通函数、函数模板、普通类成员函数、类模板成员函数以及普通类、类模板。

当类模板的友员是函数模板时,它们可以定义不同形式的类属参数。

程序略。

5.类模板的静态数据成员可以是抽象类型吗?

它们的存储空间是什么时候建立的?

请你用验证程序试一试。

【解答】

类模板的静态数据成员可以是抽象类型。

它们的存储空间在生成具体模板类的时候建立,即每生成一个模板类同时建立静态储存空间并做一次文件范围的初始化。

程序略。

6.设计一个函数模板,其中包括数据成员Ta[n]以及对其进行排序的成员函数sort(),模板参数T可实例化成字符串。

【解答】

#include

#include

usingnamespacestd;

templatevoidSort(T*a,intn){

inti,j;

Tt;

for(i=0;i

for(j=0;j

if(a[j]>a[j+1])

{t=a[j];

a[j]=a[j+1];

a[j+1]=t;

}

}

templatevoidPrint(T*a,intn){

inti;

for(i=0;i

cout<

cout<

}

intmain(){

stringStr[10]={"Zhang","Li","Wang","Qian","Zhao","Wu","Xu","Tang","Shen","Liang"};

intInt[8]={20,12,0,-5,9,-18,6,11};

doubleDou[7]={3.07,8.12,-0.45,6,10,-9,7.29};

Sort(Str,10);

Sort(Int,8);

Sort(Dou,7);

Print(Str,10);

Print(Int,8);

Print(Dou,7);

return0;

}

7.设计一个类模板,其中包括数据成员Ta[n]以及在其中进行查找数据元素的函数intsearch(T)模板参数T可实例化成字符串。

【解答】

#include

usingnamespacestd;

templateclassA{

intsize;

T*element;

public:

A();

~A();

intSearch(T);

voidSetElement(intindex,constT&value);

};

templateA:

:

A(){

size=n>1?

n:

1;

element=newT[size];

}

templateA:

:

~A(){

delete[]element;

}

templateintA:

:

Search(Tt){

inti;

for(i=0;i

if(element[i]==t)

returni;

return-1;

}

templatevoidA:

:

SetElement(intindex,constT&value){

element[index]=value;

}

intmain(){

AintAry;//用int实例化,建立模板类对象

AdouAry;//用double实例化,建立模板类对象

inti;

for(i=0;i<5;i++)

intAry.SetElement(i,i+3);

for(i=0;i<10;i++)

douAry.SetElement(i,(i+i)*0.35);

i=intAry.Search(7);

if(i>=0)cout<

i=douAry.Search(0.7);

if(i>=0)cout<

return0;

}

8.设计一个单向链表类模板,结点数据域中数据从小到大排列,并设计插入、删除节点的成员函数。

【解答】

#include

usingnamespacestd;

templateclassList;

templateclassNode{

Tinfo;//数据域

Node*link;//指针域

public:

Node();//生成头结点的构造函数

Node(constT&data);//生成一般结点的构造函数

friendclassList;

};

templateNode:

:

Node(){link=NULL;}

templateNode:

:

Node(constT&data){

info=data;

link=NULL;

}

//定义链表类

templateclassList{

Node*head;//链表头指针和尾指针

public:

List();//构造函数,生成头结点(空链表)

~List();//析构函数

voidMakeEmpty();//清空一个链表,只余表头结点

Node*Find(Tdata);//搜索数据域与data相同的结点,返回该结点的地址

voidPrintList();//打印链表的数据域

voidInsertOrder(Node*p);//按升序生成链表

Node*CreatNode(Tdata);//创建一个结点(孤立结点)

Node*DeleteNode(Node*p);//删除指定结点

};

templateList:

:

List(){

head=newNode(-9999);//头结点,最小的数据从小到大插入

}

templateList:

:

~List(){

MakeEmpty();

deletehead;

}

templatevoidList:

:

MakeEmpty(){

Node*tempP;

while(head->link!

=NULL){

tempP=head->link;

head->link=tempP->link;//把头结点后的第一个节点从链中脱离

deletetempP;//删除(释放)脱离下来的结点

}

}

templateNode*List:

:

Find(Tdata){

Node*tempP=head->link;

while(tempP!

=NULL&&tempP->info!

=data)tempP=tempP->link;

returntempP;//搜索成功返回该结点地址,不成功返回NULL

}

templatevoidList:

:

PrintList(){

Node*tempP=head->link;

while(tempP!

=NULL){

cout<info<<'\t';

tempP=tempP->link;

}

cout<

}

templatevoidList:

:

InsertOrder(Node*p){

Node*tempP=head,*tempQ=head;//tempQ指向tempP前面的一个节点

while(tempP!

=NULL){

if(p->infoinfo)break;//找第一个比插入结点大的结点,由tempP指向

tempQ=tempP;

tempP=tempP->link;

}

p->link=tempP;

tempQ->link=p;

}

templateNode*List:

:

CreatNode(Tdata){//建立新节点

Node*tempP=newNode(data);

returntempP;

}

templateNode*List:

:

DeleteNode(Node*p){

Node*tempP=head->link,*tempQ=head,*tempC;

while(tempP!

=NULL&&tempP!

=p){

tempQ=tempP;

tempP=tempP->link;

}

tempC=tempP;

tempQ->link=tempP->link;

returntempC;

}

intmain(){

Node*P1;

Listlist1;

inta[10]={20,12,0,-5,9,-18,6,11,5,3},i,j;

for(i=0;i<10;i++){

P1=list1.CreatNode(a[i]);

list1.InsertOrder(P1);

}

list1.PrintList();

cout<<"请输入一个要求删除的整数"<

cin>>j;

P1=list1.Find(j);

if(P1!

=NULL){

P1=list1.DeleteNode(P1);

deleteP1;

list1.PrintList();

}

elsecout<<"未找到"<

cout<<"请输入一个要求插入的整数"<

cin>>j;

P1=list1.CreatNode(j);

list1.InsertOrder(P1);

list1.PrintList();

list1.MakeEmpty();//清空list1

list1.PrintList();

return0;

}

9.为单链表类模板增加一个复制构造函数和赋值运算符(=)。

在上题基础上,List类增加一个复制构造函数和赋值运算符(=)

【解答】

templateList:

:

List(List&l){

head=newNode(-9999);//现建立头结点

Node*tempP=l.head->link,*tempC;

while(tempP!

=NULL){

tempC=CreatNode(tempP->info);

InsertAfter(tempC);

tempP=tempP->link;

}

}

templateList&List:

:

operator=(List&l){

MakeEmpty();//先释放原来链表的数据结点

Node*tempP=l.head->link,*tempC;

while(tempP!

=NULL){

tempC=CreatNode(tempP->info);

InsertAfter(tempC);

tempP=tempP->link;

}

return*this;

}

intmain(){

Node*P1;

Listlist1,list2;

inta[10]={20,12,0,-5,9,-18,6,11,5,3},i,j;

for(i=0;i<10;i++){

P1=list1.CreatNode(a[i]);

list1.InsertOrder(P1);

}

list1.PrintList();

cout<<"请输入一个要求删除的整数"<

cin>>j;

P1=list1.Find(j);

if(P1!

=NULL){

P1=list1.DeleteNode(P1);

deleteP1;

list1.PrintList();

}

elsecout<<"未找到"<

cout<<"请输入一个要求插入的整数"<

cin>>j;

P1=list1.CreatNode(j);

list1.InsertOrder(P1);

list1.PrintList();

list2=list1;

list2.PrintList();

Listlist3=list1;

list3.PrintList();

cout<<"请输入一个要求删除的整数"<

cin>>j;

P1=list1.Find(j);

if(P1!

=NULL){

P1=list1.DeleteNode(P1);

deleteP1;

list1.PrintList();

}

elsecout<<"未找到"<

list2=list3=list1;

list2.PrintList();

list3.PrintList();

list1.MakeEmpty();//清空list1

list2.MakeEmpty();//清空list1

list3.MakeEmpty();//清空list1

return0;

}

10.编写一个用来排序的函数模板,对下列数组进行降序的选择法排序。

初始化int类型数组data[]={1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20}

答:

/*

函数功能:

对数组中的某一部分进行选择法降序排序。

函数原形:

voidSelectSort(DataTypea[],intl,intr);

参数:

DataTypea[]:

欲排序的数组;

intl:

有序序列在数组中的起始位置;

intr:

有序序列在数组中的结束位置;

返回值:

无。

*/

#include

usingnamespacestd;

template

voidSelectSort(DataTypea[],intl,intr)

{

inti,j;

DataTypek;

for(i=l;i<=r-1;i++)

for(j=i+1;j<=r;j++)

if(a[i]

{

k=a[i];

a[i]=a[j];

a[j]=k;

}

}

intmain(void)

{

intdata[]={1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20};

inti;

SelectSort(data,0,19);

for(i=0;i<=19;i++)

{

cout<

}

cout<

return0;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2