第九章 习题及答案.docx
《第九章 习题及答案.docx》由会员分享,可在线阅读,更多相关《第九章 习题及答案.docx(13页珍藏版)》请在冰豆网上搜索。
第九章习题及答案
第九章习题
一、选择题
1.以下选项中不能正确把cl定义成结构体变量的是()
A)typedefstruct B)structcolorcl
{intred; {intred;
intgreen; intgreen;
intblue; intblue;
}COLOR;COLORcl; };
C)structcolor D)struct
{intred; {intred;
intgreen; intgreen;
intblue; intblue;
}cl; }cl;
2.有以下说明和定义语句
structstudent
{intage;charnum[8];};
structstudentstu[3]={{20,"200401"},{21,"200402"},{10\9,"200403"}};
structstudent*p=stu;
以下选项中引用结构体变量成员的表达式错误的是()
A)(p++)->num B)p->num C)(*p).num D)stu[3].age
3.有以下结构体说明、变量定义和赋值语句
structSTD
{charname[10];
intage;
charsex;
}s[5],*ps;
ps=&s[0];
则以下scanf函数调用语句中错误引用结构体变量成员的是()。
A)scanf(“%s”,s[0].name); B)scanf(“%d”,&s[0].age);
C)scanf(“%c”,&(ps->sex));D)scanf(“%d”,ps->age);
4.以下叙述中错误的是()
A)可以通过typedef增加新的类型
B)可以用typedef将已存在的类型用一个新的名字来代表
C)用typedef定义新的类型名后,原有类型名仍有效
D)用typedef可以为各种类型起别名,但不能为变量起别名
5.有以下程序段()
typedefstructnode{intdata;structnode*next;}*NODE;
NODEp;
以下叙述正确的是(C)
A)p是指向structnode结构变量的指针的指针
B)NODEp;语句出错
C)p是指向structnode结构变量的指针
D)p是structnode结构变量
6.若有以下定义和语句
uniondata
{inti;charc;floatf;}x;
inty;
则以下语句正确的是()。
A)x=10.5; B)x.c=101; C)y=x; D)printf(“%d\n”,x);
7.有以下程序
main()
{union{unsignedintn;
unsignedcharc;
}u1;
ul.c=`A`;
printf("%c\n",u1.n);
}
执行后输出结果是()
A)产生语法错 B)随机值 C)A D)65
8.有以程序
#include
#include
typedefstruct{charname[9];charsex;floatscore[2];}STU;
voidf(STUa)
{STUb={“Zhao”,’m’,85.0,90.0};inti;
strcpy(a.name,b.name);
a.sex=b.sex;
for(i=0;i<2;i++)a.score[i]=b.score[i];
}
main()
{STUc={“Qian”,’p’,95.0,92.0};
f(c);printf(“%s,%c,%2.0f,%2.0f\n”,c.name,c.sex,c.score[0],c.score[1]);
}
程序的运行结果是
A)Qian,p,95,92B)Qian,m,85,90
C)Zhao,p,95,92D)Zhao,m,85,90
9.现有以下结构体说明和变量定义,如图所示,指针p,q,r分别指向一个链表中连续的三个结点。
structnode
{
chardata;
structnode*next;
}*p,*q,*r;
现要将q和r所指结点交换前后位置,同时要保持链表的连续,以下不能完成此操作的语句是
A)q->next=r->next;p->next=r;r->next=q;
B)p->next=r;q->next=r->next;r->next=q;
C)q->next=r->next;r->next=q;p->next=r;
D)r->next=q;p->next=r;q-next=r->next;
10.有以下程序段
structst
{intx;int*y;}*pt:
inta[]={1,2},b[]={3,4};
structstc[2]={10,a,20,b};
pt=c;
以下选项中表达式的值为11的是()
A)*pt->y B)pt->x C)++pt->x D)(pt++)->x
二、填空题
1.设有说明
structDATE{intyear;intmonth;intday;};
请写出一条定义语句,该语句定义d为上述结构体变量,并同时为其成员year、month、day依次赋初值2006、10、1。
2.已有定义如下:
structnode
{intdata;
structnode*next;
}*p;
以下语句调用malloc函数,使指针p指向一个具有structnode类型的动态存储空间。
请填空。
p=(structnode*)malloc();
3.以下程序中函数fun的功能是:
统计person所指结构体数组中所有性别(sex)为M的记录的个数,存入变量n中,并做为函数值返回。
请填空:
#include
#defineN3
typedefstruct
{intnum;charnam[10];charsex;}SS;
intfun(SSperson[])
{inti,n=0;
for(i=0;iif(==’M’)n++;
returnn;
}
main()
{SSW[N]={{1,”AA”,’F’},{2,”BB”,’M’},{3,”CC”,’M’}};intn;
n=fun(W);printf(“n=%d\n”,n);
}
4.以下程序运行后的输出结果是()。
structNODE
{intk;
structNODE*link;
};
main()
{structNODEm[5],*p=m,*q=m+4;
inti=0;
while(p!
=q){
p->k=++i;p++;
q->k=i++;q--;
}
q->k=i;
for(i=0;i<5;i++)printf("%d",m[i].k);
printf("\n");
}
5.以下程序的功能是:
建立一个带有头结点的单向链表,并将存储在数组中的字符依次转储到链表的各个结点中,请从与下划线处号码对应的一组选若中选择出正确的选项。
#include
stuctnode
{chardata;structnode*next;};
(1)CreatList(char*s)
{structnode*h,*p,*q);
h=(structnode*)malloc(sizeof(structnode));
p=q=h;
while(*s!
='\0')
{p=(structnode*)malloc(sizeof(structnode));
p->data=
(2);
q->next=p;
q=(3);
s++;
}
p->next='\0';
returnh;
}
main()
{charstr[]="linklist";
structnode*head;
head=CreatList(str);
...
}
三、编程题
1.定义一个能正常反映教师情况的结构体teacher,包含教师姓名、性别、年龄、所在部门和薪水;定义一个能存放两人数据的结构体数组tea,并用如下数据初始化:
{{“Mary“,‘W’,40,‘Computer’,1234},{“Andy“,‘M’,55,‘English’,1834}};要求:
分别用结构体数组tea和指针p输出各位教师的信息,写出完整定义、初始化、输出过程。
2.定义一个结构体变量(包括年、月、日)。
计算该日在本年中是第几天,注意闰年问题。
3.构建简单的手机通讯录,手机通讯录包括信息(姓名、年龄、联系电话),要求实现新建、查询功能。
假设通信录最多容纳50名联系人信息。
4.建立一个教师链表,每个结点包括学号(no),姓名(name[8]),工资(wage),写出动态创建函数creat和输出函数print。
5.在上一题基础上,假如已经按学号升序排列,写出插入一个新教师的结点的函数Insert。
第9章习题答案
一、选择题
1-5BDDAC6-10BCADC
二、填空题
1.structDATAd={2006,10,1};
2.sizeof(structnode)
3.person[i].sex
4.13431
5.
(1)structnode*
(2)*s(3)p
三、编程题
1.定义一个能正常反映教师情况的结构体teacher,包含教师姓名、性别、年龄、所在部门和薪水;定义一个能存放两人数据的结构体数组tea,并用如下数据初始化:
{{“Mary“,‘W’,40,‘Computer’,1234},{“Andy“,‘M’,55,‘English’,1834}};要求:
分别用结构体数组tea和指针p输出各位教师的信息,写出完整定义、初始化、输出过程。
#include
structteacher
{charname[8];
charsex;
intage;
chardepartment[20];
floatsalary;
};