![c一7自定义数据类型_第1页](http://file4.renrendoc.com/view/613b4ce562397a5bf230e865519d9d22/613b4ce562397a5bf230e865519d9d221.gif)
![c一7自定义数据类型_第2页](http://file4.renrendoc.com/view/613b4ce562397a5bf230e865519d9d22/613b4ce562397a5bf230e865519d9d222.gif)
![c一7自定义数据类型_第3页](http://file4.renrendoc.com/view/613b4ce562397a5bf230e865519d9d22/613b4ce562397a5bf230e865519d9d223.gif)
![c一7自定义数据类型_第4页](http://file4.renrendoc.com/view/613b4ce562397a5bf230e865519d9d22/613b4ce562397a5bf230e865519d9d224.gif)
![c一7自定义数据类型_第5页](http://file4.renrendoc.com/view/613b4ce562397a5bf230e865519d9d22/613b4ce562397a5bf230e865519d9d225.gif)
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第7章自定义数据类型2课时面向对象程序设计1本章内容7.1结构体类型7.2枚举类型27.1结构体类型3structStudent{intnum;stringname;charsex;intage;floatscore;stringaddress;};与数组不同,结构体中各元素可以是不同类型
.类型定义Studentstudent1={10001,"ZhangXin",'M',19,90.5,"Shanghai"};变量定义和初始化结构体变量的另外两种定义形式4structStudent{intnum;stringname;charsex;intage;floatscore;stringaddress;}student1,student2;struct{intnum;stringname;charsex;intage;floatscore;stringaddress;}student1,student2;形式二形式三成员也可以是一个结构体5structDate//声明一个结构体类型Date{intmonth;intday;intyear;};structStudent//声明一个结构体类型Student{intnum;charname[20];charsex;intage;Datebirthday;//Date是结构体类型,birthday是Date类型的成员charaddr[30];}student1,student2;//定义student1和student2为结构体类型Student的变量引用结构体变量6Students1,s2,s3;s1=s2;s1.num=10010;//可单独赋值s3.score=s1.score+s2.score;cout<<s1.num;cout<<&s1;//s1的首地址cout<<&s1.age;正确Students1,s2,s3;//不能作为整体赋值s1={10001,"ZhangXin",'M',19,90.5,"Shanghai"};//不能作为整体运算、输入输出s1=s2+s3;cout<<s1;错误例7.1引用结构体变量成员7#include<iostream>#include<iomanip>#include<string>usingnamespacestd;structStudent{intID;stringname;stringsex;floatscore;};intmain(){Studentstudent1,student2={10002,“张三","女",89.5};student1=student2;cout<<"学号姓名性别成绩"<<endl;cout<<setw(6)<<student1.ID;cout<<setw(8)<<;cout<<setw(6)<<student1.sex;cout<<setw(6)<<student1.score<<endl;return0;}定义结构体数组8Studentstu[3];//定义Student类型的数组stustructStudent{…}stu[3];struct{…}stu[3];形式一形式二形式三结构体数组初始化9Studentstu[]={{10101,"LiLin",'M',18,87.5,"103BeijingRoad"},{10102,"ZhangFun",'M',19,99,"130ShanghaiRoad"},{10104,"WangMin",'F',20,78.5,"1010ZhongshanRoad"}};数组各元素在内存中连续存放.例7.2统计候选人得票10#include<iostream>#include<string>usingnamespacestd;structPerson{stringname;intcount;};intmain(){Personleader[3]={"Li",0,"Liu",0,"Wang",0};inti,j;
cout<<"候选人分别是Li,Liu和Wang,共有张选票"<<endl<<endl;stringleader_name;for(i=0;i<10;i++)
{cout<<"请输入第"<<i+1<<"张选票上候选人的姓:";cin>>leader_name;for(j=0;j<3;j++)
{if(leader_name==leader[j].name)leader[j].count++;
}
}cout<<endl;for(i=0;i<3;i++)
{cout<<leader[i].name<<":"<<leader[i].count<<endl;
}return0;}指向结构体变量的指针11#include<iostream>#include<string>usingnamespacestd;structStudent//声明结构体类型student{intnum;stringname;charsex;floatscore;};intmain(){Studentstu={10301,"WangFang",'f',89.5};Student*p=&stu;//定义p为指向Student类型数据的指针变量并指向stu
cout<<stu.num<<""<<<<""<<stu.sex<<""<<stu.score<<endl;cout<<(*p).num<<""<<(*p).name<<""<<(*p).sex<<""<<(*p).score<<endl;return0;}结构体变量运算符12结构体变量.成员名
//如stu.num(*p).成员名
//如(*p).nump->成员名
//“->”为指向运算符三种形式等价链表链表是一种数据结构“头指针”变量head存放第一个元素的地址每个中间结点包括两个部分:用户数据,下一个结点地址链表中各元素在内存中的存储单元可以是不连续的13链表的实现14structStudent{intnum;floatscore;Student*next;//next指向Student结构体变量};链表必须利用结构体变量和指针才能实现.例7.4静态链表15structStudent{intID;stringname;stringsex;floatscore;Student*pNext;};intmain(){Students1={10002,"张三","女",89.5,NULL};Students2={10102,"李四","男",99,NULL};Students3={10104,"王五","男",78.5,NULL};Student*pHead,*p;pHead=&s1;s1.pNext=&s2;s2.pNext=&s3;
cout<<"学号姓名性别成绩"<<endl;for(p=pHead;NULL!=p;p=p->pNext){cout<<setw(6)<<p->ID;cout<<setw(8)<<p->name;cout<<setw(6)<<p->sex;cout<<setw(6)<<p->score<<endl;}return0;}结构体作为函数参数161.用结构体变量名作参数//值传递2.用指向结构体变量的指针作参数3.用结构体变量的引用作参数三种方法例7.5a用结构体变量名作参数17#include<iostream>#include<string>usingnamespacestd;structStudent{intnum;stringname;floatscore[3];};voidprint(Studentstu){cout<<stu.num<<""<<<<""<<stu.score[0]<<""<<stu.score[1]<<""<<stu.score[2]<<endl;}intmain(){Studentstu;stu.num=12345;="LiFung";stu.score[0]=67.5;stu.score[1]=89;stu.score[2]=78.5;print(stu);return0;}值传递,形参单独开辟存储单元.例7.5b用指针作参数18#include<iostream>#include<string>usingnamespacestd;structStudent{intnum;stringname;floatscore[3];};voidprint(Student*p){cout<<p->num<<""<<p->name<<""<<p->score[0]<<""<<p->score[1]<<""<<p->score[2]<<endl;}intmain(){Studentstu;stu.num=12345;="LiFung";stu.score[0]=67.5;stu.score[1]=89;stu.score[2]=78.5;print(&stu);return0;}形参是结构体起始地址,占4字节.例7.5c用引用作参数19#include<iostream>#include<string>usingnamespacestd;structStudent{intnum;stringname;floatscore[3];};voidprint(Student&stu){cout<<stu.num<<""<<stu.name<<""<<stu.score[0]<<""<<stu.score[1]<<""<<stu.score[2]<<endl;}intmain(){Studentstu;stu.num=12345;="LiFung";stu.score[0]=67.5;stu.score[1]=89;stu.score[2]=78.5;print(stu);return0;}传递的是实参的地址,形参和实参是同一对象.new和delete运算符20newint;//开辟一个存放整数的存储空间,返回指向该存储空间的地址(即指针)newint(100);//开辟一个存放整数的空间,并指定该整数的初值为100,返回指向该存储空间的地址newchar[10];//开辟一个存放字符数组(包括10个元素)的空间,返回首元素的地址newint[5][4];//开辟一个存放二维整型数组(大小为5*4)的空间,返回首元素的地址float*p=newfloat(3.14159);//开辟一个存放单精度数的空间,并指定该实数的初值为3.14159,将返回的该空间的地址赋给指针变量pnew类型[初值]
//如果内存不足,new返回NULL//用new分配数组空间时不能指定初值delete[]指针变量//在指针变量前加方括号,表示是对数组空间的操作new和delete运算符new运算符例子例7.6动态分配存放结构体的空间21#include<iostream>#include<string>usingnamespacestd;structStudent{stringname;intnum;charsex;};intmain(){Student*p=NULL;p=newStudent;if(NULL!=p){p->name="WangFun";p->num=10123;p->sex='m';cout<<p->name<<endl<<p->num<<endl<<p->sex<<endl;
deletep;p=NULL;}return0;}例7.6B动态链表结构(1)22#include<iostream>usingnamespacestd;structStudent{intnum;floatscore;Student*next;};Student*CreatLink();voidListLink(Student*);voidDeleteLink(Student*);intmain(){Student*phead=CreatLink();ListLink(phead);DeleteLink(phead);return0;}Student*CreatLink()/*返回指向链表头指针*/{Student*head=NULL,*p,*tail; while(true){p=newStudent;cout<<"输入学号,以0结束:";cin>>p->num;if(!p->num)break;cout<<"输入分数:";cin>>p->score;p->next=NULL;if(head==NULL){head=p;tail=p;}else{tail->next=p;tail=p;/*指向尾结点*/}}returnhead;}例7.6B动态链表结构(2)23voidListLink(Student*phead){Student*p;cout<<endl<<"学号分数"<<endl;for(p=phead;p!=NULL;p=p->next){cout<<p->num<<""<<p->score<<endl;}}voidDelete
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 大雪节气科普
- 词汇量的力量
- 进入团圆的申请书
- 全国导游基础知识-2022全国导游科目五现场面试真题及答案
- 初级公司信贷-初级银行从业资格考试《公司信贷》点睛提分卷1
- 2025年捆钞机项目效益评估报告
- 企业内部API文档编写指南
- 土地证申请书范文
- 医学影像三基三严试题
- DB2113-T 0011-2024 地理标志产品 三十家子鳞棒葱
- 2025年湘西民族职业技术学院高职单招职业技能测试近5年常考版参考题库含答案解析
- 北京市西城区2024-2025学年高三上学期期末考试语文试题(解析版)
- 《新能源汽车技术》课件-第二章 动力电池
- 拘留所被拘留人员管理教育
- 2025年湖南交通职业技术学院高职单招职业技能测试近5年常考版参考题库含答案解析
- 河南省天一大联考2024-2025学年高三上学期1月期末地理含答案
- 北京市朝阳区2025下半年事业单位招聘149人历年高频重点提升(共500题)附带答案详解
- 2024-2025学年成都市高一上英语期末考试题(含答案和音频)
- 三坐标考试试题和答案
- 数字金融 远程音视频手机银行技术规范
- 《中药调剂技术》课件- 处方调配
评论
0/150
提交评论