结构体、共用体和枚举类型.ppt_第1页
结构体、共用体和枚举类型.ppt_第2页
结构体、共用体和枚举类型.ppt_第3页
结构体、共用体和枚举类型.ppt_第4页
结构体、共用体和枚举类型.ppt_第5页
已阅读5页,还剩66页未读 继续免费阅读

下载本文档

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

文档简介

南京航空航天大学信息学院计算机基础教研室 制作(版权所有) 第10章 结构体、共用体和枚举类型 第10章 本章要点 v了解结构 v了解关于结构的各种操作 v理解怎样使用结构操作数据 v理解结构和函数之间的关系、掌握结构作 为参数传递与返回结构的函数方法 v理解结构中数组的用法 v理解怎样创建结构数组 v了解共用体的特点 v了解枚举类型 第10章 问题的提出 学号 姓名 年龄 性别 成绩1 成绩2 平均成绩 1 aa 19 m 80 90 90 2 bb 18 f 78 70 73 3 cc 17 m 81 75 79 4 dd 18 f 80 60 75 5 ee 19 m 76 83 83 每行的数据类型不相同,如何表示此二维数据? 如何交换两行值? 能否将一行看成一个整体? 定义一种类型,把不同的数据作为一个整体来处理结构体 第10章 数据类型 数组 - 有相同类型的数据集合 结构体- 不同类型的数据集合 构造数据类型: 由简单数据类型(int、float、 char)组合而 成的(有机整体)数据类型。 基本类型 : 整型 、实型 、字符型. 第10章 主要内容 v结构体类型的定义 v结构体类型的变量和结构体类型数组 v共用体 v枚举类型 第10章 结构体类型的定义 定义了一种新的数据类型 struct 结构体名或结构体名 struct 结构体名 类型标识符 成员名1 类型标识符 成员名2 . 类型标识符 成员名n ; 可为简单类型 也可为构造类型 关键字 不能少 成员 列表 第10章 例 v以学生档案为例,假设包括如下数据项 学号(num):整型 姓名(name):字符串 性别(sex):字符型 出生日期(birthday):date结构体 四门课成绩(sc):一维实型数组 第10章 例 依此格式定义上例 struct date int year ,month ,day; struct student int num; char name12; char sex; date birthday; float sc4; ; 如struct date 没事先说明可 写成: struct student int num ; char name12; char sex; struct int year; int month; int day; birthday; float sc4; ; 第10章 例 例1:一组相关变量 定义结构体类型 新数据类型 结构体成员 int month ; int day ; int year ; struct date int year ; int month ; int day ; ; char name30; struct wage float salary ; char name30; float salary ; ;不同数据类型的成员 例2:一组相关变量 定义结构体类型 第10章 说明 (1) 结构体类型是一种构造数据类型,它与int, char,float等系统定义的基本数据类型具有同等地位 ,是由用户自行定义的。 (2) 结构体类型不分配任何存储空间。 相应结构体类型的变量、数组及动态开辟的 存储单元占存储空间。 构造的数据类型可以用来定义变量 第10章 结构体类型的变量和数组的定义方法 先定义结构体类型,再单独进行定义 在定义类型的同时定义结构体变量 在定义一个无名结构体类型的同时,直接进行定义 第10章 方法一 先声明结构体类型再定义结构体变量先声明结构体类型再定义结构体变量 struct 结构体名 成员列表; struct 结构体名 变量名列表; 或 结构体名 变量名列表 第10章 例 struct student int num; char name12; char sex; date birthday ; float sc4; ; struct student std,pers3; 或 student std,pers3; std:结构体变量 pers:结构体数组, (三个元素均为结构体类型变量) 第10章 变量std的结构 num name sex year month day sc0 sc1 sc2 sc3 birthday 注意:各成员按顺序排列 第10章 方法二 在声明类型的同时定义结构体变量在声明类型的同时定义结构体变量 struct student int num; char name12; char sex; struct date birthday ; float sc4; std,pers3; struct 结构体名 成员列表 变量名列表; 第10章 struct int num; char name12; char sex; date birthday ; float sc4; std,pers3; 方法三 直接定义结构体变量直接定义结构体变量 struct 成员列表 变量名列表; 无结构体名 第10章 结构体类型的几点说明 结构体中的成员可以象变量一样使用。 成员名可以与程序中的变量同名 结构体中的成员也可为结构体变量 类型与变量是完全不同的概念。 先定义结构类型,再定义变量为该类型 。 类型:不可赋值、存贮、运算;系统不分配空间。 变量:可赋值、存贮、运算;系统要分配空间。 第10章 结构体类型变量初值 struct student int num; char name12; char sex; date birthday ; float sc4; std=10101,“liming“,m,1962,5,10,88,76,8 5.5,90; 注意: 一一对应赋初值, 不允许跳跃赋值。 可只给前面的成员赋值 第10章 给结构体数组赋初值 v数组中的每个元素是一个结构体类型的数据 ,因此将此成员的值依次放在一对花括号中 例: struct bookcard char num5; float money; bk3=“no.1“,35.5,“no.2“,25.0, “no.3“,66.7; 第10章 给二维结构体数组赋初值例 struct char ch; int i; float x; arr23=a,1,3e10,a,2,4e10, a,3,5e10,b,1,6e5,b,2,7e5, b,3, 8e5; 第10章 结构体类型变量及其成员的引用 v两种方式 f引用成员 m结构体变量名.成员名 f引用整体 struct t int i, j; char name10; ; t t1=12, 48, “lili“, t2; t2=t1; 其中“t2=t1;”等同于: t2.i=t1.i; t2.j=t1.j; strcpy(, ); 结构体变量的成员可以像普 通变量一样进行各种运算, 如stud1.num+、 stud1.sex=stud2.sex; 结构体变量不能 进行整体输入输出 第10章 优先级: 1 结合性: 左 例:有定义: struct date 对变量today作成员选择运算: int year ; today. year int型 int month ; today. month int型 int day; today. day int型 today ; 成员选择运算的结果是得到了该结构变量的某个成 员,其数据类型是定义该结构体类型时成员列表中定 义的类型。 成员运算符“.” 第10章 例 struct data int month; int day; int year; ; struct student char name20; char sex; data birthday; int sc4; std,arr5; (1)引用sex std.sex(通过结构体变量引用 ) 引用arr0中的sex: arr0.sex 不能写作 arr.sex 第10章 例 struct data int month; int day; int year; ; struct student char name20; char sex; data birthday; int sc4; std,arr5 ; (2)引用sc中的元素,如sc1 std.sc1(通过结构体变量引用) 不能写成std.sc c+语言不允许对数组 整体访问(字符串除外 ) 第10章 例 struct data int month; int day; int year; ; struct student char name20; char sex; data birthday; int sc4; std,arr5 ; (3)成员为字符串 如name (通过结构体变量引用 ) 第10章 例 struct data int month; int day; int year; ; struct student char name20; char sex; data birthday; int sc4; std,arr5 ; (4)内嵌结构体成员的引用 逐层使用成员名定位 引用std中的出生年份 std.birthday.year 只可引用最 低一级成员 第10章 例是字符串,可以对它进行对任何 字符串允许的操作,包括输入输出。 对结构体变量中的每个成员,可对它进行同类 变量所允许的任何操作。 对结构体变量中的成员进行操作 第10章 例 struct data int month; int day; int year; ; struct student char name20; char sex; data birthday; int sc4; std,pers5 ; (1)对name的操作 ; 或cin.getline(,80); (2)对sex的操作 cinstd.sex; for(i=0;ipersi.sex; std.sex= m; 注意: 不能写成 =“li ming“ 第10章 struct data int month; int day; int year; ; struct student char name20; char sex; data birthday; int sc4; std,pers5; 例 (3)对birthday中year的操作 cinstd.birthday.year; std.birthday.year=1962; 第10章 结构体数组 struct student int num; char name20; char sex; int age; float score; char addr30; stud4 = 23901, “zang li“, f, 19, 78.5, “35 shanghai road“, 23902, “wang fang“, f, 19, 92, “101 taiping road“, 23905, “zhao qiang“, m, 20, 87, “56 ninghai road“, 23908, “li hai“, m, 19, 95, “48 jiankang road“; 第10章 内存存储情况 stud0 stud1 . . . 23901 “zang li“ f 19 78.5 “35 shanghai road“ 23902 “wang fang“ f 19 92 “101 taiping road“ . . . 第10章 结构体数组的应用 问题:建立一个学生档案的结构体数组,输入并输出学生的信息 。 #include /li0802_2.cpp 输入输出学生信息 #include struct student char num16; char name20; float score; ; student input(student stud) stud.score; return stud; 第10章 结构体数组的应用 void output(student stud) cout/输入输出学生信息 #include struct student char num16; char name20; float score; ; void input(student 第10章 引用调用 void output(student for(int i=0; iname; (*p).id 等效于p-id; (*p).salary 等效于p-salary. 第10章 在使用指针访问成员时,通常使用箭头操作符“-”。 例如:下面程序中,定义了结构指针,通过结构指针访问结构 成员。 #include #include strut person char name20; unsigned long id; float salary; ; 第10章 void main( ) person pr1; person *p; p= strcpy(p-name, “wang ming”); p-id=12345678; p-salary=4800.0; coutnamesalary struct person char name20; unsigned long id; float salary; ; 第10章 person allone6=“jone”,12345,3390.0, “david”,13916,4490.5, “marit”,27519,3110.0, “jasen”,42876,6230.5, “peter”,23987,4000.2, “yoke”,12335,5110.0; void main( ) person temp; for(int i=1; iallonej+1.salary) /工资高的后移 temp=allonej; allonej=allonej+1; alonej+1=temp; 第10章 for(int k=0;k struct person char name20; unsiged long id; float saslary; ; 10.4 传递结构参数 第10章 void print(person pr) cout struct person char name20; unsigned long id; float salary; ; 10.5 结构函数 第10章 person getperson( ) /定义返回结构变量的函数 person temp; ; couttemp.idtemp.salary; return temp; /返回结构变量temp void print(person unsigned long id; float salary; ; void getperson(person coutp.idp.salary; 第10章 void print(person float score; student *next; ; 第10章 student *head; /链首指针 student *greate( ) /创建链表函数 student *ps; /创建新结点指针 student *pend; /移动的链尾指针,用于在其后插入结点 ps=new student; /动态申请新建结点的存储空间 cinps-numberps-score; /结点赋值 head=null; /开始时链表为空 pend=ps; while(ps-number!=0) if(head=null) head=ps; else pend-next=ps; 第10章 pend=ps; /s点 ps=new student; cinps-numberps-score; pend-next=null; delete ps; return(head); void showlist(student *head) coutnumberscorenext; 第10章 void main( ) showlist(greate( ); 输入: 结果: 401 85.0 the items of list are: 410 73.0 401, 85.0 425 91.0 410, 73.0 413 68.0 425, 91.0 432 82.0 413, 68.0 0 0 432, 82.0 在main( )中,把建立链表函数greate( )的返回值head(指向链 表指针)作为实参数调用showlist( )函数。 第10章 greate( )函数中,链表创建过程如下图所示: 进入循环之前: head null 第一次进入循环到达s点: pend ps head 第一次循环结束后: head pend ps 40185.040185.071073.0 第10章 第二次循环到达s点时: head pend ps 第二次循环结束时: head pend ps 程序结束时: head 40185.041073.040185.041073.0 42591.0 40185.041073.042591.041368.043282.0 null 第10章 共用体的定义及应用 v问题的提出:表格 “学生班级 / 教师职务” name num sex job li 1011 f s 501 zhou 2085 m t prof class position struct people char name10; int num; char sex; char job; union int class; char position20; category; ; class 和 position 的使用是互斥的,可分成两列, 为节省空间,合并成一列。 第10章 共用体变量的定义 不同数据类型的一组变量使用同一组内存单元。 union data char c; float f; int i ; ; union data a,b,c; 或data a,b,c; union data char c; float f; int i ; a,b,c; union char c; float f; int i ; a,b,c; 共用体变量的定义同结构体一样有三种形式: 关键字共用体名 共用体类型 第10章 使用共用体变量 引用成员 a.i /引用共用体变量中的整型变量i a.c /引用共用体变量中的字符变量c a.f /引用共用体变量中的实型变量f 引用整体 a.i=2; b=a; /引用整体 union short int i; char c; float f; a, b, c; 第10章 共用体类型的特点 f i c 起始地址 相同 特点:三个成员共享存储单元, 即三个成员起始地址相等 (部分存储单元共用) 同一时刻,只能有一个成员有效! 图 c 占 1 个字节 i占 2 个字节 f占 4 个字节 (1) 采用内存覆盖技术,使不 同数据类型的各

温馨提示

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

评论

0/150

提交评论