结构体和共用体习题.doc

上传人:scccc 文档编号:12038259 上传时间:2021-12-01 格式:DOC 页数:13 大小:40.50KB
返回 下载 相关 举报
结构体和共用体习题.doc_第1页
第1页 / 共13页
结构体和共用体习题.doc_第2页
第2页 / 共13页
结构体和共用体习题.doc_第3页
第3页 / 共13页
结构体和共用体习题.doc_第4页
第4页 / 共13页
结构体和共用体习题.doc_第5页
第5页 / 共13页
点击查看更多>>
资源描述

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

1、习 题 六1. 从下列四个选项中选择一个正确的填入括号中。(1)在说明一个结构体变量时系统分配给它的存储空间是( D)。A该结构体中第一个成员所需存储空间B该结构体中最后一个成员所需存储空间C该结构体中占用最大存储空间的成员所需存储空间D该结构体中所有成员所需存储空间的总和(2)在说明一个共用体变量时系统分配给它的存储空间是(D )。A该共用体中第一个成员所需存储空间B该共用体中最后一个成员所需存储空间C该共用体中占用最大存储空间的成员所需存储空间D该共用体中所有成员所需存储空间的总和(3)共用体类型在任何给定时刻, ( B) 。A所有成员一直驻留在内存中B只有一个成员驻留在内存中C部分成员驻

2、留在内存中D没有成员驻留在内存中(4)以下定义结构体类型的变量st1,其中不正确的是(A )A typedef stuct studentint num;int age;STD;STD st1;B struct student int num,age;st1;C struct int num;float age;st1;D struct studentint num;int age;struct student st1;(5)已知职工记录描述为:struct workers推荐精选int no;char name20;char sex;structint day;int month;int y

3、ear;birth;struct workers w;设变量w中的”生日”应是”1993年10月25日”,下列对”生日”的正确赋值方式是( C)。A day=25; month=10; year=1993;B w.day=25w.month=10; w.year=1993;C w.birth.day=25; w.birth.month=10; w.birth.year=1993;D birth.day=25; birth.month=10; birth.year=1993;(6)设有如下定义:struct skint a;float b;data,*p;若有p=&data;则对data

4、中的a成员的正确引用是( B)。A (*p).data.aB (*p).aC p->data.aD p.data.a2.填空(1)若有以下说明和定义且数组w和变量k已正确赋值,则对w数组中第k个元素中各成员的正确引用形式是 wk-1.b、 wk-1.c 、 wk-1.d 。struct aaint b;char c;double d;struct aa w10;int k=3;(2)若有以下说明和定义,则对x.b成员的另外两种引用形式是 x->b-> 和 p.b. 。struct stint a;推荐精选struct st *b;*p, x;p=&x;3.阅读下面程序

5、,写出运行结果。(1)98765432,ffffffcc#include <stdio.h>main( )struct byteint x;char y;unionint i2;long j;char m2;struct byte d;r,*s=&r;s->j=0x98765432;printf("%x,%xn",s->d.x,s->d.y);(2)结果:1,minicomputer#include <stdio.h>struct treeint x;char *s;t;func(struct tree t )t.x=10;

6、t.s="computer"return(0);main()t.x=1;t.s="minicomputer"func(t);printf("%d,%sn",t.x,t.s);推荐精选#include <stdio.h>.结果:.0,<null>struct treeint x;char *s;t;func(struct tree t )t.x=10;t.s="computer"return(0);main()/t.x=1;/t.s="minicomputer"func(t

7、);printf("%d,%sn",t.x,t.s);(3)结果:34,12#include <stdio.h>main()unionchar s2;int i;a;a.i=0x1234;printf("%x,%xn",a.s0,a.s1);(4)结果:1,2,30#include <stdio.h>struct stint x;int *y;*p;int s=10,20,30,40;struct st a=1,&s0,2,&s1,3,&s2,4,&s3;main()p=a;推荐精选printf(&

8、quot;%d,",p->x); printf("%d,",(+p)->x);printf("%d,",*(+p)->y);(5)结果:8typedef union long a2; int b; char c8;TY;TY our;main() printf(“%dn”,sizeof(our);4. 编写程序输入一个学生记录, 记录包含学号、姓名、性别和成绩信息,从键盘输入这些数据,并且显示出来。 #include<stdio.h>struct student int no; char name20; char

9、sex2; float score;stu1;main()printf("*请输入该学生的学号、姓名、性别和成绩:n");scanf("%d %s %s %f",&stu1.no,stu1.name,stu1.sex,&stu1.score);printf("学号:%dn 姓名:%sn 性别:%sn 成绩:%fn",stu1.no,stu1.name,stu1.sex,stu1.score); /*5.有若干运动员,每个运动员包括编号、姓名、性别、年龄、身高、体重。如果性别为男,参赛项目为长跑和登山;如果性别为女,参赛

10、项目为短跑、跳绳。用一个函数输入运动员信息,用另一个函数输出运动员的信息,再建立一个函数求所有参赛运动员每个项目的平均成绩。*/#include<stdio.h>#include<string.h>char items12='长跑','登山'推荐精选char items22='短跑','跳绳'void InputAthInfo();void outputAthInfo();void items_Avg();struct athleteint num;char name10;char sex;int age;

11、 float height;float weight;char items2;ath2; void InputAthInfo() int i;for( i=0;i<2;i+)printf("请输入第%d个运动员的信息:",i); scanf("编号:%dn 姓名:%sn 性别: %cn 年龄:%dn 身高: %fn 体重:%fn ",&athi.num ,athi.name,athi.sex,&athi.age,&athi.height,&athi.weight); void outputAthInfo() int

12、j;printf("编号t 姓名t 性别t 年龄t 身高t 体重t 项目n" );for(j=0;j<2;j+)if(athj.sex='m'|athj.sex='M')strcpy(athj.items ,items1);else strcpy(athj.items,items2);/printf("第%d个运动员的信息是:",j) printf("%dt %st %c t%dt %ft %ft %st %st",athj.num,athj.name,athj.sex,athj.age,athj

13、.height,athj.weight,athj.items0,athj.items1); 推荐精选void items_Avg()int i;float sum1=0,sum2=0,avg1,avg2;for(i=0;i<2;i+)sum1+=athi.items0;avg1=sum1/2;sum2+=athi.items1;avg2=sum2/2;printf("%-3f%-3f",avg1,avg2);void main() void InputAthInfo(); void outputAthInfo(); void items_Avg(); 6.一个班有30

14、名学生,每个学生的数据包括学号、姓名、性别、及2门课的成绩,现从键盘上输入这些数据,并且要求:(1) 输出每个学生2门课的平均分。(2) 输出每门课的全班平均分。(3) 输出姓名为”zhangliang”的学生的2门课的成绩。/*6.一个班有30名学生,每个学生的数据包括学号、姓名、性别、及2门课的成绩,现从推荐精选键盘上输入这些数据,并且要求:(1) 输出每个学生2门课的平均分。(2) 输出每门课的全班平均分。(3) 输出姓名为”zhangliang”的学生的2门课的成绩。*/ #include<stdio.h> #include<string.h> #define

15、N 2void Input_Info();void score_avg();void everyStu_avgScore();void score_zhangliang();void main() Input_Info(); score_avg(); everyStu_avgScore(); score_zhangliang();struct student int no; char name20; char sex2; float score1;float score2;stuN; void Input_Info()int i;printf("*输入该班所有人的信息:(学号、姓名、

16、性别、 数学成绩、 英语成绩,并用空格隔开)n"); /printf("学号 t 姓名t 性别t 数学 t 英语");for(i=0;i<N;i+) printf("请输入第%d个人的信息:n",i);scanf("%d %s %s %f %f",&stui.no,stui.name,stui.sex,&stui.score1,&stui.score2);推荐精选void everyStu_avgScore()/(1) 输出每个学生2门课的平均分。float oneself_avgN=0.0f;

17、int i;for(i=0;i<N;i+)oneself_avgi=(stui.score1+stui.score2)/2;for(i=0;i<N;i+) printf("第%d个学生的两门课平均分为%5.2fn",i,oneself_avgi);void score_avg()/(2) 输出每门课的全班平均分float sum1=0.0f,avg1,sum2=0.0f,avg2;int i;for(i=0;i<N;i+)sum1+=stui.score1; sum2+=stui.score2;avg1=(sum1)/2;avg2=(sum2)/2;pri

18、ntf("该班的数学平均成绩为%5.2f,英语平均分为%5.2fn",avg1,avg2);void score_zhangliang()/(3) 输出姓名为”zhangliang”的学生的2门课的成绩。 int i; for(i=0;i<N;i+) if(stui.name="zhangliang") / printf("n"); printf("n zhangliang同学的两门课成绩分别为:%5.2f %5.2f",stui.score1,stui.score2 ); 推荐精选7. 定义枚举类型money,用枚举元素代表人民币的面值。包括1,2,5分;1,2,5角,1,2,5,10,50,100元。 enum moneyfen1,fen2,fen5,jiao1,jiao2,jiao5,yuan1,yuan2,yuan5,yuan10,yuan20,yuan50,yuan100 (注:可编辑下载,若有不当之处,请指正,谢谢!) 推荐精选

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

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1