




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第七章 用户自定义数据类型7.1 结构体类型7.1.1 结构体的概述一个学生的学号、姓名、性别、年龄、成绩、家庭住址 num name sex age score addr 10010 Li Fun M 18 87.5 BeiJing声明一个新的结构体的类型:struct Studentint num;char name20;char sex;int age;float score;char addr30;7.1.2 结构体类型变量的定义方法及其初始化1. 定义结构体变量的方法(1) 先声明结构体的类型再定义变量名Student student1,student2;(2) 声明类型的同时定义变
2、量struct Studentint num;char name20;char sex;int age;float score;char addr30;std1,std2;(3) 直接定义结构体类型变量struct int num;char name20;char sex;int age;float score;char addr30;std1,std2;(4) 成员也可以是一个结构体变量struct Dateint month;int day;int year;struct Studentint num;char name20;char sex;int age;Date birthday;f
3、loat score;char addr30;2. 结构体变量的初始化struct Studentint num;char name20;char sex;int age;float score;char addr30;student1=10001,Zhang Xin,M,19,90.5,shanghai;Student student2=10002,Wang Li,F,20,98,Beijing;7.1.3 引用结构体变量(1) 可以将一个结构体变量的值赋给另一个具有相同结构的结构体变量。student1=student2;(2) 可以引用一个结构体变量中的一个成员的值。student1.n
4、um=10010; .是成员运算符,它的优先级最高。(3) 对于结构体嵌套,要逐级引用。student1.birthday.month=11;(引用student1中birthday中的month成员)。(4) 不能将一个结构体变量作为一个整体进行输入和输出。(5) 对于结构体变量的成员可以像普通变量一样进行各种运算。(6) 可以引用结构体变量成员的地址,也可以引用结构体变量的地址。cout&student1;cout&student1.age;例7.1 引用结构体变量中的成员 P199#include using namespace std;struct Dateint month;int
5、day;int year;struct Studentint num;char name20;char sex;Date birthday;float score;char addr30;student1,student2=10002,Wang Li,F,5,23,1982,89.5;void main()student1=student2;coutstudent1.numendl;endl;coutstudent1.sexendl;coutstudent1.birthday.month/student1.birthday.day/student1.birth
6、day.yearendl;coutstudent1.scoreendl;10002Wang LiF5/23/1982 结构体数组1. 定义结构体数组struct Studentint num;char name20;char sex;int age;float score;char addr30;stu3;Student x8;2. 结构体数组的初始化Student y2=10101,Li Lin,M,18,87.5,103 Beijing Road ,10102,Zhang Fun,M19,99,130 Shanghai Road;3. 结构体数组应用举例例7.2 对候选人
7、得票统计程序.P202#include using namespace std;struct Personchar name20;int count;void main()Person leader3=Li,0,Zhang,0,Fun,0;int i,j;char leader_name20;for(i=0;ileader_name;for(j=0;j3;j+)if(strcmp(leader_name,)=0)leaderj.count+;break;for(i=0;i3;i+):leaderi.countendl;ZhangLiFun
8、LiZhangLiZhangLiFunWangLi:4Zhang:3Fun:27.1.5 指向结构体变量的指针1. 通过指向结构体变量的指针引用结构体变量中的成员例7.3 指向结构体变量的指针的应用 P204#include #include using namespace std;void main()struct Studentint num;string name;char sex;float score;Student stu;Student *p=&stu;stu.num=10301;=Wang Fun;stu.sex=F;stu.score=89.5;coutstu
9、.num stu.sex stu.scoreendl;cout(*p).num (*p).name (*p).sex (*p).scoreendl;coutnum name sex score是指向运算符,即指向结构体变量运算符。请分析以下几种运算:p-np-n+p-n#include #include using namespace std;void main()struct Studentint num;string name;char sex;float score;Student stu;Student *p=&stu;stu.num=10301;coutnumend
10、l;coutnum+endl;coutnumendl;coutstu.numendl;103011030110303103032. 用结构体变量和指向结构体变量的指针构成链表struct Studentint num;float score;Student *next;利用指向自己的指针构成链表。例7.4 P206#define NULL 0#include using namespace std;struct Studentint num;float score;Student *next;void main()Student a,b,c,*head,*p;a.num=31001;a.sco
11、re=89.5;b.num=31003;b.score=90;c.num=31007;c.score=85;head=&a;a.next=&b;b.next=&c;c.next=NULL;p=head;while(p!=NULL)coutnum scorenext;31001 89.531003 9031007 857.1.6 结构体类型数据作为函数参数例7.5(1) 用结构体变量作函数参数。#include #include using namespace std;struct Studentint num;char name20;float score3;void main()void p
12、rint(Student);Student stu;stu.num=12345;strcpy(,Li Feng);stu.score0=67.5;stu.score1=89;stu.score2=78.5;print(stu);void print(Student stu)coutstu.num stu.score0 stu.score1 stu.score2endl;12345 Li Feng 67.5 89 78.5(2) 用指向结构体变量的指针作实参数。#include #include using namespace std;struct Studen
13、tint num;char name20;float score3;void main()void print(Student*);Student stu=12345,Li Feng,67.5,89,78.5;print(&stu);void print(Student *p)coutnum name score0 score1 score2endl;12345 Li Feng 67.5 89 78.5(3) 用结构体变量的引用做函数参数。#include #include using namespace std;struct Studentint num;string name;float
14、score3;void main()void print(const Student &);Student stu;stu.num=12345;=Li Feng;stu.score0=67.5;stu.score1=89;stu.score2=78.5;print(stu);void print(const Student &stu)coutstu.num stu.score0 stu.score1 stu.score2endl;12345 Li Feng 67.5 89 用new和delete运算符进行动态分配和撤销内存空间int *p=
15、new int;delete p;int *p=new int(100);delete p;char *pc=new char10;delete pc;/删除数组int (*pp)4=new int54;deletepp;/删除数组例7.6 开辟空间存放一个结构体变量。#include using namespace std;struct Studentint num;string name;char sex;void main()Student *p;p=new Student;p-num=10123;p-name=Wang Fun;p-sex=M;coutname num sexendl;
16、delete p;Wang Fun 10123 M new和delete运算符与含有指向自己的指针的结构体,就可以实现动态链表,在数据结构课中要详细介绍。7.2 共用体类型7.2.1 共用体的概念几个不同的变量共占同一段内存的结构,称为共用体(有的书称之为联合)。7.2.2 对共用体变量的访问方式【例】演示联合的例子。这里exam是个联合,为说明数据成员val和h开始于同一地址,考虑下面的程序:#include #includeunion examshort val; char h2;void main()exam var;var.h0= A;coutThe value of var.val:
17、var.valendl;coutThe value of var.h0:var.h0endl;var.val=66;var.h1=A;coutvar.valendl;coutvar.h0endl;coutvar.h1endl;/65*256+66=16706The value of var.val:65The value of var.h0:A16706BA7.2.3 共用体类型数据的特点(1) 每一瞬时只有一个成员起作用,其它成员不起作用。(2) 共用体变量的地址和它的各个成员的地址都是同一地址。不能对共用体变量赋值;不能企图引用变量名来得到一个值;不能在定义共用体变量时对它进行初始化;不能
18、用共用体变量作为函数参数。7.3 枚举类型如果一个变量只有几中可能的值,可以定义为枚举类型 enum colorRED,BLUE,GREEN;关键字enum标志枚举类型定义开始,分号标志其结束。在C+中允许不写enum;enum后的标识符color称为枚举类型名。枚举元素(枚举常量):花括号内用逗号隔开的标识符是为这个类型定义的常量,枚举元素的缺省赋值:编译器为这些枚举常量赋予不同的值,第一个常量RED值为0,以后的常量的值是它前面常量的值增1,所以,BLUE为1,GREEN为2 。声明枚举变量:枚举标记可以用类型名说明具有该类型的变量,例如: color CorVariable;上面的语句说
19、明了变量CorVariable,可以将说明类型color时所列举的枚举常量中的任何一个置给变量CorVariable , 例如: CorVariable=GREEN; coutCorVariableendl;#includevoid main()enum colorRED,BLUE,GREEN;color CorVariable;CorVariable=GREEN;coutCorVariableendl;coutsizeof(GREEN)endl;/CorVariable=1;/这样不行CorVariable=(color)1;24枚举常量的自定义赋值:说明可在枚举常量名之后使用等号将一个(c
20、har或int类型的)常量置给一个枚举常量名,以改变编译的缺省赋值,例如: enum color RED=-1,BLUE,GREEN=6,YELLOW;这里RED代表值1,而BLUE为它前 面的枚举常量的值加1,所以BLUE代表值0。同样,GREEN的值为6,而YELLOW的值为7。为枚举常量指定重复的值也是合法的,例如: enum state FALSE,TRUE,FAIL=0,BAD=0;在这个说明中FALSE,FAIL和BAD的值都为0。枚举常量的类型:枚举常量的类型隐含是unsigned char 或int类型 ,但到底是何种类型取决于枚举常量的值。如果所有的值都可用unsigned
21、char表示,则它就是每个枚举常量的类型。例7.7 口袋中有红、黄、蓝、白、黑5种颜色的球若干个。每次从口袋中任意取出3个球,问得到3种不同颜色球的可能取法,输出每种排列的情况。#include#includeusing namespace std;enum colorred,yellow,blue,white,black;void main()void print(color);color pri;int i,j,k,n=0;for(i=red;i=black;i+)for(j=red;j=black;j+)if(i!=j)for(k=red;k=black;k+)if(k!=i)&(k!=
22、j)coutsetw(3)+n;pri=color(i);print(pri);pri=color(j);print(pri);pri=color(k);print(pri);coutendl;couttotal:nendl;void print(color co)coutsetw(8);switch(co)case red:coutred;break;case yellow:coutyellow;break;case blue:coutblue;break;case white:coutwhite;break;case black:coutblack;break;default:break;
23、 38 white red blue 39 white red black 40 white yellow red 41 white yellow blue 42 white yellow black 43 white blue red 44 white blue yellow 45 white blue black 46 white black red 47 white black yellow 48 white black blue 49 black red yellow 50 black red blue 51 black red white 52 black yellow red 53
24、 black yellow blue 54 black yellow white 55 black blue red 56 black blue yellow 57 black blue white 58 black white red 59 black white yellow 60 black white bluetotal:607.4 用typedef声明类型用typedef声明一个新的类型名来代替已有的类型名。typedef int INTEGER;typedef float REAL;则,以下两行等价:int i,j;float a,b;INTEGER i,j;REAL a,b;typedef int NUM100;NUM n;/定义n为包含100整型元素数组。typedef char *STRING;/STRING为字符指针类型。STRING p,s10;/p为字符指针变量,s为字符指针数组.typedef int (*POINTER)(); /POINTER为指
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- CBWQA/T 0002-2013螺旋空气分离器
- 足部按摩与调节血压考核试卷
- 资产转让补充协议
- 物流企业智能分拣中心租赁与运营支持协议
- 网络配偶忠诚协议及社交账号监管执行合同
- 2025年中国办公室储物柜行业市场前景预测及投资价值评估分析报告
- 电池梯次利用与环保产业链协同发展合作协议
- 资产评估机构与金融机构股权合作投资合同
- 证券公司经纪人培训与派遣一体化服务协议
- 2025年中国岸边集装箱起重机行业市场前景预测及投资价值评估分析报告
- 反射疗法师理论考试复习题及答案
- 2023版中职教材-心理健康与职业生涯-第11课-主动学习-高效学习-课件
- 2024年重庆市高考思想政治试卷真题(含答案解析)
- 2024春期国开电大本科《外国文学》在线形考(形考任务一至四)试题及答案
- 阳光雨棚制作安装合同范本
- 福建小凤鲜禽业有限公司100万羽蛋鸡养殖基地项目环境影响报告书
- CJT 489-2016 塑料化粪池 标准
- 2024中考语文语言运用考点备考试题精练 (含答案)
- 苗木供应质量保证措施方案
- 2022-2023学年广东省广州市番禺区教科版(广州)四年级下册期末测试英语题卷(无答案)
- 【蔚来新能源汽车营销策略探究9200字(论文)】
评论
0/150
提交评论