结构体(VC++第7章wu)

上传人:猪** 文档编号:242983214 上传时间:2024-09-13 格式:PPT 页数:34 大小:203KB
返回 下载 相关 举报
结构体(VC++第7章wu)_第1页
第1页 / 共34页
结构体(VC++第7章wu)_第2页
第2页 / 共34页
结构体(VC++第7章wu)_第3页
第3页 / 共34页
点击查看更多>>
资源描述
Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,*,*,Page,*,结构体、共用体和枚举类型,参考书,VC+,程序设计基础第七章,1,结构体, 概念,概念,结构体,:一组由不同类型数据项组成的,构造类型,数据,如:,num name sex age score,98231009,Li Ming,M,18,88.5,可由如下方法定义以上结构体类型:,Struct,student,char num9;,char name10;,char sex3;,int,age;,float score;,;,成员列表,结构体,类型,名,成员名,成员类型,结构体名,;,不能忘,Student,2,初始化时,在花括号中列出的值的类型及顺序必须与该结构体类型定义中说明的结构体成员一一对应。,结构体, 定义(一),1.先定义结构体类型,再定义变量,上述页中已定义结构体类型,struct,student,定义该类型的变量,student1,student2,student,student1,student2;,类型名,对变量,student1, student2,初始化如下:,student,student1,=“98231009”,“,li ming,”,“,男”,20,87.0;,student student2,=“98231010”,“,wang,ping”,“,女”,19,92.0;,例:,student,student1,=98231009, “,li ming,”,“,男”,20,87.0;,student student2,=“982310107”, “,wang,ping”,“,女”,19,92.0;,3,结构体, 定义(二),定义结构体类型的同时定义变量,struct,student, char num8;,char name10;,char sex;,int,age;,float score;, student1,student2;,此时结构体名可省略,但省略后,以后就不能再定义该类型的变量,4,结构体, 定义(三),struct,student, char num8;,char name10;,char sex;,struct,date birthday ;,float score;, student1,student2;,结构体成员也可为结构体,如:,struct,date,int,year;,int,month;,;,student,num,name,sex,birthday,score,year,month,5,结构体变量的引用和存放,结构体成员引用,结构体变量名.成员名,各成员与其同类型的普通变量用法相同,struct,date, char year5;,char month3;,;,struct,student, char num9;,char name10;,struct,date birthday;,char sex3;,stu,;,结构体的各成员在内存为依次连续存放,3字节,stu,stu,.num,stu,.name,stu,. sex,9字节,10字节,stu,.birthday.year,stu,.birthday.month,5字节,3字节,6,结构体成员举例,#,include ,struct,date, char year5;,char month3;,;,struct,student, char num9;,char name10;,struct,date birthday;,char sex3;,;,void main(),student student1=20001021,huangjiao,1980,12,男;,cout,student1.birthday.yearn;,cout,student1.sexstu2.birthday.year;,注意输入跳空问题,9,结构体变量可以作为函数的参数,当形参与实参为结构体变量时,其结合方式为值调用方式,即单向传送。,函数返回值类型也可为结构体。,例:,10,#,include ,struct,s,int,m;,float x;,;,void swap(s s1, s s2), s t;,t=s1; s1=s2; s2=t;,s fun(s s1, s s2), s t;,t.m=s1.m+ s2.m;,t.x=s1.x+ s2.x;,return t;,void main(),s r1=100,250.5, r2=200,350.5;,swap(r1, r2);,cout,“r1.m=“r1.mt “r1.x=“r1.x n;,cout,“r2.m=“r2.mt “r2.x=“r2.x n;,s sum;,sum=fun(r1, r2);,cout,“sum.m=“sum.mt “sum.x=“sum.x n;,r1.m=100 r1.x=250.5,r2.m=200 r2.x=350.5,sum.m=300 sum.x=601,11,结构体数组,定义,struct,student, char num10;,float score;,stu,3;,student stud3;,初始化,struct,student stud3=,“1001”,76,”1002”,83.5,”1003”,64;,stud,stud,1001,1002,83.5,1003,64,76,stud2,stud0,stud1,12,#,include ,struct,s,int,id;,int,eng,math;,;,void main(),s r3=15,60,70,10,100,100;,s,rr,2=1,60,30,4,80,90;,for(,int,i=0;i3;i+),cout,ri.idtri.engtri.mathn;,for(i=0;i2;i+),cout,rr,i.idt,rr,i.engt,rr,i.mathn;,60 70,10 100 100,0 0 0,1 60 30,4 80 90,13,结构体类型的静态成员,在,定义结构体的成员时,不能指定成员的存储类型为,auto、register、extern,但可指定成员的存储类型为,static,.,若结构体类型中某个成员定义为,static,存储类型,表示对于这种结构体类型的所有变量,系统为该成员只分配一个存储空间。,例:,14,#,include ,struct,s,static,int,id;,int,eng;,;,int,s:id=50;,s s1;,void main(),s s2,s3;,cout,“s1.id=“s1.idn;,s2.id=200;,cout,“s2.id=“s2.idn;,cout,“s1.id=“s1.idn;,s3.id=400;,cout,“s3.id=“s3.idn;,cout,“s2.id=“s2.idn;,cout,“s1.id=“s1.idn;,S1.id = 50,S2.id = 200,S1.id = 200,S3.id = 400,S2.id = 400,S1.id = 400,/对静态成员进行定义性说明,并初始化。,必须在文件作用域的某个地方,对静态,成员进行定义性说明,且仅能说明一次。,15,共用体(联合体),定义形式:,union,共用体名,数据类型 成员名 1;,数据类型 成员名 2;, :,数据类型 成员名,n;,;,引用形式:,共用体名成员名,共用体变量各成员合用同一起始地址开始的内存,共用体变量所占内存为占内存最多的成员所占用内存,共用体变量不可初始化,在任一时刻,在一个共用体变量内,只有一个成员起作用,16,例:,union,uarea, char c_data;,short,int,s_data;,long l_data;,uarea,c_data,l_data,s_data,17,枚举类型,问题的提出:,当处理一些非数值数据时,用自然语言中有相应含义的单词代表某一种状态。,枚举类型及枚举类型变量的定义,enum,weekdays sun,mon,tue,wed,thu,fri,sat; weekdays day1,day2;,枚举常量/枚举元素,枚举类型变量,18,枚举元素,enum,weekdays sun,mon,tue,wed,thu,fri,sat;,枚举元素不可以被赋值,枚举元素作为常量,有默认值。,可以改变枚举元素的值。(在定义枚举类型时另外指定),例:,enum boolean,true=1,false=0;,enum,colors red=5,blue=1,green,black,white,yellow;,19,枚举类型变量的使用,enum,weekdays sun,mon,tue,wed,thu,fri,satday1,day2;,赋值,运算:,day1=,tue,;,day1=(,enum,weekdays) 2,day2= day1;,比较运算:,(比较枚举元素的序号),if( day1 day1;,错,枚举类型变量可以直接输出:,day1=wed;,cout,day1;,20,举例,#,include ,enum,week,monthday,tuesday,wedensday,thirsday,friday,saturday,sunday,;,void main(),week weekday;,weekday=,saturday,;,cout,weekdayn;,int,i=weekday;,switch(i),case(0):,cout,monthday,;,case(1):,cout,tuesday,;,case(2):,cout,wedensday,;,case(3):,cout,thirsday,;,case(4):,cout,friday,;,case(5):,cout,saturday,;,case(6):,cout,num p-score,两种表示方法等价,p,stu,1001,76,分析下面运算:,p,-,score+,+p,-,score,22,指向结构体数组的指针,定义,struct,student, char num10;,int,score;, stud3 =“1001”,76,”1002”,83.5,”1003”,64;,struct,student *p;,p=stud;,p,stud,1001,1002,83.5,1003,64,76,p+1,p+2,23,#,include ,struct,w,int,a;,int,*b;,*p;,void main(),int,x0=12,11,x1=32,31;,w x2=100,x0,300,x1;,p=x;,cout,bn;,cout,a+n;,cout,an;,cout,an;,cout,jjj,n;,cout,an;,cout,adata,指针域:,p-next,特点:,插入、删除元素时不必大量移动数据,不能随机存取其中记录,要从头指针开始。,动态分配内存的函数,malloc,(size),分配内存长度为,size,的连续空间,,成功,则返回该空间的,起始地址,,不成功,返回0(,NULL)。,如:,struct,node *p;,p=(,stuct,node),malloc,(,sizeof,(,struct,node);,calloc,(n,size),分配,n,个内存长度为,size,的,连续,空间,,成功,则返回该空间的,起始地址,,不成功,返回0(,NULL)。,Free(,ptr,),释放指针,ptr,所指的内存空间。,建立链表(一),Typedef struct,node *,linklist,1.,建空表,linklist,head,last,p;,head=NULL;,2.,加,n,个结点(逐个加在链表尾),head=(,LinkList,),malloc,(,sizeof,(,LNode,),last=head;,last-next=NULL;,for(i=n; i0; -i), p=(,LinkList,),malloc,(,sizeof,(,LNnde,);,scanf,(“%d”,指向链表头,指向最后一结点,指向新结点,NULL,p,head,p,NULL,NULL,按大小次序插入一个结点,stud,Linklist,p0,p1,p2;,指向新结点,指向当前结点,指向当前结点的前结点,1.,链表为空时:,head=p0;p0-next=NULL;,2.,插在链表的第一个结点前,(,p1,指向第一个结点:,head=p0;p0-next=p1;,NULL,head,p0,NULL,head,p0,p1,按大小次序插入一个结点,stud,3.,插入在链表中,p1,结点前,p2,结点后,:,p2-next=p0;p0-next=p1;,4.,插在链表的尾部,p1-next=p0;p0-next=NULL;,NULL,head,p0,p1,p2,head,p0,NULL,NULL,删除一个值为,num,的结点,1.,找到要删除的结点,p1,其前一,结点为,p2:,p1=head;,while(p1-data!=num & p1-next!=NULL), p2=p1;p1=p1-next;,2.若,p1,为第一个结点,head=p1-next;,head,p1,p2,NULL,head,p1,NULL,3.若,p1,不为第一个结点,p2-next=p1-next;,head,合并两个有序单链表,LinkList MergeList,(,LinkList,la,LinkList,lb),ap,=la-next;,bp,=lb-next;,lc,=,tp,=la;,while (,ap,&,bp,), if (,ap,-datadata),tp,-next=,ap,;,tp,=,ap,;,ap,=,ap,-next;,else ,tp,-next=,bp,;,tp,=,bp,;,bp,=,bp,-next;,tp,-next=,ap,?,ap,:,bp,;,free(lb);,return(,lc,);,链表类型,双向链表,环型链表,十字链表,head,NULL,NULL,head,
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 小学资料


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!