资源描述
第三讲 结构体与共用体
一、选择题
1.在说明一个结构体变量时系统分配给它的存储空间是 。
A) 该结构体中第一个成员所需存储空间
B) 该结构体中最后一个成员所需存储空间
C) 该结构体中占用最大存储空间的成员所需存储空间
D) 该结构体中所有成员所需存储空间的总和
2.若有以下说明和语句:
struct worker
{ int no; char ﹡name; }work, ﹡p=&work;
则以下引用方式不正确的是 。*
A) work.no B) (﹡p).no C) p->no D)work->no
3.有如下定义:
struct date { int year, month, day; };
struct worklist { char name[20]; char sex;
struct date birthday; }person;
对结构体变量person的出生年份进行赋值时,下面正确的赋值语句是 。
*
A) year=1958 B) birthday.year=1958
C) person.birthday.year=1958 D) person.year=1958
4.以下对结构体类型变量的定义中不正确的是 。*
A) #define STUDENT struct student B) struct student
STUDENT { int num;
{ int num; float age;
float age; }std1; }std1;
C) struct D) struct
{ int num; { int num;
float age; float age; } student;
}std1; struct student std1;
5.设有以下说明语句
struct stu
{ int a; float b; }stutype;
则下面的叙述不正确的是 。*
A) struct是结构体类型的关键字
B) struct stu是用户定义的结构体类型
C) stutype是用户定义的结构体类型名
D) a和b都是结构体成员名
6.C语言结构体类型变量在程序执行期间 。
A) 所有成员一直驻留在内存中
B) 只有一个成员主留在内存中
C) 部分成员驻留在内存中
D) 没有成员驻留在内存中
7.以下程序的运行结果是 。*
# include <stdio.h>
main( )
{ struct date
{ int year, month, day; }today;
printf(“%d\n”,sizeof(struct date)); }
A) 6 B) 8 C) 10 D)12
8.有如下定义
struct person{char name[9]; int age;};
struct person class[10]={“Johu”, 17,
“Paul”, 19
“Mary”, 18,
“Adam 16,};
根据上述定义,能输出字母M的语句是 。*
A) prinft(“%c\n”,class[3].mane);
B) pfintf(“%c\n”,class[3].name[1]);
C) prinft(“%c\n”,class[2].name[1]);
D) printf(“%^c\n”,class[2].name[0]);
9.设有如下定义
struct ss
{ char name[10];
int age;
char sex;
} std[3],* p=std;
下面各输入语句中错误的是 。*
A) scanf("%d",&(*p).age);
B) scanf("%s",&std.name);
C) scanf("%c",&std[0].sex);
D) scanf("%c",&(p->sex))
10.设有以下说明语句,则下面的叙述中不正确的是 。*
struct ex {
int x ; float y; char z ;
} example;
A) struct结构体类型的关键字 B) example是结构体类型名
C) x,y,z都是结构体成员名 D) struct ex是结构体类型
11.若程序中有下面的说明和定义:
struct stt
{ int x;
char b;
}
struct stt a1,a2;
则会发生的情况是 。*
A)程序将顺利编译、连接、执行。
B)编译出错。
C)能顺利通过编译、连接,但不能执行。
D)能顺利通过编译,但连接出错。
12.已知教师记录定义为:
struct student
{ int no;
char name[30];
struct
{ unsigned int year;
unsigned int month;
unsigned int day;
}birthday;
} stu;
struct student *t = &stu;
若要把变量t中的生日赋值为“1980年5月1日”,则正确的赋值方式为 。*
A) year = 1980; B) t.year = 1980;
month = 5; t.month = 5;
day = 1; t.day = 1;
C) t.birthday.year = 1980; D) t-> birthday.year = 1980;
t.birthday.month = 5; t-> birthday.month = 5;
t.birthday.day = 1; t-> birthday.day = 1;
13.以下结构类型可用来构造链表的是 。*
A) struct aa{ int a;int * b;};
B) struct bb{ int a;bb * b;};
C) struct cc{ int * a;cc b;};
D) struct dd{ int * a;aa b;};
14.以下程序的输出结果是 。**
amovep(int *p, int a[3][3],int n)
{ int i, j;
for( i=0;i<n;i++)
for(j=0;j<n;j++){ *p=a[i][j];p++; }
}
main()
{ int *p,a[3][3]={{1,3,5},{2,4,6}};
p=(int *)malloc(100);
amovep(p,a,3);
printf("%d %d \n",p[2],p[5]);free(p);
}
A) 56 B) 25 C) 34 D) 程序错误
15.以下程序的输出结果是 。**
struct HAR
{ int x, y; struct HAR *p;} h[2];
main()
{ h[0].x=1;h[0].y=2;
h[1].x=3;h[1].y=4;
h[0].p=&h[1].x;
h[1].p=&h[0].x;
printf("%d %d \n",(h[0].p)->x,(h[1].p)->y);
}
A) 12 B) 23 C) 14 D) 32
二、填空题
1.有如下定义:*
struct {int x; int y; }s[2]={{1,2},{3,4}}, ﹡p=s;
则:表达式 ++p->x 的结果是 。
表达式 ++p->x 的结果是 。
2.若有定义:**
struct num {int a; int b; float f; }n={1, 3, 5.0}};
struct num ﹡pn=&n;
则表达式pn->b/n.a﹡++pn->b的值是 ,表达式(﹡pn).a+pn->f的值是 。
3.若要使指针p指向一个double类型的动态存储单元,请填空。*
p= malloc(sizeof(double));
4.设有以下结构类型说明和变量定义,则变量a在内存所占字节数是 。
*
Struct stud
{ char num[6];
int s[4];
double ave;
} a,*p;
三、程序填空题
1.结构数组中存有三人的姓名和年龄,以下程序输出三人中最年长者的姓名和年龄。请填空。*
static struct man
{ char name[20]; int age; }person[ ]={ “li-ming”,18, “wang-hua”,19,”zhang-ping”,20};
main( )
{struct man *p,*q; int old=0; p=person; for( ; (1) )
if(old<p->age) {q=p; (2) ;}
printf(“%s %d”, (3) ); }
2.以下程序段的功能是统计链表中结点的个数,其中first为指向第一个结点的指针(链表不带头结点)。请填空。*
struct link
{char data; struct link *next; };
……
struct link * p, * first; int c=0; p=first;
while ( (1) ) { (2) ; p= (3) ; }
3.已知head 指向一个带头结点的单向链表,链表中每个结点包含数据域(data)和指针域(next),数据域为整型。以下函数求出链表中所有链结点数据域的和值,作为函数值返回。请填空。**
struct link
{int data; struct link *next; };
main( )
{ struct link *head;
.
.
.
sum(head);
.
.
.
}
sum( (1) )
{ struct link *p; int s=0;
p=head->next;
while(p) {s+= (2) ; p= (3) ; }
return(s);
}
4.已知head指向单链表的第一个结点,以下函数完成往降序单向链表中插入一个结点,插入后链表仍有序。请填空。**
# include <stdio.h>
struct student
{int info; struct student *link; };
struct student *insert(struct student * head, struct student * stud)
{ struct student * p0, * p1, * p2; p1=head; p0=stud;
if(head= =NULL) {head=p0; p0->link=NULL;}
else while(p0->info<p1->info)&&(p1->link!=NULL))
{p2=p1; p1=p1->link; }
if(p0->info>=p1->info) { if(head= =p1) { (1) ; head=p0; }
else {p2->link=p0; (2) ; }
}
else {p1->link=p0; (3) ; } return(head);
}
四、读程序写结果题
1.以下程序的运行结果是 。*
struct n {int x; char c; };
main( )
{ struct n a={10, ’x’}; func(a); printf ( “%d,%c”, a.x, a.c); }
func(struct n b)
{b.x=20; b.c=’y’; }
2.以下程序的运行结果是 。*
main( )
{ struct EXAMPLE { struct {int x; int y; }in int a; int b; }e;
e.a=1; e.b=2; e.in.x=e.a * e.b; e.in.y=e.a + e.b; printf(“%d,%d”, e.in.x, e.in.y); }
3.以下程序的运行结果是 。**
main( )
{ static struct s1 {char c[4], *s; }s1={“abc”,”def”};
static struct s2 {char *cp; struct s1 ss1; }s2={“ghi”, {“jkl”, “mno”}};
printf(“%c,%c\n”, s1.c[0], *s1.s);
printf(“%s,%s\n”, s1.c, s1.s);
printf(“%s,%s\n”, s2.cp, s2.ss1.s);
printf(“%s,%s\n”, ++s2.cp, ++s2.ss1.s);
}
4.以下程序的运行结果是 。*
struct s{ int a; float b; char *c; }
main( )
{static struct s x={19,83.5,”zhang”}; struct s *px=&x;
printf(“%d %.1f %s\n”, x.a, x.b,x.c);
printf(“%d %.1f %s\n”, px->a, (*px).b,px->c);
printf(“%c %s\n”, *px->c-1, &px->c[1]);
}
5.以下程序的运行结果是 。*
struct stru {int x; char c; };
main( )
{ struct stru a={10, ’x’},*p=&a; func (p);
printf ( “%d,%c”, a.x, a.c); }
func (struct stru *b)
{b->x=20; b->c=’y’; }
6.以下程序的执行结果是 。*
#include <stdio.h>
struct stu {
int num;
char name[10];
int age;
};
void fun(struct stu *p)
{
printf("%s\n",(*p).name);
}
void main(void)
{
struct stu students[3]={ {9801,"Zhang",20}, {9802,"Wang",19}, {9803,"Zhao",18} };
fun(students+2);
}
五、编程题
1.试利用结构体类型编制一程序,实现输入一个学生的数学期中和期末成绩,然后计算并输出其平均成绩。*
2.试利用指向结构体的指针编制一程序,实现输入三个学生的学号、数学期中和期末成绩,然后计算其平均成绩并输出成绩表。*
3.请编程建立一个带有头结点的单向链表,链表结点中的数据通过键盘输入,当输入数据为-1时,表示输入结束。(链表头结点的data域不放数据,表空的条件是 ph->next = =NULL)。**
4.已知head指向一个带头结点的单向链表,链表中每个结点包含字符型数据域(data)和指针域(next)。请编写函数实现在值为a的结点前插入值为key的结点,若没有值为a的结点,则插在链表最后。**
第三讲 答案
一、选择题
1~5 D D C D C 6~10 A A D B B
11~15 B D B A D
二、填空题
1. 2 3
2. 12 6.0
3. (double *)
4. 22
三、程序填空题
1.(1)p<=person+2
(2)old=p->age
(3)q->name, q->age
2.(1)p!=NULL
(2)c++
(3)p->next
3.(1)struct link *head
(2)p->data
(3)p->next
4.(1)p0->link=head
(2)p0->link=p1
(3)p0->link=NULL
四、读程序写结果
1. 10,x
2. 2,3
3. a,d<cr>abc,def<cr>ghi,mno<cr>hi,no<cr>
4. 19 83.5 zhang<cr>19 83.5 zhang<cr>y hang<cr>
5. 20,y
6. Zhao
二、 编程题
1.main()
{struct study
{int mid;
int end;
int average;
}math;
scanf(“%d %d”,&math.mid,&math.end);
math.average=(math.mid+math.end)/2;
printf(“average=%d\n”,math.average);
}
2.struct stu
{int num;
int mid;
int end;
int ave;
}s[3];
main( )
{struct stu *p;
for(p=s;p<s+3;p++)
{scanf(“%d %d %d”,&(p->num),&(p->mid),&(p->end));
p->ave=(p->mid+p->end)/2;
}
for(p=s;p<s+3;p++)
printf(“%d %d %d %d\n”,p->num,p->mid,p->end,p->ave);
}
3. #include <stdio.h>
struct list {int data; struct list *next; };
struct list *creatlist( )
{struct list *p,*q,*ph;
int a;
ph=(struct list *)malloc(sizeof(struct list));
p=q=ph;
printf(“Input an integer number,enter –1 to the end:\n”);
scanf(“%d”,&a);
while(a!=-1)
{p=(struct list *)malloc(sizeof(struct list));
p->data=a;
q->next=p;
q=p;
scanf(“%d”,&a);
}
p->next=’\0’;
return(ph);
}
main( )
{struct list *head;
head=creatlist();
}
4.typedef char datatype;
typedef struct node
{datatype data;
struct node *next;
}linklist;
INSERT1(linklist *head,datatype a,datatype key)
{linklist *s,*p,*q;
s=malloc(sizeof(linklist));
s->data=key;
q=head; p=head->next;
if(p= =NULL)
{s->next=p; q->next=s; return; }
while((p->data!=a)&&(p->next!=NULL))
{q=p; p=p->next; }
if(p->data= =a)
{s->next=p; q->next=s; }
else
展开阅读全文