第五章习题答案.docx
《第五章习题答案.docx》由会员分享,可在线阅读,更多相关《第五章习题答案.docx(28页珍藏版)》请在冰豆网上搜索。
![第五章习题答案.docx](https://file1.bdocx.com/fileroot1/2023-1/26/0bd014b7-ec1a-4fbc-8ec8-624689228267/0bd014b7-ec1a-4fbc-8ec8-6246892282671.gif)
第五章习题答案
第5章集合与结构
5.1选择题
1.语句cout<<(1&2)<<","<<(1&&2)<(A)0,0(B)0,1(C)1,0(C)1,1
2.语句cout<<(1|2)<<","<<(1||2)<(A)0,0(B)1,1(C)2,0(D)3,1
3.有以下说明语句:
structpoint
{intx;inty;}p;
则正确的赋值语句是(C)。
(A)point.x=1;point.y=2;(B)point={1,2};
(C)p.x=1;p.y=2;(D)p={1,2};
4.已知有职工情况结构变量emp定义为:
structDate
{
intyear;
intmonth;
intday;
};
strnctEmployee
{
charname[20];
longcode;
Datebirth
};
Employeeemp;
下列对emp的birth正确赋值方法是(D)。
(A)year=1980;month=5;day=1;
(B)birth.year=1980;birth.month=5;birth.day=1;
(C)emp.year=1980;emp.month=5;emp.day=1;
(D)emp.birth.year=1980;emp.birth.month=5;emp.birth.day=1;
5.有以下说明语句:
structStudent
{
intnum;
doublescore;
};
Studentstu[3]={{1001,80},{1002,75},{1003,91}},p=stu;
则下面引用形式错误的是(B)。
(A)p>num(B)(p++).num(C)(p++)>num(D)(p).num
6.有以下说明语句:
structWorker
{
intno;
charname[20];
};
Workerw,p=&w;
则下列错误的引用是(D)。
(A)w.no(B)p->no(C)(p).no(D)p.no
7.s1和s2是两个结构类型变量,若要使赋值s1=s2合法,则它们的说明应该是(C)。
(A)s1只能接收相同类型的数据成员(B)结构中的成员相同
(C)同一结构类型的变量(D)存储字节长度一样的变量
5.2阅读下列程序,写出运行结果。
1.
#include
usingnamespacestd;
structData
{
intn;
doublescore;
};
intmain()
{
Dataa[3]={1001,87,1002,72,1003,90},p=a;
cout<<(p++)->n<cout<<(p++)->n<cout<n++<cout<<(p).n++<}
【解答】
1001
1002
1003
1004
2.
#include
usingnamespacestd;
structEmployee
{
charname[20];
charsex;
};
voidfun(Employeep)
{
if((p).sex=='m')
cout<<(p).name<}
intmain()
{
Employeeemp[5]={"Liming",'m',"Wangxiaoping",'f',"Luwei",'m'};
inti;
for(i=0;i<3;i++)
fun(emp+i);
}
【解答】
Liming
Luwei
3.
#include
usingnamespacestd;
structNode
{
chars;
Nodeq;
};
intmain()
{
Nodea[]={{"Mary",a+1},{"Jack",a+2},{"Jim",a}};
Nodep=a;
cout<s<cout<q->s<cout<q->q->s<cout<q->q->q->s<}
【解答】
Mary
Jack
Jim
Mary
5.3思考题
1.判断一个整数n的奇偶性,可以利用位运算判断吗?
请你试一试。
【解答】
可以。
一个整数当最低位为1时,它是奇数,否则为偶数。
以下函数返回对参数k的奇偶判断。
boolodd(intk)
{
return1&k;
}
2.长度为N的数组可以表示N个元素的集合,若有:
S[i]==1,表示对应元素在集合中
如何实现集合的基本运算?
请你试一试。
并从内存和处理要求上与5.2.2节中集合的实现方法进行比较。
【解答】
长度为N的数组S可以表示有N个元素的集合。
当S[i]==1,表示元素i+1在集合中;当S[i]==0,表示元素i+1不在集合中。
集合运算通过对数组元素操作完成。
用数组实现集合运算的空间和时间消耗高于用无符号整数和位运算实现集合运算。
用数组实现集合运算程序如下。
#include
usingnamespacestd;
voidsetPut(unsigned*S);//输入集合S的元素
voidsetDisplay(constunsigned*S);//输出集合S中的全部元素
boolputX(unsigned*S,unsignedx);//元素x并入集合
voidCom(unsigned*C,constunsigned*A,constunsigned*B);//求并集C=A∪B
voidsetInt(unsigned*C,constunsignedA,constunsignedB);//求交集C=A∩B
voidsetDif(constunsignedA,constunsignedB);//求差集C=A-B
boolInc(constunsigned*A,constunsigned*B);//判蕴含
boolIn(constunsigned*S,constunsignedx);//判属于x∈S
boolNull(constunsigned*S);//判空集
constintN=32;
//输入集合元素
voidsetPut(unsigned*S)
{
unsignedx;
cin>>x;
while(x>0&&x<=N)
{
putX(S,x);//把输入元素并入集合S
cin>>x;
}
}
//输出集合S中的全部元素
voidsetDisplay(constunsigned*S)
{
cout<<"{";
if(Null(S))
cout<<"}\n";
else
{
for(inti=0;i{
if(S[i])
cout<
}
cout<<"\b\b}\n";//擦除最后的逗号
}
return;
}
//元素x并入集合S
boolputX(unsigned*S,unsignedx)
{
if(x>0&&x<=N)
{
S[x-1]=1;
returntrue;
}
returnfalse;
}
//求并集C=A∪B
voidCom(unsigned*C,constunsigned*A,constunsigned*B)
{
for(inti=0;iC[i]=int(A[i]||B[i]);
}
//求交集C=A∩B
voidsetInt(unsigned*C,constunsigned*A,constunsigned*B)
{
for(inti=0;iC[i]=int(A[i]&&B[i]);
}
//求差集C=A-B
voidsetDif(unsigned*C,constunsigned*A,constunsigned*B)
{
for(inti=0;iC[i]=int(A[i]&&!
(A[1]&&B[i]));
}
//判蕴含,A蕴含于B时返回true
boolInc(constunsigned*A,constunsigned*B)
{
for(inti=0;i{
if(A[i]&&!
B[i])
returnfalse;
}
returntrue;
}
//判属于,x∈S时返回true
boolIn(constunsigned*S,constunsignedx)
{
returnS[x-1];
}
//判空集,S为空集时返回true
boolNull(constunsigned*S)
{
for(inti=0;i{
if(S[i])
returnfalse;
}
returntrue;
}
intmain()
{
unsignedA[N]={0},B[N]={0},C[N]={0};
unsignedx;
cout<<"InputtheelementsofsetA,1-"<\n";
setPut(A);
cout<<"InputtheelementsofsetB,1-"<\n";
setPut(B);
cout<<"A=";
setDisplay(A);
cout<<"B=";
setDisplay(B);
cout<<"Inputx:
";
cin>>x;
cout<<"Put"<putX(A,x);
setDisplay(A);
cout<<"C=A+B=";
Com(C,A,B);
setDisplay(C);
cout<<"C=A*B=";
setInt(C,A,B);
setDisplay(C);
cout<<"C=A-B=";
setDif(C,A,B);
setDisplay(C);
if(Inc(A,B))
cout<<"A<=Bistrue\n";
else
cout<<"notA<=B\n";
cout<<"Inputx:
";
cin>>x;
if(In(A,x))
cout<else
cout<}
3.分析以下说明结构的语句:
structNode
{
intdata;
Nodeerror;//错误
Nodeok;//正确
};
error和ok分别属于什么数据类型?
有什么存储要求?
error出错的原因是什么?
【解答】
error是Node结构类型数据成员,错误。
原因是结构定义的数据成员若为本身的结构类型,是一种无穷递归。
ok是指向Node类型的指针,定义正确,占4字节。
4.本章例5-8中用辅助数组对结构数组进行关键字排序,有定义:
personindex[100];
index数组存放结构数组元素的地址。
如果把index定义改为:
intindex[100];
用于存放结构数组元素的下标,可以实现对结构数组的索引排序吗?
如何修改程序?
请你试一试。
【解答】
可以。
关键是通过整型索引数组元素作为下标访问结构数组。
表示为:
all[pi[i]].nameall[pi[i]].idall[pi[i]].salary
有关程序如下:
#include
usingnamespacestd;
structperson//说明结构类型
{
charname[10];
unsignedintid;
doublesalary;
};
voidInput(person[],constint);
voidSort(person[],int[],constint);
voidOutput(constperson[],int[],constint);
intmain()
{
personallone[100];//说明结构数组
intindex[100];//说明索引数组
inttotal;
for(inti=0;i<100;i++)//索引数组元素值初始化为结构数组元素下标
index[i]=i;
cout<<"输入职工人数:
";
cin>>total;
cout<<"输入职工信息:
\n";
Input(allone,total);
cout<<"以工资做关键字排序\n";
Sort(allone,index,total);
cout<<"输出排序后信息:
\n";
Output(allone,index,total);
}
voidInput(personall[],constintn)
{
inti;
for(i=0;i{
cout<
姓名:
";
cin>>all[i].name;
cout<<"编号:
";
cin>>all[i].id;
cout<<"工资:
";
cin>>all[i].salary;
}
}
voidSort(personall[],intpi[],constintn)
{
inti,j;
intt;//交换用中间变量
for(i=1;i{
for(j=0;j<=n-1-i;j++)
if(all[pi[j]].salary>all[pi[j+1]].salary)//通过索引数组访问结构数组元素
{
t=pi[j];//交换索引数组元素值
pi[j]=pi[j+1];
pi[j+1]=t;
}
}
}
voidOutput(constpersonall[],intpi[],constintn)
{
for(inti=0;icout<}
5.有以下结构说明和遍历单向链表的函数。
函数内有错误吗?
是什么性质的错误?
请上机验证你的分析。
structNode
{
intdata;
Nodenext;
};
voidShowList(Nodehead)
{
while(head)
{
cout<date<<'\n';
head++;
}
}
【解答】
head++错误,原因是动态链表的结点存放不是连续顺序的内存空间,它们是逐个结点通过new建立的,所以不能用++做地址偏移运算。
应该用:
head=head->next;
5.4编程题
1.编写程序,将一个整型变量右移4位,并以二进制形式输出该整数在移位前和移位后的数值。
观察系统填补空缺的数位情况。
#include
usingnamespacestd;
voidbitDisplay(unsignedvalue);
intmain()
{
unsignedx;
cout<<"Enteranunsignedinteger:
";
cin>>x;
bitDisplay(x);
x>>=4;
cout<<"Right4-bit\n";
bitDisplay(x);
}
voidbitDisplay(unsignedvalue)
{
unsignedc;
unsignedbitmask=1<<31;
cout<for(c=1;c<=32;c++)
{
cout<<(value&bitmask?
'1':
'0');
value<<=1;
if(c%8==0)
cout<<'';
}
cout<}
2.整数左移一位相当于将该数乘以2。
编写一个函数
unsignedpower2(unsignednumber,unsignedpow);
使用移位运算计算number*2pow,并以整数形式输出计算结果。
注意考虑数据的溢出。
【解答】
unsignedpower2(unsignednumber,unsignedpow)
{
unsignedc=1;
unsignedbitmask=1<<31;
while(c<31)//溢出判断
{
if(number&bitmask)break;//查找最高位的1即判断c为何值时最高位为1,判断可左移的最大次数
c++;
bitmask>>=1;
}
if(powreturnnumber<else
{
cout<<"overflow!
\n";
return0;
}
}
3.使用按位异或(^)运算,可以不使用中间变量,快速交换两个变量的值。
设计一个函数,实现快速交换两个整型变量的值。
【解答】
voidSwap(int&A,int&B)
{
A=A^B;
B=A^B;
A=A^B;
}
4.集合的元素通常是字符。
设计程序,用无符号整数表示ASCII码字符集合,用位运算实现各种基本集合运算。
【解答】
ASCII码是0~127的整数,可以用长度为4的无符号整型数组表示集合,如教材例5-4所示。
区别是,在输入集合元素时,需要把字符转换成整型数据,在输出操作中,把整型集合元素转换成字符型数据。
程序略。
5.使用结构类型表示复数。
设计程序输入两个复数,可以选择进行复数的+、-、×或÷运算,并输出结果。
【解答】
#include
#include
usingnamespacestd;
structcomplex
{
doublere,im;
};
intmain()
{
complexa,b,c;charoper;
cout<<"输入复数a的实部和虚部:
";
cin>>a.re>>a.im;
cout<<"输入复数b的实部和虚部:
";
cin>>b.re>>b.im;
cout<<"输入运算符:
";
cin>>oper;
switch(oper)
{
case'+':
c.re=a.re+b.re;c.im=a.im+b.im;break;
case'-':
c.re=a.re-b.re;c.im=a.im-b.im;break;
case'*':
c.re=a.re*b.re-a.im*b.im;
c.im=a.im*b.re+a.re*b.im;break;
case'/':
c.re=(a.re*b.re+a.im*b.im)/(b.re*b.re+b.im*b.im);
c.im=(a.im*b.re-a.re*b.im)/(b.re*b.re+b.im*b.im);
break;
default:
cout<<"inputerror!
"<return0;
}
cout<<"c="<cout<:
showpos);
cout<return0;
}
6.把一个班的学生姓名和成绩存放到一个结构数组中,寻找并输出最高分者。
【解答】
#include
usingnamespacestd;
intmain()
{
structdata
{
charname[12];
doublescore;
}a[]={"李小平",90,"何文章",66,"刘大安",87,"汪立新",93,"罗建国",78,
"陆丰收",81,"杨勇",85,"吴一兵",55,"伍晓笑",68,"张虹虹",93};
doublemax=a[0].score;
inti,n=sizeof(a)/sizeof(data);
for(i=1;iif(a[i].score>max)max=a[i].score;
for(i=0;iif(a[i].score==max)cout<}
7.使用结构表示X—Y平面直角坐标系上的点,编写程序顺序读入一个四边形的4个顶点坐标,判别由这个顶点的连线构成的图形是否为正方形、矩