第五章习题答案Word文件下载.docx
- 文档编号:21013360
- 上传时间:2023-01-26
- 格式:DOCX
- 页数:28
- 大小:26.81KB
第五章习题答案Word文件下载.docx
《第五章习题答案Word文件下载.docx》由会员分享,可在线阅读,更多相关《第五章习题答案Word文件下载.docx(28页珍藏版)》请在冰豆网上搜索。
(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;
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<
iostream>
usingnamespacestd;
structData
{
intn;
doublescore;
};
intmain()
Dataa[3]={1001,87,1002,72,1003,90},p=a;
cout<
(p++)->
n<
endl;
p->
n++<
(p).n++<
}
【解答】
1001
1002
1003
1004
2.
structEmployee
charname[20];
charsex;
voidfun(Employeep)
{
if((p).sex=='
m'
)
(p).name<
Employeeemp[5]={"
Liming"
'
Wangxiaoping"
f'
Luwei"
inti;
for(i=0;
i<
3;
i++)
fun(emp+i);
Liming
Luwei
3.
structNode
chars;
Nodeq;
Nodea[]={{"
Mary"
a+1},{"
Jack"
a+2},{"
Jim"
a}};
Nodep=a;
s<
q->
Mary
Jack
Jim
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<
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
}
//输出集合S中的全部元素
voidsetDisplay(constunsigned*S)
"
{"
;
if(Null(S))
cout<
}\n"
else
{
for(inti=0;
N;
i++)//输出元素
{
if(S[i])
cout<
i+1<
}
cout<
\b\b}\n"
//擦除最后的逗号
return;
//元素x并入集合S
boolputX(unsigned*S,unsignedx)
if(x>
S[x-1]=1;
returntrue;
returnfalse;
//求并集C=A∪B
voidCom(unsigned*C,constunsigned*A,constunsigned*B)
for(inti=0;
C[i]=int(A[i]||B[i]);
//求交集C=A∩B
voidsetInt(unsigned*C,constunsigned*A,constunsigned*B)
C[i]=int(A[i]&
B[i]);
//求差集C=A-B
voidsetDif(unsigned*C,constunsigned*A,constunsigned*B)
!
(A[1]&
B[i]));
//判蕴含,A蕴含于B时返回true
boolInc(constunsigned*A,constunsigned*B)
for(inti=0;
i++)
if(A[i]&
B[i])
returnfalse;
//判属于,x∈S时返回true
boolIn(constunsigned*S,constunsignedx)
returnS[x-1];
//判空集,S为空集时返回true
boolNull(constunsigned*S)
if(S[i])
unsignedA[N]={0},B[N]={0},C[N]={0};
InputtheelementsofsetA,1-"
N<
untilinput0:
\n"
setPut(A);
InputtheelementsofsetB,1-"
setPut(B);
A="
setDisplay(A);
B="
setDisplay(B);
Inputx:
cin>
x;
Put"
<
x<
inA="
putX(A,x);
setDisplay(A);
C=A+B="
Com(C,A,B);
setDisplay(C);
C=A*B="
setInt(C,A,B);
C=A-B="
setDif(C,A,B);
if(Inc(A,B))
A<
=Bistrue\n"
else
notA<
=B\n"
if(In(A,x))
inA\n"
notinA\n"
3.分析以下说明结构的语句:
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
有关程序如下:
structperson//说明结构类型
charname[10];
unsignedintid;
doublesalary;
};
voidInput(person[],constint);
voidSort(person[],int[],constint);
voidOutput(constperson[],int[],constint);
personallone[100];
//说明结构数组
intindex[100];
//说明索引数组
inttotal;
100;
i++)//索引数组元素值初始化为结构数组元素下标
index[i]=i;
输入职工人数:
total;
输入职工信息:
Input(allone,total);
以工资做关键字排序\n"
Sort(allone,index,total);
输出排序后信息:
Output(allone,index,total);
voidInput(personall[],constintn)
inti;
n;
i++)//输入数据
cout<
i<
:
姓名:
all[i].name;
编号:
all[i].id;
工资:
all[i].salary;
voidSort(personall[],intpi[],constintn)
inti,j;
intt;
//交换用中间变量
for(i=1;
i++)//以成员salary做关键字排序
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)
i++)//输出排序后数据
all[pi[i]].name<
'
\t'
all[pi[i]].id<
all[pi[i]].salary<
5.有以下结构说明和遍历单向链表的函数。
函数内有错误吗?
是什么性质的错误?
请上机验证你的分析。
Nodenext;
voidShowList(Nodehead)
while(head)
cout<
head->
date<
'
\n'
head++;
head++错误,原因是动态链表的结点存放不是连续顺序的内存空间,它们是逐个结点通过new建立的,所以不能用++做地址偏移运算。
应该用:
head=head->
next;
5.4编程题
1.编写程序,将一个整型变量右移4位,并以二进制形式输出该整数在移位前和移位后的数值。
观察系统填补空缺的数位情况。
voidbitDisplay(unsignedvalue);
Enteranunsignedinteger:
bitDisplay(x);
x>
=4;
Right4-bit\n"
voidbitDisplay(unsignedvalue)
unsignedc;
unsignedbitmask=1<
31;
value<
=\t"
for(c=1;
c<
=32;
c++)
(value&
bitmask?
1'
:
0'
);
value<
=1;
if(c%8==0)
2.整数左移一位相当于将该数乘以2。
编写一个函数
unsignedpower2(unsignednumber,unsignedpow);
使用移位运算计算number*2pow,并以整数形式输出计算结果。
注意考虑数据的溢出。
unsignedpower2(unsignednumber,unsignedpow)
unsignedc=1;
while(c<
31)//溢出判断
if(number&
bitmask)break;
//查找最高位的1即判断c为何值时最高位为1,判断可左移的最大次数
c++;
bitmask>
=1;
if(pow<
c)为此处做准备
returnnumber<
pow;
cout<
overflow!
return0;
3.使用按位异或(^)运算,可以不使用中间变量,快速交换两个变量的值。
设计一个函数,实现快速交换两个整型变量的值。
voidSwap(int&
A,int&
B)
A=A^B;
B=A^B;
4.集合的元素通常是字符。
设计程序,用无符号整数表示ASCII码字符集合,用位运算实现各种基本集合运算。
ASCII码是0~127的整数,可以用长度为4的无符号整型数组表示集合,如教材例5-4所示。
区别是,在输入集合元素时,需要把字符转换成整型数据,在输出操作中,把整型集合元素转换成字符型数据。
程序略。
5.使用结构类型表示复数。
设计程序输入两个复数,可以选择进行复数的+、-、×
或÷
运算,并输出结果。
iomanip>
structcomplex
doublere,im;
complexa,b,c;
charoper;
输入复数a的实部和虚部:
a.re>
a.im;
输入复数b的实部和虚部:
b.re>
b.im;
输入运算符:
oper;
switch(oper)
case'
+'
c.re=a.re+b.re;
c.im=a.im+b.im;
break;
-'
c.re=a.re-b.re;
c.im=a.im-b.im;
*'
c.re=a.re*b.re-a.im*b.im;
c.im=a.im*b.re+a.re*b.im;
/'
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:
inputerror!
return0;
c="
c.re;
setiosflags(ios:
showpos);
c.im<
i"
return0;
6.把一个班的学生姓名和成绩存放到一个结构数组中,寻找并输出最高分者。
structdata
charname[12];
}a[]={"
李小平"
90,"
何文章"
66,"
刘大安"
87,"
汪立新"
93,"
罗建国"
78,
陆丰收"
81,"
杨勇"
85,"
吴一兵"
55,"
伍晓笑"
68,"
张虹虹"
93};
doublemax=a[0].score;
inti,n=sizeof(a)/sizeof(data);
for(i=1;
if(a[i].score>
max)max=a[i].score;
if(a[i].score==max)cout<
a[i].name<
同样高分!
7.使用结构表示X—Y平面直角坐标系上的点,编写程序顺序读入一个四边形的4个顶点坐标,判别由这个顶点的连线构成的图形是否为正方形、矩
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 第五 习题 答案