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

下载本文档

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

文档简介

2020年4月1日 1 第七章结构体 共用体和枚举类型数据 教材P244 7 1结构体类型数据概述 一 结构体类型的引入简单变量 数组 结构体类型 2020年4月1日 2 二 结构体类型的不确定性结构体类型中包含的数据项因问题而异 是不确定的 结构体类型必须由用户自行定义 结构体类型中的数据项称为结构体的成员 或称为域 2020年4月1日 3 三 结构体类型的定义 struct结构体名 结构体成员 structstudent charname 20 charsex intage floatscore 结构体类型的定义形式为 类型标识符 2020年4月1日 4 structtime inthour intminute intsecond 类型标识符 structtime inthour minute second 注意 此处成员不是变量 程序中完全可以用这些名字作为其它的变量名来使用 但必须另行定义 2020年4月1日 5 7 2结构体类型变量 一 结构体类型变量的定义结构体类型在一个函数内只能定义一次 如果是全局的 则在整个源程序中只能定义一次 而结构体类型变量则可以定义多个 结构体类型变量的定义方式有三种 2020年4月1日 6 structstudentstud1 stud2 structtimet1 t2 t3 structstudent charname 20 charsex intage floatscore 1 单独定义 即先定义结构体类型 然后单独定义结构体类型的变量 注意 结构体关键字struct和结构体名必须作为一个整体使用 structtime inthour intminute intsecond 2020年4月1日 7 structstudentstud3 stud4 structtimet structstudent charname 20 charsex intage floatscore stud1 stud2 2 同时定义 即在定义结构体类型时 同时给出变量名 structtime inthour intminute intsecond t1 t2 t3 2020年4月1日 8 3 直接定义 即在定义结构体类型时不指定结构体名 只给出变量名 这时结构体类型变量必须一次定义完 因为没有结构体名 不可能再单独定义 struct charname 20 charsex intage floatscore stud1 stud2 struct inthour intminute intsecond t1 t2 t3 2020年4月1日 9 1 不要将结构体名与变量名混淆 2 结构体成员的类型可以是另一个已定义的结构体类型 形成嵌套结构 例如 在定义结构体类型变量时 应注意以下几点 2020年4月1日 10 structdate intmonth intday intyear structstudent charname 20 charsex structdatebirthday floatscore 若定义structstudentstud1 则stud1的数据结构为 2020年4月1日 11 二 结构体变量的初始化 structtimet1 14 35 55 t2 structstudentstud zhangsan m 11 25 1980 95 5 2020年4月1日 12 三 结构体变量的引用 结构体变量的成员可被作为普通的变量使用 包括赋值 运算 I O等操作 成员的引用形式为 结构体变量 成员名 1 结构体变量中成员的引用 2020年4月1日 13 main structstudent charname 20 charsex intage floatscore structstudentstud1 zhangsan m 20 95 5 stud2 gets stud2 name stud2 sex f stud2 age stud1 age 2 stud2 score stud1 score stud2 age printf sis stud2 name if stud2 sex f printf female elseprintf male printf whoseageis dandscoreis 6 2f n stud2 age stud2 score scanf s stud2 name 输入 lili 显示 liliisfemalewhoseageis19andscoreis95 50 2020年4月1日 14 一般仅限于赋值 2 结构体变量作为整体的引用 main structdate intmonth day year olddate 4 30 2007 structdatenewdate newdate olddate newdate year 10 printf Thedateoftenyearslateris d d d n newdate year newdate month newdate day 显示 Thedateoftenyearslateris2017 4 30 2020年4月1日 15 通常结构体变量作为整体使用时不用于输入输出 四 结构体的输入输出 main structtime inthour intminute intsecond t scanf d d d 虽然输入 14 35 55可正确显示 14 35 55但不提倡使用 2020年4月1日 16 main structstudent charname 20 charsex intage floatscore stud zhangsan m 20 95 5 printf s c d f n stud 可将printf函数中stud改为stud name stud sex stud age stud score 输出时将产生错误 为什么 如何改正 2020年4月1日 17 五 位运算及位段 位 bit 每一个0或1称为一个 位 bit 字节 byte 8位构成一个字节 byte 内存地址 每一个字节均有一个编号 一 位运算 2020年4月1日 18 1 按位与运算符 参与运算的两个量按照对应的位进行与运算 且0 0 0 0 1 0 1 0 0 1 1 1 2 按位或运算符 参与运算的两个量按照对应位进行或运算 且0 0 0 0 1 1 1 0 1 1 1 1 3 按位异或运算符 参与运算的两个量按照对应的位进行异或运算 且0 0 0 0 1 1 1 0 1 1 1 0 2020年4月1日 19 4 按位取反运算符 位运算符中唯一的单目运算符 即将数的每一位取反 0 1 1 0 5 左移运算符 将一个数的各二进制位全部左移若干位 左移后右边补入0 左边移出的位舍弃 2020年4月1日 20 6 右移运算符 将一个数的各二进制位全部右移若干位 右移后 右边移出的位被舍弃 左边补入的位分两种情况 对无符号数 左边补入0对有符号数 若符号位为0 正数 则补入0 若符号位为1 负数 则补入1 每右移一位相当于除以2 由于上述规则 对于负数亦成立 但仅适用于右边没有1被移出的情况 2020年4月1日 21 1 位段及其定义位段是一种特殊的结构体成员 这些成员允许以位为单位进行定义和存储 可以作为独立的量参与操作和运算 二 位段 教材P253例7 4 7 5 7 6 2020年4月1日 22 2 位段的引用方法引用方法与结构体成员的引用相同 例如word a word b使用位段应注意以下几方面 1 位段只能是int或unsignedint型2 不能引用位段的地址 如 word a 3 若给位段赋的值超出其最大值 则仅截取其有效位 2020年4月1日 23 复习 1 结构体类型的引入 2 结构体类型的不确定性 1 结构体类型变量的定义 3 结构体类型的定义 2 结构体变量的初始化 3 结构体变量的引用 4 结构体的输入输出 重点 二 结构体类型变量 一 结构体类型数据概述 2020年4月1日 24 一 结构体数组的定义 二 结构体数组的初始化 三 结构体数组的引用 四 输入数据时容易出现的问题 7 3结构体数组 7 4结构体与函数 一 结构体变量作为函数参数 二 结构体类型函数 2020年4月1日 25 一 结构体数组的定义 structstudent charname 20 charsex intage floatscore structstudentstud 50 单独定义 同时定义 structtime inthour intminute intsecond t1 20 t2 40 struct intmonth intday intyear date 30 直接定义 2020年4月1日 26 二 结构体数组的初始化 structdate of birth intyear intmonth intday structdate of birthbirthday 50 1979 3 12 1978 8 23 1980 4 8 为数组birthday的前3个元素赋了初值 其余元素未赋初值 系统将数值型成员赋以0 对字符型数据赋以 0 即在定义数组时提供初值 2020年4月1日 27 若初始化时提供的数据组的个数与数组的元素个数相同 则元素个数可省略 教材P255下例 2020年4月1日 28 1 引用某元素的一个成员例如接上例birthday 3 year birthday 0 year birthday 3 month birthday 1 month birthday 3 day birthday 2 day 三 结构体数组的引用 遵从一般数组和结构体变量的规定 2020年4月1日 29 2 引用某一个元素这时一般用于赋值 与结构体变量用法类似 structdate of birthd d birthday 0 birthday 0 birthday 2 birthday 2 d 例如接上例 2020年4月1日 30 四 输入数据时容易出现的问题 include stdio h main struct charname 20 intage charsex stud scanf s d c stud name 若输入 zhangsan 20 m 若输入 zhangsan 20 m 2020年4月1日 31 可将输入改为 gets stud name scanf d c 这时输入 zhangsan 20 m 结果正确 zhangsan20m 2020年4月1日 32 但若将输入改为 gets stud name scanf d 则输入 zhangsan 20 若再改为 gets stud name scanf d 为了吸收前一个输入中的回车符对后一个输入的影响 通常增加一个语句 gets stud name scanf d 则正确 还未及输入m 程序已运行 且stud sex得到的是换行符 2020年4月1日 33 综上所述 为了避免不同类型引起的混乱 可以将各种类型的数据都用gets函数输入 单个字符则用getchar函数 然后利用C的库函数将数据转换成原来的类型 转换函数有 须包含头文件 stdlib h atoi 字符串 转换为整型atof 字符串 转换为实型atol 字符串 转换为长整型 2020年4月1日 34 7 4结构体与函数 一 结构体变量作为函数参数 可同时将各个成员的值传递到其它函数 例 2020年4月1日 35 structtime inthour intminute intsecond main voidtime output structtimet structtimetim scanf d d d voidtime output structtimet char p if t hour 12 t hour 12 p pm elsep am printf Itis d d d s n t hour t minute t second p 2020年4月1日 36 利用结构体变量作函数参数是传值方式 对形参的操作不影响实参的值 结构体类型定义在函数外 但并未定义变量 所有变量都在各函数体内 故属于局部变量 2020年4月1日 37 二 结构体类型函数 函数的类型就是其返回值的类型 若函数返回一个结构体类型的值 其中包含若干个成员 这样的函数就是结构体类型的 将上例中的输入部分设计成结构体类型函数 2020年4月1日 38 structtime inthour intminute intsecond main structtimetime input void voidtime output structtimet structtimenew time new time time input time output new time voidtime output structtimet char p if t hour 12 t hour 12 p pm elsep am printf Itis d d d s n t hour t minute t second p structtimetime input void structtimett scanf d d d 2020年4月1日 39 7 5结构体与指针 结构体指针 structstudent charname 20 charsex intage floatscore structstudentstud p p 指向结构体变量 不能指向其他类型的数据 2020年4月1日 40 通过结构体指针访问结构体成员有两种方法 strcpy p name zhangsan p sex m stud age 20 p score 95 5 printf s c d 6 2f n p name stud sex p age p score printf x x n p p 1 指针变量 成员名称为显式方式指针变量 成员名称为专用方式 例如接上定义 将上述程序作为主函数 运行后显示 zhangsan m 20 95 50ffc2 ffdd 2020年4月1日 41 2020年4月1日 42 for i 0 iname for p freshmen pname 表示输入50名新生的姓名 表示输出50名新生的姓名 structstudentfreshmen 50 p p freshmen 或写为p 二 指向结构体数组的指针 例如对于上述结构体类型 有以下定义 或写为gets p name gets freshmen i name 2020年4月1日 43 三 用结构体指针作为函数参数 只需传递一个地址 且函数中的结构体成员并不占据新的内存单元 而与主调函数中的成员共享存储单元 还可通过修改形参所指成员影响实参所对应的成员值 2020年4月1日 44 structbook charbookname 30 intquantity main voidfun structbook p structbookbook1 ProgramminginC 10 fun voidfun structbook p printf Thebook s has p bookname p quantity 3 显示 Thebook ProgramminginC has7copies 2020年4月1日 45 此外 还可将结构体数组作为函数参数 还可设计结构体指针型函数 即返回的地址是指向结构体类型数据的 2020年4月1日 46 7 6动态存储分配与链表 一 基本概念 1 内存单元的分配 静态存储分配动态存储分配 2020年4月1日 47 此外 链表中设一个头指针变量 它指向第一个结点 并且末结点 又称为表尾 的指针部分存放的是 空地址 NULL 表示它不指向任何结点 链表到此结束 2 链表 链表由若干结点构成 每一个结点含有两部分内容 数据部分和指针部分 数据部分是程序所需的 指针部分则存放下一个结点的地址 教材P256图7 15 2020年4月1日 48 3 链表的插入 链表中结点的插入如P256下图7 16 4 链表的删除 链表中结点的删除如P257下图7 17 2020年4月1日 49 二 链表的建立 1 定义结点的组成 利用包含指针成员的结构体变量构成结点 structstud score longnum floatscore structstud score next 2020年4月1日 50 2 定义结点 structstud scorestud1 stud2 stud3 stud1 num 98101 stud1 score 89 5 stud2 num 98102 stud2 score 90 5 stud3 num 98103 stud3 score 94 5 根据以上结点的组成 可定义结点实体 并输入数据 2020年4月1日 51 3 建立链表 structstud score head head 将各个结点链接起来便构成一个链表 教材P258图7 19 2020年4月1日 52 4 输出链表 访问链表 第一个结点的学号和成绩可表示为 head num head score 通过上一个结点的指针项访问下一个结点 第二个结点的学号和成绩可表示为 stud1 next num stud1 next score或head next num head next score 第三个结点的学号和成绩可表示为 head next next num head next next score 2020年4月1日 53 三 动态存储分配函数 1 malloc函数 开辟指定大小的存储空间 并返回该存储区的起始地址 函数原型为void malloc unsignedintsize 使用时应包含头文件 alloc h 或 malloc h 或 stdlib h 2020年4月1日 54 例如float p1 int p2 p1 float malloc 8 p1 3 14 p1 1 3 14 p2 int malloc 20 sizeof int for i 0 i 20 i scanf d p2 2020年4月1日 55 2 calloc函数 按所给数据个数和每个数据所占字节数开辟存储空间 例如上例中p2可改写为p2 int calloc 20 sizeof int 例如calloc 10 20 函数原型为void calloc unsignedintnum unsignedintsize 2020年4月1日 56 3 realloc函数 重新定义所开辟存储空间的大小 例如p1 float realloc p1 16 函数原型为void realloc void ptr unsignedintsize 2020年4月1日 57 4 free函数 将以前开辟的某内存空间释放 例如free void p1 将上例开辟的16个字节释放 可简写为free p1 由系统自动进行类型转换 函数原型为void free void ptr 2020年4月1日 58 四 链表应用举例 先在主函数中定义一个结点 然后每回答一次 y 则定义一个新结点加入到链表中 回答其它则打印链表中所有结点内容 2020年4月1日 59 include stdio h include alloc h structstudent charname 20 intage structstudent next structstudent head new this 定义结构体类型 定义头指针 新结点指针及操作用指针变量 2020年4月1日 60 main voidnew one void voidout put void charc intflag 1 new structstudent malloc sizeof structstudent gets new name scanf d 定义第一个结点 并输入内容 头指针指向第一个结点 2020年4月1日 61 voidnew one void new structstudent malloc sizeof structstudent gets new name scanf d 新结点指针指向空 表示末结点 链尾 原来的末结点指向新结点 voidout put void this head do printf Student s ageis d n this name this age this this next while this NULL 定义第一个结点 并输入内容 从第一个结点起搜索原链表中的末结点 2020年4月1日 62 单向链表 最简单的链表 单向链表不能逆向访问结点 双向链表 每一个结点均含有两个指针项 分别指向上一结点和下一结点 环形链表 链尾指向链头形成环路 2020年4月1日 63 环形链表简例 编号为1 n的n个人围成一圈 以第一个人为起点从1到3报数 每数到3则退出圆圈 问最后留下的人其编号是几 2020年4月1日 64 include alloc h main structperson intposition structperson next head new this inti n printf Howmanypeople scanf d n this structperson malloc sizeof structperson this position 1 head this 定义第一个结点 并使头指针指向它 定义其余n 1个结点 并形成链表 for i 2 iposition i this next new this new this next head 链尾指向链头 2020年4月1日 65 从第一个结点开始 依条件陆续删除结点 直至最后一个结点 this head while this next this for i 1 inext new next this next this this next printf ThepeopleleftisNo d n this position while循环也可写为 for k 1 k n k 每循环一次 删除一个结点 2020年4月1日 66 将不同类型的数据组织在相同的存储空间中 即在同一个存储区中先后存放不同类型的数据 共用体变量的定义方式 单独定义 同时定义 直接定义 7 7共用体类型数据 一 共用体的概念 unionexam inta floatb charc x 2020年4月1日 67 二 共用体变量的引用 1 引用共用体变量的成员只能引用当前成员的值 不能同时引用多个成员的值 例如x a 3 x b 4 5 x c A 2020年4月1日 68 引用成员时可以引入指针来进行 例如unionexam p x p 2020年4月1日 69 2 引用共用体变量共用体变量作为整体使用一般限于赋值 或作为函数参数 但这时传递的仍只有一个成员的值 例如P267例7 14 2020年4月1日 70 三 共用体变量的应用

温馨提示

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

评论

0/150

提交评论