版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第8章自定义类型8.1结构体8.2联合体8.3枚举8.4类型别名8.5应用8.6本章小结1118.1结构体22先前的数组可以将多个相同类型的数据统一组织起来,方便用户使用。而当多个数据的类型可能不同,但它们在逻辑上有内在联系时,C++提供了一种称为结构体的数据类型来描述这种组合。8.1.1结构体类型定义33结构体数据类型的定义形式如下:struct结构体类型名{ 成员列表;};44
比如一名学生的信息,可以用:学号(num),姓名(name),年龄(age),身高(height)等特征来描述,C++中可以通过如下的声明来描述这样的一个组合类型:structStudent{intnum;//整型变量num表示学号charname[10];//字符数组变量name表示姓名intage;//整型变量age表示年龄floatheight;//浮点变量height表示身高};10010LiFang181.65numnameageheight558.1.2结构体变量定义定义结构体变量的方式分成如下三种:(1)分段声明有名类型和定义变量(2)同步声明有名类型和定义变量(3)同步声明匿名类型和定义变量66分段声明有名类型和定义变量Studentstu1,stu2;//定义结构体变量这里可以用[struct]结构体类型变量名;的方式来定义变量,struct可以省略,比如structStudentstu1,stu2;这种早期的定义方式也是可以的。这种定义结构体变量的方式比较灵活,适用面比较广泛,如果将此结构体声明放在头文件(h)中,则只要在任意需要使用该结构体的地方包含该头文件则就可以方便定义变量了。77
同步声明有名类型和定义变量这种定义方式的语法形式:struct结构体类型名{ 成员列表;}变量名表;这种定义的形式也支持另行基于该结构体类型名定义结构体变量,在只需要在本文件中使用该结构体数据类型时,可以采用该方法来定义结构体类型和结构体变量。88
同步声明匿名类型和定义变量struct{intnum;charname[10];intage;floatheight;}stu1,stu2;这里声明结构体变量的时候没有定义名字,这种方式比较适合这种结构体类型只在一个局部使用,即不需要另行基于该类型创建其他结构体变量。998.1.3结构体变量初始化结构体变量在定义时可以赋初始值,赋值的形式类似于数组的初始化,只是其各项数据类型需要和结构体数据类型定义保持一致。比如:structStudent{intnum;charname[10];intage;floatheight;}stu={1000,"Li",20,175};1010
对于结构体类型定义中包括另外结构体类型定义的情形,可以通过嵌套赋值的方式实现。比如:structDate{intyear;intmonth;intday;};structStudent{intnum;charname[10];intage;floatheight;Dateenrollment;};Studentstu={1000,"Li",20,175,{2000,1,1}};11118.1.4读写结构体变量对于结构体变量,可以有两种读写形式:(1)同一个结构体类型的结构体变量之间可以直接相互赋值。(2)对于结构体变量的成员读写,可以通过成员运算符点”.”进行访问,也就是可以通过结构体变量名.成员名的形式指定到具体的某个变量的成员,然后进行操作.12例8.1比较输出身高较高学生信息structStudent{intnum;charname[10];intage;floatheight;};
intmain(){Studentstu1,stu2;cout<<"Inputthefirststudent:"<<endl;cin>>stu1.num>>>>stu1.age
>>stu1.height;
cout<<"Inputthesecondstudent:"<<endl;
cin>>stu2.num
>>
>>stu2.age
>>stu2.height;
cout<<"Thetallerstudent:"<<endl;
if(stu1.height>=stu2.height)
{
cout<<stu1.num<<'\t'<<<<'\t'<<stu1.age<<'\t'<<stu1.height<<endl;
}
else
{
cout<<stu2.num<<'\t'<<<<'\t'<<stu2.age<<'\t'<<stu2.height<<endl;
}
return0;
}138.1.5函数中的结构体结构体变量可以用在函数中,可以作为函数参数作为函数返回值14
作为函数参数例8.2显示学生信息#include<iostream>usingnamespacestd;structDate{
intyear;
intmonth;
intday;};structStudent{
intnum;
charname[10];
intage;
floatheight;
Dateenrollment;};voidshowStudent(Studentstu){
cout<<"Num:"<<stu.num<<endl
<<"Name:"<<<<endl
<<"Age:"<<stu.age<<endl
<<"Height:"<<stu.height<<endl
<<"Enrollment:"<<stu.enrollment.year
<<"-"
<<stu.enrollment.month
<<"-"
<<stu.enrollment.day;}intmain(){
Studentstu={1000,"zhang",20,175,2000,1,1};
showStudent(stu);
return0;}15作为函数返回值例8.3
输出较高学生姓名structStudent{
intnum;
charname[10];
intage;
floatheight};StudentgetOneStudent(){
Studentstu;
cout<<"Num:"<<endl;
cin>>stu.num;
cout<<"Name:"<<endl;
cin>>;
cout<<"Age:"<<endl;
cin>>stu.age;
cout<<"Height:"<<endl;
cin>>stu.height;
returnstu;}intmain(){
Studentstu1=getOneStudent();
Studentstu2=getOneStudent();
cout<<"Thetallerstudent'sname:";
if(stu1.height>=stu2.height)
{
cout<<;
}
else
{
cout<<;
}
return0;}168.1.6结构体数组当需要操作多个相同结构体类型的变量时,可以用结构体数组来组织管理。结构体数组是每一个元素均为结构体类型的数组,对数组整体的操作仍然和一般数据类型的数组相同,只是当需要对数组元素进行具体操作时,需要清楚其为结构体变量,需要用读写结构体变量的相关技术来访问。
例8.4
显示超过平均身高的学生信息17#include<iostream>usingnamespacestd;structStudent{
intnum;
charname[10];
intage;
floatheight;};
intmain(){
constintLEN=4;
Studentstu[LEN]={{1000,"Zhao",20,175},
{2000,"Qian",21,170},
{3000,"Sun",19,180},
{4000,"Li",22,176}};doubleavg=0;
for(Studenttmp:stu)
{
avg=avg+tmp.height/LEN;
}
cout<<"averageheight:"<<avg<<endl;
for(Studenttmp:stu)
{
if(tmp.height>avg)
{
cout<<tmp.num<<'\t'<<<<'\t'<<tmp.age<<'\t'<<tmp.height<<endl;
}
}
return0;}188.2联合体联合体,又称共用体。在定义形式上与之前的结构体比较类似,可以把多个成员组合在一个统一定义的数据类型中。定义联合体的语法形式如下:union联合类体型名{
成员列表;};例8.5
同成员的结构体和联合体内存容量比较19#include<iostream>usingnamespacestd;intmain(){structsData{inti;charc[8];};unionuData{inti;charc[8];};sDatas1;uDatau1;
cout<<"size"<<endl;cout<<"int:"<<sizeof(int)<<endl<<"char:"<<sizeof(char)<<endl<<"struct:"<<sizeof(s1)<<endl<<"union:"<<sizeof(u1)<<endl;return0;}20例8.5内存占用示意图C++程序设计及项目实践2121例8.6
判断数据存储的大小端#include<iostream>#include<iomanip>usingnamespacestd;intmain(){
unionuData
{
inti;
charc[4];
};
uDatau;
u.i=0x12345678;
for(inti=0;i<4;i++)
{
cout<<hex<<int(u.c[i])<<'\t';
}
if(u.c[0]==0x78)
{
cout<<endl<<"LittleEndian"<<endl;
}
else
{
cout<<endl<<"BigEndian"<<endl;
}
return0;}C++程序设计及项目实践2222
8.3枚举定义枚举类型的语法形式:enum类型名{枚举常量列表};定义之后,可以使用这种类型来定义变量,比如:enumanswer{A,B,C,D};
//定义一道选额题的答案为一种新的枚举类型answeranswerans1,ans2; //基于这种类型定义了两个变量ans1,ans2这里,answer是一种新的数据类型,ans1和ans2是这种数据类型定义的两个变量,ans1和ans2只能取A,B,C或D中的一个值。这里的A,B,C,D是自定义的标识符,表示四个不同的值。C++程序设计及项目实践23例8.7枚举类型简单使用#include<iostream>usingnamespacestd;intmain(){
enumanswer{A,B,C,D};
answerans1=A;
answerans2=D;
cout<<"ans1:"<<ans1<<"\tans2:"<<ans2<<endl;
answertmp=ans1;
ans1=ans2;
ans2=tmp;
cout<<"ans1:"<<ans1<<"\tans2:"<<ans2<<endl;
return0;}C++程序设计及项目实践248.4类型别名为了适应多用户以及多平台下的软件开发,有时需要将已有的数据类型重新取一个名字,也就是类型别名,这样可以更好地协调软件开发或多平台下的软件移植。C++程序设计及项目实践258.4.1#define#define主要用来做宏定义,先前用来定义名称常量即是一种使用情况。还可以用:#define类型别名
现有类型名这种方式将现有的一种数据类型定义成一种新的名字的类型。例8.8
#define类型别名使用示例C++程序设计及项目实践26#include<iostream>usingnamespacestd;structStudent{
intnum;
charname[10];
intage;
floatheight;};#defineSTUDENTStudent#defineBIGINTintintmain(){
STUDENTstu={1000,"zhang",20,175};
BIGINTb=1000;
cout<<stu.num<<'\t'<<<<'\t'<<stu.age<<'\t'<<stu.height<<endl;
cout<<sizeof(b)<<endl;
return0;}C++程序设计及项目实践278.4.2typedef#define定义类型别名本质上是利用了其标识符替换的特征。而typedef定义类型别名则是C++提供的专用别名定义的关键字,其定义别名的形式为:typedef现有类型名
类型别名;注意,定义中所用到的两个名字的顺序和#define刚好相反,这里是别名在后,已有的名字在前,同时这里是一条语句,最后需要加分号“;”。例8.9
typedef别名定义示例C++程序设计及项目实践28#include<iostream>usingnamespacestd;structStudent{
intnum;
charname[10];
intage;
floatheight;};typedefStudentSTUDENT;typedefint
BIGINT;typedefchar
Name[10];intmain(){
STUDENTstu={1000,"zhang",20,175};
BIGINTb=1000;
NameaMan="zhao",bMan="qian",cMan="sun",dMan="li";
cout<<stu.num<<'\t'<<<<'\t'<<stu.age<<'\t'<<stu.height<<endl;
cout<<sizeof(b)<<endl;
cout<<aMan<<'\t'<<bMan<<'\t'<<cMan<<'\t'<<dMan<<endl;
return0;}C++程序设计及项目实践298.4.3using采用using进行类型别名定义语法形式如下:using类型别名=现有类型名;这里采用了赋值语句的形式来定义类型别名,语句的末尾需要有分号。例8.10
using类型别名示例C++程序设计及项目实践30
#include<iostream>
usingnamespacestd;
structStudent
{
intnum;
charname[10];
intage;
floatheight;
};
usingSTUDENT=Student;
usingBIGINT=int;
usingName=char[10];intmain(){STUDENTstu={1000,"zhang",20,175};BIGINTb=1000;
NameaMan="zhao",bMan="qian",cMan="sun",dMan="li";
cout<<stu.num<<'\t'<<<<'\t'<<stu.age<<'\t'<<stu.height<<endl;
cout<<sizeof(b)<<endl;
cout<<aMan<<'\t'<<bMan<<'\t'<<cMan<<'\t'<<dMan<<endl;
return0;
}C++程序设计及项目实践308.5应用例8.11文明城市投票:现有5个文明城市,以及用户的投票列表20个,需要统计这5个文明城市获得选票的情况,用户投票列表中的名单可能会有大小写不一致的情况,最终需要根据城市得票数由高到低排序显示城市名称和得票数。C++程序设计及项目实践32structCity{
charname[N];
intvote;};usingCITY=City;boolisequal(charstr1[],charstr2[],intn){
boolis=true;
for(inti=0;i<n;i++)
{
if(str1[i]==str2[i]||
str1[i]-str2[i]==32||
str1[i]-str2[i]==-32)
continue;
else{
is=false;
break;
}}
returnis;}intmain(){constintLEN=5;constintNUM=20;CITYcitys[LEN]={{"beijing",0},{"shanghai",0},{"tianjin",0},{"chongqing",0},{"xuzhou"}};charvotes[NUM][N]={"beijing","shanghai","tianjin","chongqing","ShangHai","beijing","shanghai","tianJin","beiJing","Xuzhou","beijing","shanghai","Shanghai","chongqing","xuZhou","beijing","Beijing","tianjin","XUZHOU","XuZhou"};for(inti=0;i<NUM;i++){for(intj=0;j<LEN;j++){if(isequal(votes[i],citys[j].name,N)){citys[j].vote++;break;}}}for(intround=1;round<LEN;round++){for(inti=0;i<LEN-round;i++){if(citys[i].vote<citys[i+1].vote){CITYctemp=citys[i];citys[i]=citys[i+1];citys[i+1]=ctemp;}}}for(inti=0;i<LEN;i++){cout<<citys[i].vote<<'\t'<<citys[i].name<<endl;}C++程序设计及项目实践33例8.12纸牌花色组合:从4个不同花色的纸牌中任意选两张纸牌,求其具体花色纸牌组合形式。利用枚举表示花色。C++程序设计及项目实践34#include<iostream>usingnamespacestd;enumCard{Spade,Heart,Diamond,Club};voidshowCard(Cardc){
switch(c)
{
caseSpade:
cout<<"Spade";
break;
caseHeart:
cout<<"Heart";
break;
caseDiamond:
cout<<"Diamond";
break;
caseClub:
cout<<"Club";
break;
}}intmain(){
intn=0;
for(intc1=Spade;c1<=Club;c1++)
{
for(intc2=Spade;c2<=Club;c2++){
if(c1==c2)
continue;
cout<<++n<<":\t";
showCard(Card(c1));
cout<<'\t';
showCard(Card(c2));
cout<<endl;}
}
return0;}C++程序设计及项目实践35例8.13显示入学日期相同的学生,利用结构体数据类型表示学生信息。C++程序设计及项目实践36structDate{intyear;intmonth;intday;};structStudent{intnum;charname[10];Dateenrollment;};boolisSameDay(Studentstu1,Studentstu2){if(stu1.enrollment.year==stu2.enrollment.year&&stu1.enrollment.month==stu2.enrollment.month&&stu1.enrollment.day==stu2.enrollment.day){returntrue;}else{returnfalse;}}voidshowDay(Studentstu){cout<<stu.enrollment.year
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026主管口腔技师模拟冲刺题目及答案
- 2026年计算机科学与技术(人工智能应用)专业期末考试综合模拟试题及参考答案
- 2026年景观材料模拟考试卷带答案
- 2026年药理模拟试题及参考答案
- Parthenocissin-A-生命科学试剂-MCE
- 2026年自学考试刑法模拟试题及答案
- Olmesartan-impurity-15-生命科学试剂-MCE
- 2026年保险知识考试卷及答案
- 2026年汾西矿业模拟考试卷及答案
- 2026年慢病知识培训试题带答案
- 营口新山鹰大空间灭火监控系统培训讲解
- 新警安全保密教育培训课件
- 中药保留灌肠课件
- 2025年数智供应链案例集-商务部
- 学校保安闭环管理制度
- 北京市科技计划项目(课题)结题经费审计工作底稿-参考文本
- 电子商务物流说课省公开课一等奖全国示范课微课金奖课件
- CJ/T 123-2016 给水用钢骨架聚乙烯塑料复合管
- 中国法律史-第二次平时作业-国开-参考资料
- (正式版)SHT 3551-2024 石油化工仪表工程施工及验收规范
- 水处理药剂采购投标方案(技术标)
评论
0/150
提交评论