结构体和共用体习题Word文件下载.docx
《结构体和共用体习题Word文件下载.docx》由会员分享,可在线阅读,更多相关《结构体和共用体习题Word文件下载.docx(13页珍藏版)》请在冰豆网上搜索。
};
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
structst*b;
}*p,x;
p=&
x;
3.阅读下面程序,写出运行结果。
(1)98765432,ffffffcc
#include<
stdio.h>
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
structtree
char*s;
}t;
func(structtreet)
t.x=10;
t.s="
computer"
;
return(0);
main()
t.x=1;
minicomputer"
func(t);
%d,%s\n"
t.x,t.s);
..................................................结果:
.0,<
null>
//t.x=1;
//t.s="
t.x,t.s);
(3)结果:
34,12
chars[2];
inti;
}a;
a.i=0x1234;
a.s[0],a.s[1]);
(4)结果:
1,2,30
int*y;
}*p;
ints[]={10,20,30,40};
structsta[]={1,&
s[0],2,&
s[1],3,&
s[2],4,&
s[3]};
p=a;
%d,"
p->
x);
printf("
(++p)->
*(++p)->
y);
(5)结果:
8
typedefunion{
longa[2];
intb;
charc[8];
}TY;
TYour;
printf(“%d\n”,sizeof(our));
4.编写程序输入一个学生记录,记录包含学号、姓名、性别和成绩信息,从键盘输入这
些数据,并且显示出来。
#include<
structstudent
intno;
charname[20];
charsex[2];
floatscore;
}stu1;
*********请输入该学生的学号、姓名、性别和成绩:
\n"
);
scanf("
%d%s%s%f"
&
stu1.no,stu1.name,stu1.sex,&
stu1.score);
学号:
%d\n姓名:
%s\n性别:
%s\n成绩:
%f\n"
stu1.no,stu1.name,stu1.sex,stu1.score);
/*5.有若干运动员,每个运动员包括编号、姓名、性别、年龄、身高、体重。
如果性别为男,参赛项目为长跑和登山;
如果性别为女,参赛项目为短跑、跳绳。
用一个函数输入运动员信息,用另一个函数输出运动员的信息
,再建立一个函数求所有参赛运动员每个项目的平均成绩。
*/
#include<
string.h>
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);
编号:
%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;
编号\t姓名\t性别\t年龄\t身高\t体重\t项目\n"
);
for(j=0;
j<
j++)
if(ath[j].sex=='
m'
||ath[j].sex=='
M'
)
strcpy(ath[j].items,items1);
else
strcpy(ath[j].items,items2);
//printf("
第%d个运动员的信息是:
j)
%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()
floatsum1=0,sum2=0,avg1,avg2;
for(i=0;
sum1+=ath[i].items[0];
avg1=sum1/2;
sum2+=ath[i].items[1];
avg2=sum2/2;
%-3f%-3f"
avg1,avg2);
voidmain()
voidInputAthInfo();
voidoutputAthInfo();
voiditems_Avg();
6.一个班有30名学生,每个学生的数据包括学号、姓名、性别、及2门课的成绩,现从
键盘上输入这些数据,并且要求:
(1)输出每个学生2门课的平均分。
(2)输出每门课的全班平均分。
(3)输出姓名为”zhangliang”的学生的2门课的成绩。
/*6.一个班有30名学生,每个学生的数据包括学号、姓名、性别、及2门课的成绩,现从
#defineN2
voidInput_Info();
voidscore_avg();
voideveryStu_avgScore();
voidscore_zhangliang();
Input_Info();
score_avg();
everyStu_avgScore();
score_zhangliang();
floatscore1;
floatscore2;
}stu[N];
voidInput_Info()
*************输入该班所有人的信息:
(学号、姓名、性别、数学成绩、英语成绩,并用空格隔开)\n"
//printf("
学号\t姓名\t性别\t数学\t英语"
N;
请输入第%d个人的信息:
scanf("
%d%s%s%f%f"
stu[i].no,stu[i].name,stu[i].sex,&
stu[i].score1,&
stu[i].score2);
voideveryStu_avgScore()//
(1)输出每个学生2门课的平均分。
floatoneself_avg[N]={0.0f};
oneself_avg[i]=(stu[i].score1+stu[i].score2)/2;
for(i=0;
第%d个学生的两门课平均分为%5.2f\n"
i,oneself_avg[i]);
voidscore_avg()//
(2)输出每门课的全班平均分
floatsum1=0.0f,avg1,sum2=0.0f,avg2;
sum1+=stu[i].score1;
sum2+=stu[i].score2;
avg1=(sum1)/2;
avg2=(sum2)/2;
该班的数学平均成绩为%5.2f,英语平均分为%5.2f\n"
voidscore_zhangliang()//(3)输出姓名为”zhangliang”的学生的2门课的成绩。
if(stu[i].name=="
zhangliang"
//printf("
\nzhangliang同学的两门课成绩分别为:
%5.2f%5.2f"
stu[i].score1,stu[i].score2);
7.定义枚举类型money,用枚举元素代表人民币的面值。
包括1,2,5分;
1,2,5角,
1,2,5,10,50,100元。
enummoney
fen1,fen2,fen5,jiao1,jiao2,jiao5,yuan1,yuan2,yuan5,yuan10,yuan20,yuan50,yuan100