
第八都分结构体 8,1填空题 1.按下面的提示写出对应的代码。 (1)声明一个名为C的结构体类型。该类型的成员为:一个韩由各元素的整型数组a 和一个整型b.【 】 (2)定义ae结构体类型的变量x和整型变量y,【 (3)哈变量x的成员a的第三个元素献一个任意值,并给y赋2:【 】 (4)比较上而两个值,并输出较大者。【 2.假设有如下界奥固体类型说明: struct aa[ int a: int b: : 根据下面的提示写出对应的代到。 (1)定义上述结构体类型变量x和整型变量y,【 】 (2)给变量x的两个成员a和b分别赋1和2。【 (3)将变量x的两个成贝员之和赋给y。【 】 (4)输出y的值。【 3。以下程序的执行结果是【 1. 年include(stdio.h> typedef struct strin char c[5】: char◆s: )st min( static st s1[2]-(“ABD”,“EFH”},{“1jK”,“Lw”}》: static struct str2 st sr; int d: 1s2={“0f0”,“5T”,32765:
第八部分结构体 8.1 填空题 1.按下面的提示写出对应的代码。 (1)声明一个名为 abc 的结构体类型,该类型的成员为:一个韩由各元素的整型数组 a 和一个整型 b。【 】 (2)定义 abc 结构体类型的变量 x 和整型变量 y。【 】 (3)给变量 x 的成员 a 的第三个元素赋一个任意值,并给y 赋 2。【 】 (4)比较上面两个值,并输出较大者。【 】 2.假设有如下界噢固体类型说明: struct aa{ int a; int b; }; 根据下面的提示写出对应的代码。 (1)定义上述结构体类型变量 x 和整型变量 y。【 】 (2)给变量 x 的两个成员 a 和 b 分别赋 1 和 2。【 】 (3)将变量 x 的两个成员之和赋给 y。【 】 (4)输出 y 的值。【 】 3.以下程序的执行结果是【 】。 #include typedef struct strin { char c[5]; char *s; } st; main( ) { static st s1[2]={{“ABCD”, “EFGH”}, {“IJK”, “LMN”}}; static struct str2 { st sr; int d; } s2={“OPQ”, “RST”, 32765};

stp门-&s1[0.&sl[1}: primtf("s\n".p[o]->e[1]): printf(“sln”,t+p[o]->s): printf(“\n”,s2.sr.c2]): printf(“d八n",s2.d+l》: 1 4.有如下定义: struct int x: char◆y: 】tb[2]-({l,”ab”】,(2.”cd”]小,ptab: 则:表达式和y的结果是【 表达式*(4+》->y的结果是【 5.以下程序的执行果是【 】 struct stru int x: char c; 1: nain( struct stru a-I0,“x': fune (a); printr(“d,en”,ax,a.e): func(struct stru b) b.=20: h.c=”y': 8,以下程序的执行结果是【 1 struct stru
st *p[]={&s1[0], &s1[1]}; printf(“%c\n”, p[0]->c[1]); printf(“%s\n”, ++p[0]->s); printf(“%c\n”, s2.sr.c[2]); printf(“%d\n”, s2.d+1); } 4.有如下定义: struct { int x; char *y; } tab[2]={{1,”ab”}, {2,”cd”}}, *p=tab; 则:表达式*p->y 的结果是【 】。 表达式*(++p)->y 的结果是【 】 5.以下程序的执行结果是【 】。 struct stru { int x; char c; }; main( ) { struct stru a={10, ‘x’}; func(a); printf(“%d, %c\n”, a.x, a.c); } func(struct stru b) { b.x=20; b.c=’y’; } 6.以下程序的执行结果是【 】。 struct stru {

int x: char c: 1: main() struct stru a=I0,“x’,*p-kn: fune(p): printf("s,%c'n",a.x,a.c) 1 func(struct stru *b) b->a-20: b->e-’y': 7。以下程序的执行结果是【 】 #include main() union int i[2]: long k: char e[4]: 》r,*p-r p->i[o]-9: p->if1]=8: primtf("sd,sd.sd,sdvn",p->c[o].p->e[1],p->c[2].p->e[3]) 1 8。以下程序用于在结构体数组中查找分数最高和最低的同学姓名和成绩。请在程序中 的空自处填入一条语句或一个表达式: 年include(stdio.h nain() int max,min,i.j:
int x; char c; }; main( ) { struct stru a={10, ‘x’}, *p=&a; func(p); printf(“%d, %c\n”, a.x, a.c); } func(struct stru *b) { b->x=20; b->c=’y’; } 7.以下程序的执行结果是【 】。 #include main( ) { union { int i[2]; long k; char c[4]; } r, *p=&r; p->i[0]=9; p->i[1]=8; printf(“%d, %d, %d, %d\n”, p->c[0], p->c[1], p->c[2], p->c[3]); } 8.以下程序用于在结构体数组中查找分数最高和最低的同学姓名和成绩。请在程序中 的空白处填入一条语句或一个表达式。 #include main( ) { int max, min, i, j;

static struct char name[10]: int score: )smd[6们-(“刘芳”,88,”王痒”,8,“赵清”,81,“孙域”,65,“李玲”, 691: max=min=l: for(i=l:i(5:i+) if(stud[i].score>stud[r].score)【 】: elseif(stod[i].score(stud[nin].score》【 】: primtf(“The highest score:s,dn,【 】: printr(“The lowest score:第s,sd\n”,【 1): 9。以下程序输入若干人员的姓名及电话号码,以字符”#'结束输入。然后输入姓名, 查找该人的电话号码。请在程序中的空白处填入一条语句或一个表达式。 =include《stdio.h> #include (string.h> 年derine MAX1000 struct telephome char nane[10] char telephno[15]: main() struct telephone s[MAX]: int i-0; char na[1o]: printf("Input name:") gets(s[i】.se): printf(“Input telephno:”): scanf(“s”,s[i].telephno): while(strcap(【 】,“#")》
static struct { char name[10]; int score; } stud[5]={“刘芳”,88,”王萍”, 78, “赵清”, 81, “孙斌”, 65, “李玲”, 69}; max=min=1; for (i=1; istud[max].score) 【 】 ; else if (stud[i].score #include #define MAX 1000 struct telephone { char name[10]; char telephno[15]; } main() { struct telephone s[MAX]; int i=0; char na[10]; printf(“Input name:”); gets(s[i].name); printf(“Input telephno:”); scanf(“%s”, s[i].telephno); while (strcmp(【 】 , “#”)) {

i+计: printf(“Input nane:”): gets(s[i].name); printf("Input telephno:"): scanf(“s”,s[i].telephno): printf("Input a nare for search:") gets(na): search(s,d,i一): search(【 】,char%,intn int J: j0: while(stremp(b[j】.nae,x)-o)kja)j+: if(【 】) printf(“telephno is:ssln",【 】: else printf("Not found!\n"); ) 10,以下函数e1eh,)用于副除一个整数单硅表中值为x的结点,h是链表的首指针, 剩除成功返日0,否则返目-1。链表的结点结构定义如下: struct link int data; 8 truct link*知ext: 1 链表的首结点的dat域存放该链表中整数的个数,如果链表中整数个数为0,则h->data 为0,>next为MLL,而h不指向ML。find仙,函数用于在首指针为h的链表中查找 值为x的结点,找到时返目一个指向x前趋的指针,否则返回M山,函数dele(h,x)和find(h, )的定义如下。请在程序中的空白处填入一条语句或一个表达式, struct link *find(struct link韩,int stct1inkp,利i p=h->next:qh:
i++; printf(“Input name:”); gets(s[i].name); printf(“Input telephno:”); scanf(“%s”, s[i].telephno); } printf(“Input a name for search:”); gets(na); search(s,na,i--); } search(【 】 , char *x, int n) { int j; j=0; while (strcmp(b[j].name, x)==0) && jdata 为 0,h->next 为 NULL,而 h 不指向 NULL。find(h, x)函数用于在首指针为 h 的链表中查找 值为 x 的结点,找到时返回一个指向 x 前趋的指针,否则返回 NULL。函数 dele(h, x)和 find(h, x)的定义如下。请在程序中的空白处填入一条语句或一个表达式。 struct link *find(struct link *h, int x) { struct link *p, *q; p=h->next; q=h;

hile(p!=Lk接ata=J 【 】: pr【 】: if (p==NULL)return (NULL) return(【 】: 1 dele(struct link wh.int x) struct1ink刺.find0: if (!(h->data))return -1; if(a-find (h,x)-【 】 struct link *p: p=q->next: r>nete【 】 free(struct link *)p): h->data--: return 0; return -1: 8.2程序设计题 L.定义5个元素的struct STUDENT数组a5)],编写函数(结构体数组名作为街数参 数)实现如下功能 (1)从键盒输入5个学生的姓名,年龄,语文成镜,数学成绩保存到数组中。 (2)计算这5个学生的均分并保存到相应的结构体成员avee中。 (3)按照总分降序排序。 (4)输出这5个学生排序后的列表。 1.2.编写一个程序建立一条单链表存储线性表(al,2,a3”,,如下图所示, 然后将该线性表逆置成(an,~l,,al)
while (p!=NULL && p->data!=x) { q= 【 】 ; p= 【 】 ; } if (p==NULL) return (NULL); return (【 】 ); } dele(struct link *h, int x) { struct link *q, *find(); if (!(h->data)) return -1; if ((q=find(h,x))!= 【 】 ); { struct link *p; p=q->next; q->next= 【 】; free(struct link *)p); h->data--; return 0; } return -1; } 8.2 程序设计题 1.定义 5 个元素的 struct STUDENT 数组 a[5],编写函数(结构体数组名作为函数参 数)实现如下功能: (1)从键盘输入 5 个学生的姓名、年龄、语文成绩、数学成绩保存到数组中。 (2)计算这 5 个学生的均分并保存到相应的结构体成员 average 中。 (3)按照总分降序排序。 (4)输出这 5 个学生排序后的列表。 1.2.编写一个程序建立一条单链表存储线性表(a1, a2, a3, …, an),如下图所示, 然后将该线性表逆置成(an, an-1, …, a1)