结构体和共用体习题.docx

上传人:b****5 文档编号:2933514 上传时间:2022-11-16 格式:DOCX 页数:13 大小:17.53KB
下载 相关 举报
结构体和共用体习题.docx_第1页
第1页 / 共13页
结构体和共用体习题.docx_第2页
第2页 / 共13页
结构体和共用体习题.docx_第3页
第3页 / 共13页
结构体和共用体习题.docx_第4页
第4页 / 共13页
结构体和共用体习题.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

结构体和共用体习题.docx

《结构体和共用体习题.docx》由会员分享,可在线阅读,更多相关《结构体和共用体习题.docx(13页珍藏版)》请在冰豆网上搜索。

结构体和共用体习题.docx

结构体和共用体习题

习题六

1.从下列四个选项中选择一个正确的填入括号中。

(1)在说明一个结构体变量时系统分配给它的存储空间是(D)。

A该结构体中第一个成员所需存储空间

B该结构体中最后一个成员所需存储空间

C该结构体中占用最大存储空间的成员所需存储空间

D该结构体中所有成员所需存储空间的总和

(2)在说明一个共用体变量时系统分配给它的存储空间是(D)。

A该共用体中第一个成员所需存储空间

B该共用体中最后一个成员所需存储空间

C该共用体中占用最大存储空间的成员所需存储空间

D该共用体中所有成员所需存储空间的总和

(3)共用体类型在任何给定时刻,(B)。

A所有成员一直驻留在内存中

B只有一个成员驻留在内存中

C部分成员驻留在内存中

D没有成员驻留在内存中

(4)以下定义结构体类型的变量st1,其中不正确的是(A)

Atypedefstuctstudent

{

intnum;

intage;

}STD;

STDst1;

Bstructstudent

{

intnum,age;

}st1;

Cstruct

{

intnum;

floatage;

}st1;

Dstructstudent

{

intnum;

intage;

};

structstudentst1;

(5)已知职工记录描述为:

structworkers

{

intno;

charname[20];

charsex;

struct

{

intday;

intmonth;

intyear;

}birth;

};

structworkersw;

设变量w中的”生日”应是”1993年10月25日”,下列对”生日”的正确赋值方式是(C)。

Aday=25;month=10;year=1993;

Bw.day=25w.month=10;w.year=1993;

Cw.birth.day=25;w.birth.month=10;w.birth.year=1993;

Dbirth.day=25;birth.month=10;birth.year=1993;

(6)设有如下定义:

structsk

{

inta;

floatb;

}data,*p;

若有p=&data;则对data中的a成员的正确引用是(B)。

A(*p).data.a

B(*p).a

Cp->data.a

Dp.data.a

2.填空

(1)若有以下说明和定义且数组w和变量k已正确赋值,则对w数组中第k个元素中各成员的正确引用形式是w[k-1].b、w[k-1].c、w[k-1].d。

structaa

{

intb;

charc;

doubled;

};

structaaw[10];

intk=3;

(2)若有以下说明和定义,则对x.b成员的另外两种引用形式是x->b->和p.b.。

structst

{

inta;

structst*b;

}*p,x;

p=&x;

3.阅读下面程序,写出运行结果。

(1)98765432,ffffffcc

#include

main()

{

structbyte

{

intx;

chary;

};

union

{

inti[2];

longj;

charm[2];

structbyted;

}r,*s=&r;s->j=0x98765432;

printf("%x,%x\n",s->d.x,s->d.y);

}

(2)结果:

1,minicomputer

#include

structtree

{

intx;

char*s;

}t;

func(structtreet)

{

t.x=10;

t.s="computer";

return(0);

}

main()

{

t.x=1;

t.s="minicomputer";

func(t);

printf("%d,%s\n",t.x,t.s);

}

 

#include..................................................结果:

.0,

structtree

{

intx;

char*s;

}t;

func(structtreet)

{

t.x=10;

t.s="computer";

return(0);

}

main()

{

//t.x=1;

//t.s="minicomputer";

func(t);

printf("%d,%s\n",t.x,t.s);

}

(3)结果:

34,12

#include

main()

{

union

{

chars[2];

inti;

}a;

a.i=0x1234;

printf("%x,%x\n",a.s[0],a.s[1]);

}

(4)结果:

1,2,30

#include

structst

{

intx;

int*y;

}*p;

ints[]={10,20,30,40};

structsta[]={1,&s[0],2,&s[1],3,&s[2],4,&s[3]};

main()

{

p=a;

printf("%d,",p->x);

printf("%d,",(++p)->x);

printf("%d,",*(++p)->y);

}

(5)结果:

8

typedefunion{

longa[2];

intb;

charc[8];

}TY;

TYour;

main()

{

printf(“%d\n”,sizeof(our));

}

4.编写程序输入一个学生记录,记录包含学号、姓名、性别和成绩信息,从键盘输入这

些数据,并且显示出来。

#include

structstudent

{

intno;

charname[20];

charsex[2];

floatscore;

}stu1;

main()

{

printf("*********请输入该学生的学号、姓名、性别和成绩:

\n");

scanf("%d%s%s%f",&stu1.no,stu1.name,stu1.sex,&stu1.score);

printf("学号:

%d\n姓名:

%s\n性别:

%s\n成绩:

%f\n",stu1.no,stu1.name,stu1.sex,stu1.score);

}

/*5.有若干运动员,每个运动员包括编号、姓名、性别、年龄、身高、体重。

如果性别为男,参赛项目为长跑和登山;

如果性别为女,参赛项目为短跑、跳绳。

用一个函数输入运动员信息,用另一个函数输出运动员的信息

,再建立一个函数求所有参赛运动员每个项目的平均成绩。

*/

#include

#include

 

charitems1[2]={'长跑','登山'};

charitems2[2]={'短跑','跳绳'};

voidInputAthInfo();

voidoutputAthInfo();

voiditems_Avg();

structathlete

{

intnum;

charname[10];

charsex;

intage;

floatheight;

floatweight;

charitems[2];

}ath[2];

voidInputAthInfo()

{

inti;

for(i=0;i<2;i++)

{

printf("请输入第%d个运动员的信息:

",i);

scanf("编号:

%d\n姓名:

%s\n性别:

%c\n年龄:

%d\n身高:

%f\n体重:

%f\n",&ath[i].num,ath[i].name,ath[i].sex,&ath[i].age,&ath[i].height,&ath[i].weight);

}

}

voidoutputAthInfo()

{intj;

printf("编号\t姓名\t性别\t年龄\t身高\t体重\t项目\n");

for(j=0;j<2;j++)

{

if(ath[j].sex=='m'||ath[j].sex=='M')

strcpy(ath[j].items,items1);

else

strcpy(ath[j].items,items2);

//printf("第%d个运动员的信息是:

",j)

printf("%d\t%s\t%c\t%d\t%f\t%f\t%s\t%s\t",ath[j].num,ath[j].name,ath[j].sex,ath[j].age,ath[j].height,ath[j].weight,ath[j].items[0],ath[j].items[1]);

}

 

}

voiditems_Avg()

{

inti;

floatsum1=0,sum2=0,avg1,avg2;

for(i=0;i<2;i++)

{

sum1+=ath[i].items[0];

avg1=sum1/2;

sum2+=ath[i].items[1];

avg2=sum2/2;

}

printf("%-3f%-3f",avg1,avg2);

}

 

voidmain()

{

voidInputAthInfo();

voidoutputAthInfo();

voiditems_Avg();

}

 

6.一个班有30名学生,每个学生的数据包括学号、姓名、性别、及2门课的成绩,现从

键盘上输入这些数据,并且要求:

(1)输出每个学生2门课的平均分。

(2)输出每门课的全班平均分。

(3)输出姓名为”zhangliang”的学生的2门课的成绩。

/*6.一个班有30名学生,每个学生的数据包括学号、姓名、性别、及2门课的成绩,现从

键盘上输入这些数据,并且要求:

(1)输出每个学生2门课的平均分。

(2)输出每门课的全班平均分。

(3)输出姓名为”zhangliang”的学生的2门课的成绩。

*/

#include

#include

#defineN2

voidInput_Info();

voidscore_avg();

voideveryStu_avgScore();

voidscore_zhangliang();

voidmain()

{

Input_Info();

score_avg();

everyStu_avgScore();

score_zhangliang();

}

 

structstudent

{

intno;

charname[20];

charsex[2];

floatscore1;

floatscore2;

}stu[N];

voidInput_Info

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1