c++课件第七章自定义数据类型.doc_第1页
c++课件第七章自定义数据类型.doc_第2页
c++课件第七章自定义数据类型.doc_第3页
c++课件第七章自定义数据类型.doc_第4页
c++课件第七章自定义数据类型.doc_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第七章 自定义数据类型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) 声明类型的同时定义变量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;float score;char addr30;2. 结构体变量的初始化struct Studentint num;char name20;char sex;int age;float score;char addr30;stdent=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.num=10010; .是成员运算符,它的优先级最高。(3) 对于结构体嵌套,要逐级引用。student1.birthday.month=11;(4) 不能将一个结构体变量作为一个整体进行输入和输出。(5) 对于结构体变量的成员可以像普通变量一样进行各种运算。(6) 可以引用结构体变量成员的地址,也可以引用结构体变量的地址。cout&student1;cout&student1.age;例7.1 引用结构体变量中的成员#include using namespace std;struct Dateint month;int 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.birthday.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 对候选人得票统计程序.#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;ZhangLiFunLiZhangLiZhangLiFunWangLi:4Zhang:3Fun:27.1.5 指向结构体变量的指针1. 通过指向结构体变量的指针引用结构体变量中的成员例7.3 指向结构体变量的指针的应用#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.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;coutnumendl;coutnum+endl;coutnumendl;coutstu.numendl;103011030110303103032. 用结构体变量和指向结构体变量的指针构成链表struct Studentint num;float score;Student *next;利用指向自己的指针构成链表。#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.score=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 结构体类型数据作为函数参数(1) 用结构体变量作参数(传值)。#include #include using namespace std;struct Studentint num;char name20;float score3;void main()void print(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 Studentint 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 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和deleteint *p=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;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: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) 共用体变量的地址和它的各个成员的地址都是同一地址。(3) 不能对共用体变量赋值;不能企图引用变量名来得到一个值;不能在定义共用体变量时对它进行初始化;不能用共用体变量作为函数参数。例7.7 name num sex job grade/positionLi 1011 f s 3Wang 2085 m t prof#include #include #includeusing namespace std;struct int num;char name10;char sex;char job;union Pint grade;char position10;category;person2;void main()int i;for(i=0;personi.sexpersoni.job;if(personi.job=s) cinpersoni.category.grade;else cinpersoni.category.position;coutNo. Name sex job grade/positionendl;for(i=0;i2;i+)coutpersoni.numsetw(6) personi.sex personi.job;if(personi.job=s) coutsetw(10)personi.category.grade;else coutsetw(10)personi.category.position;coutendl;101 Li f s 3102 Wang m t profNo. Name sex job grade/position101 Li f s 3102 Wang m t prof7.3 枚举类型如果一个变量只有几中可能的值,可以定义为枚举类型 enum colorRED,BLUE,GREEN;关键字enum标志枚举类型定义开始,分号标志其结束。在C+中允许不写enum;enum后的标识符color称为枚举类型名。枚举元素(枚举常量):花括号内用逗号隔开的标识符是为这个类型定义的常量,枚举元素的缺省赋值:编译器为这些枚举常量赋予不同的值,第一个常量RED值为0,以后的常量的值是它前面常量的值增1,所以,BLUE为1,GREEN为2 。声明枚举变量:枚举标记可以用类型名说明具有该类型的变量,例如: color CorVariable;上面的语句说明了变量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枚举常量的自定义赋值:说明可在枚举常量名之后使用等号将一个(char或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 char表示,则它就是每个枚举常量的类型。例7.8 口袋中有红、黄、蓝、白、黑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!=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; 38 white red blue 39 white red black 40 white yellow red 41 white yellow blue 42

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论