第6章 自定义数据类型思考与练习题答案Word文档下载推荐.docx
《第6章 自定义数据类型思考与练习题答案Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《第6章 自定义数据类型思考与练习题答案Word文档下载推荐.docx(10页珍藏版)》请在冰豆网上搜索。
string.h>
typedefstruct{
charNumber[12];
//存放学生学号
charName[12];
//存放学生姓名
//intEnglishScore;
//存放学生英语成绩
intMathScore;
//存放学生数学成绩
intCPPScore;
}student;
voidmain()
{
intn=0;
studentst[10];
//说明一个Student的结构体变量数组
student*p;
p=st;
cout<
<
"
请输入学生的人数内n="
;
cin>
>
n;
for(inti=0;
i<
i++)
{
cout<
请输入第"
i+1<
学生的学号:
cin>
p->
Number;
学生的名字:
Name;
学生数学成绩:
MathScore;
学生c++成绩:
p->
CPPScore;
p++;
}
endl;
学号"
<
"
姓名"
数学"
C++"
endl;
for(intj=0;
j<
j++)//输出成绩
p--;
Number<
Name<
p->
MathScore<
CPPScore<
}
6、若定义结构体数据类型和说明其变量如下
structMyData
{
inta;
floatb;
charc[3]
};
MyDatax={10,1.0f,"
ab"
},*y=&
x;
则经过x.c[1]++,(y->
b)+=10+x.a++;
运算,x的各分量结果是什么?
{11,21.0,"
ac"
7、若定义联合体数据类型如下所示:
unionMyUnion
charx[2];
inty;
//设int占4个字节
则经过MyUnionMyKey;
*pKey=&
MyKey;
pKey->
y=0,pKey->
x[0]='
1'
;
运算之后,表达式(*pKey).y的结果是什么?
结果是:
49。
8、阅读下列程序,写出结果。
(1)#include<
stdio.h>
stdlib.h>
structStudent
charname[20];
intnum;
floatscore[3];
voidprint(structStudent*p)
num<
\n"
name<
score[0]<
<
score[1]<
score[2]<
structStudentstu;
stu.num=12345;
strcpy(stu.name,"
lili"
);
stu.score[0]=67.5;
stu.score[1]=89;
stu.score[2]=78.6;
print(&
stu);
运行结果是:
12345
lili
67.5
89
78.6
(2)#include<
{
intnum;
charname[10];
intage;
voidfun(Student*p)
{cout<
(*p).name<
}
voidmain(void)
Studentstu[3]={{9801,"
Zhang"
20},{9802,"
Long"
21},{9803,"
Xue"
19}};
fun(stu+2);
xue
(3)#include<
structab
{chara;
floatb;
};
voidf(ab&
y)
{y.a='
W'
y.b=90.5;
abx={'
L'
89.5};
f(x);
cout<
x.a<
'
\t'
x.b<
W90.5
(4)#include<
enumcolor{RED=1,GREEN=6,BLUE=9};
enumcolorv1=RED,v2;
RED="
RED<
GREEN="
GREEN<
BLUE="
BLUE<
v1="
v1<
v2=GREEN;
(v1<
v2)="
(v1<
v2)<
RED=1
GREEN=6
BLUE=9
v1=1
v2)=1
9、定义一个日期结构体数据类型,由年、月、日3个整型数据组成,用该结构体类型说明一个结构体变量并初始化为一个日期,计算该日期是本年度的第几日。
#include<
structdate//定义包含年月日的结构体
intyear;
intmonth;
intday;
boolTest(dated)//判断输入的年月日是否有误
boolk=true;
if(d.year<
1)
{
k=false;
你输入的年月日有误"
if((d.month<
1)||(d.month>
12))
if((d.month==1)||(d.month==3)||(d.month==5)||(d.month==7)
||(d.month==8)||(d.month==10)||(d.month==12))
if((d.day<
1)||(d.day>
31))
{
k=false;
cout<
if((d.month==4)||(d.month==6)||(d.month==9)||
(d.month==11))
30))
if(d.month==2)
if(((d.year%4)==0)&
&
((d.year%100)!
=0))
{
if((d.month<
29))
{
k=false;
cout<
}
}
elseif((d.month<
28))
k=false;
cout<
returnk;
intmonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
boolleapyear(dated)//判断输入的年是否为润年
if(((d.year%100==0)&
(d.year%400==0))||((d.year%100!
=0)&
(d.year%4==0)))
returntrue;
else
returnfalse;
intaccount(dated)//计算输入日期为该年的第几天
inti;
intsum=0;
for(i=0;
d.month;
sum+=month[i-1];
if(leapyear(d)&
d.month>
2)
sum=sum+1;
sum=sum+d.day;
returnsum;
inty,m,d;
cout<
请输入年月日"
cin>
y>
m>
d;
dateda;
da.year=y;
da.month=m;
da.day=d;
if(Test(da))
你输入的日期是本年的"
account(da)<
10、使用typedef定义一个职工结构类型的类型,然后定义一个该自定义类型的变量,该职工包括编号、姓名、性别、出生日期和住址。
日期包括年、月、日。
〕
答:
typedefstruct{//定义生日的结构体
intyear;
intmonth;
intday;
}birthday;
typedefstruct{//定义职工的结构体
charnumber[100];
charname[100];
charsex[100];
charaddress;
birthdaybirt;
}worker;
workerw;
请输入职工的编号:
cin>
w.number;
请输入职工的姓名:
cin>
w.name;
请输入职工的性别:
w.sex;
请输入职工的住址:
w.address;
请输入职工的出生年月:
w.birt.year>
w.birt.month>
w.birt.day;
编号:
性别"
住址"
出生年月"
w.number<
w.name<
w.sex<
w.address<
w.birt.year<
."
w.birt.month<
w.birt.day<