网站大量收购独家精品文档,联系QQ:2885784924

第6章 构造数据类型.ppt

  1. 1、本文档共27页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
6.1 结构体类型 6.2 联合体 6.3 枚举类型 6.4 用typedef定义类型 例:输入5个学生的姓名、性别和三科成绩,求每个同学的平均分. 例:结构体变量做函数参数 * 6.1.1 结构体类型的定义 08012 LiFun M 19 87.5 80 id name sex age E M struct student { char id[10]; char name[20]; char sex; int age; double score[3]; double aver; }; 结构是由一系列相同类型或不同类型数据构成的集合。结构中的每个数据称为结构的成员 struct 结构类型名 { 数据类型 成员名1; 数据类型 成员名2 …... 数据类型 成员名n }; 75 C 80.8 aver 6.1.2 结构体类型变量的定义 先定义结构体类型再定义变量名(struct可省略) struct student stu1,stu2; student stu1,stu2; 在定义类型的同时定义变量 struct student { char id[10]; char name[20]; char sex; int age; double score[3]; double aver; } stu1,stu2; struct { char id[10]; char name[20]; char sex; int age; double score[3]; double aver; } stu1,stu2; 结构类型的定义说明了该数据类型的组成,要使用该结构,必须定义结构类型的变量 无名定义 (不定义结构体名),只能定义一次。 struct student { char id[10]; char name[20]; char sex; int age; double score[3]; double aver; } stu1={“08012”, “zhangli”,’F’,19,67,86,79}; #includeiostream.h void main() { coutstu1.idstu1.namestu1.sex; for(int i=0;i3;i++) coutstu1.score[i]; } 初始数据个数、顺序和类型应与结构成员一一对应 6.1.3 结构体变量的初始化 struct student { char id[10]=“08012”; char name[20]=“zhangli”; char sex=‘F’; int age=19; double score[3]={67,86,79}; double aver; } stu1; Wrong! 不能在定义类型时初始化 初始化时可只给部分成员赋值,但是不能间隔赋值。另在定义类型时不能对各成员进行初始化。 访问结构体变量的成员 结构体变量名 . 成员名 “.”是成员运算符,优先级最高,左结合性 例如: stu1.id=10010; stu1.age++; coutstu1.id; stu1.aver=(stu1.score[0]+stu1.score[1]+score[2])/3; 结构体变量可以整体赋值 stu1=stu2 6.1.4 结构体变量的成员的访问 结构体可以嵌套 struct date { int month; int day; int year; }; struct student { char id[10]; char name[20]; char sex; struct date birthday; int age; double score[3]; double aver; } stu1,stu2; id name sex age birthday age month day year stu1.birthday.month=11; coutstu1.birthday.month; 初始化: student stu1 ={“08012”, “zhangli”,’F’, {5,24,1976}, 19,67,86,79}; 使用: 定义: score[0] aver score[1] score[2] 定义: 每个数组元素都是一个结构体类型的数据 struct student { int id; char name[20]; char sex; }; student st

文档评论(0)

PPT精品 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档