C++程序设计YDaniel Liang 第十八十九二十章课后习题答案.docx
《C++程序设计YDaniel Liang 第十八十九二十章课后习题答案.docx》由会员分享,可在线阅读,更多相关《C++程序设计YDaniel Liang 第十八十九二十章课后习题答案.docx(15页珍藏版)》请在冰豆网上搜索。
![C++程序设计YDaniel Liang 第十八十九二十章课后习题答案.docx](https://file1.bdocx.com/fileroot1/2022-10/28/eab26b65-f608-4ca9-b2e3-4af0ca153b46/eab26b65-f608-4ca9-b2e3-4af0ca153b461.gif)
C++程序设计YDanielLiang第十八十九二十章课后习题答案
Exercise18_2
#include
usingnamespacestd;
//Functionprototype
template
voidarraycopy(Tsource[],intsourceStartIndex,
Ttarget[],inttargetStartIndex,intlength);
template
voidmerge(Tlist1[],intlist1Size,
Tlist2[],intlist2Size,Ttemp[]);
/*Thefunctionforsortingthenumbers*/
template
voidmergeSort(Tlist[],intarraySize)
{
if(arraySize>1)
{
//Mergesortthefirsthalf
T*firstHalf=newT[arraySize/2];
arraycopy(list,0,firstHalf,0,arraySize/2);
mergeSort(firstHalf,arraySize/2);
//Mergesortthesecondhalf
intsecondHalfLength=arraySize-arraySize/2;
T*secondHalf=newT[secondHalfLength];
arraycopy(list,arraySize/2,secondHalf,0,secondHalfLength);
mergeSort(secondHalf,secondHalfLength);
//MergefirstHalfwithsecondHalf
T*temp=newT[arraySize];
merge(firstHalf,arraySize/2,secondHalf,secondHalfLength,
temp);
arraycopy(temp,0,list,0,arraySize);
delete[]temp;
delete[]firstHalf;
delete[]secondHalf;
}
}
template
voidmerge(Tlist1[],intlist1Size,
Tlist2[],intlist2Size,Ttemp[])
{
intcurrent1=0;//Indexinlist1
intcurrent2=0;//Indexinlist2
intcurrent3=0;//Indexintemp
while(current1{
if(list1[current1]temp[current3++]=list1[current1++];
else
temp[current3++]=list2[current2++];
}
while(current1temp[current3++]=list1[current1++];
while(current2temp[current3++]=list2[current2++];
}
template
voidarraycopy(Tsource[],intsourceStartIndex,
Ttarget[],inttargetStartIndex,intlength)
{
for(inti=0;i{
target[i+targetStartIndex]=source[i+sourceStartIndex];
}
}
template
voidprintArray(Tlist[],intarraySize)
{
for(inti=0;i{
cout<}
cout<}
intmain()
{
intlist1[]={3,5,1,0,2,8,7};
mergeSort(list1,7);
printArray(list1,7);
doublelist2[]={3.5,0.5,1.4,0.4,2.5,1.8,4.7};
mergeSort(list2,7);
printArray(list2,7);
stringlist3[]={"Atlanta","Denver","Chicago","Dallas"};
mergeSort(list3,4);
printArray(list3,4);
return0;
}
Exercise18_6
#include
usingnamespacestd;
//Tisagenerictype
template
boolordered(Tlist[],intsize);
//Tisagenerictype
template
boolordered(Tlist[],intsize,boolascending);
template
boolordered(Tlist[],intsize)
{
boolordered=true;
for(inti=0;iif(list[i]>list[i+1]){
ordered=false;
break;
}
returnordered;
}
//Tisagenerictype
template
boolordered(Tlist[],intsize,boolascending)
{
if(ascending)
returnordered(list,size);
boolordered=true;
for(inti=0;iif(list[i]ordered=false;
break;
}
returnordered;
}
template
voidprintArray(Tlist[],intarraySize)
{
for(inti=0;i{
cout<}
cout<}
intmain()
{
intlist1[]={3,5,1,0,2,8,7};
cout<cout<intlist2[]={1,2,3,5,7,8};
cout<cout<cout<return0;
}
Exercise19_2
#include
#include
#include
usingnamespacestd;
template
ElementTypemaxElement(ContainerType&container,constElementType&value)
{
for(inti=0;iif(container[i]==value)
returni;
return-1;
}
intmain()
{
intlist1[]={3,5,1,0,2,8,7,6};
vectorintVector(list1,list1+8);
cout<<"Themaximumvalueinlist1:
"<<(maxElement>(intVector,8));
return0;
}
Exercise19_4
#include
#include
#include
usingnamespacestd;
template
voidreverse(ContainerType&container)
{
intcount=0;
//returncount;
}
intmain()
{
intlist1[]={3,5,1,8,2,8,7,6};
vectorintVector(list1,list1+8);
reverse>(intVector);
return0;
}
Exercise19_6
#include
usingnamespacestd;
intv1=10;
intmain()
{
unsignedshortx=0XAEAE>>4;
cout<return0;
}
Exercise20_2
#include
#include
#include
#include
usingnamespacestd;
intnextNum()
{
srand(time(0));
returnrand();
}
intmain()
{
doublevalues[]={1.3,2.4,4.5,6.7,9.0};
dequedoubleDeque(values,values+5);
dequedoubleDeque1(values,values+5);
ostream_iterator