C程序设计第9章自定义数据类型.ppt
《C程序设计第9章自定义数据类型.ppt》由会员分享,可在线阅读,更多相关《C程序设计第9章自定义数据类型.ppt(22页珍藏版)》请在三一办公上搜索。
1、1,C程序设计第9章 自定义数据类型,主讲教师:鲁 萍西安建筑科技大学 理学院,2,第9章 自定义数据类型,小 结,习 题,结构体变量结构体数组结构体与指针指针与链表共用体枚举,3,结构体类型的定义,例1 学生信息:学号,姓名,成绩,成绩,成绩,平均成绩要求计算平均成绩,并输出学生的学号,姓名和平均成绩 问题:学生信息包含不同类型的数据例2 对一个班的若干学生进行管理,实现插入新生信息和删除转学学生信息的功能。问题:新数据无法和已有数据连续存储,结构体 链表,4,结构体类型的定义,例1学生信息:学号,姓名,成绩,成绩,成绩,平均成绩要求计算平均成绩,并输出学生的学号,姓名和平均成绩,struc
2、t studentint num;char name20;float score1;float score2;float score3;float aver;,结构体类型:不同类型的数据的有序集合 struct 类型名称 成员表列;结构体变量:用结构体类型说明的变量(定义结构体类型不分配内存单元!定义结构体变量时分配存储单元),/定义个结构体变量struct student stu1,stu2;struct student stu3;/定义3个元素的结构体数组,5,结构体变量的初始化,定义时初始化:将各元素初值放在“”里赋值给变量。例:struct student int num;char n
3、ame20;float score1;float score2;float score3;float aver;stu3=001,zhang3,80,88,75,002,li4,90,83,84,003,wang5,50,62,65;,可以这样定义结构体变量,6,结构体变量的定义,struct studentint num;char name20;float score1;float score2;float score3;float aver;stu1,stu2;,存储:(1)结构体的所有成员各自占用不同的内存单元(2)一共占用多少字节存储单元?sizeof(struct student),
4、stu3;,stu0stu1stu2,int numchar name20float score1float score2float score3float aver,int numchar name20float score1float score2float score3float aver,7,for(i=0;i3;i+)aver=score1+score2+score3;aver/=3;,结构体 变量引用,例11-1要求计算平均成绩,并输出学生的学号,姓名和平均成绩,struct student int num;char name20;float score1;float score2
5、;float score3;float aver;stu3=001,zhang3,80,88,75,002,li4,90,83,84,003,wang5,50,62,65;,stui.stui.stui.stui.stui.,成员的引用方式:结构体变量名.成员名,numname20score1score2score3aver,numname20score1score2score3aver,8,输出学生的所有信息 printf(”%d,%s,%f,%f,%f,%fn”,stud1);不能对结构体变量整体输入输出,只能对各个成员分别输入输出printf(”%d,%s,%f,%f,%f,%fn”,s
6、tud1.num,stu1.name,stu1.score1,stu1.score2,stu1.score3,stu1.aver);(),结构体变量引用,A0901:学生信息:学号,姓名,成绩,成绩,成绩,平均成绩要求计算平均成绩,并输出学生的学号,姓名和平均成绩,for(i=0;i3;i+)printf(%5d%20s%8.2fn,stui.num,stui.name,stui.aver);,9,结构体例题,A0901:#includestruct studentint num;char name20;float score1;float score2;float score3;float
7、aver;void main()struct student stu3=001,zhang3,80,88,75,002,li4,90,83,84,003,wang5,50,62,65;int i;for(i=0;i3;i+)stui.aver=stui.score1+stui.score2+stui.score3;stui.aver/=3;for(i=0;i3;i+)printf(%5d%20s%8.2fn,stui.num,stui.name,stui.aver);,10,指向结构体变量的指针,A0901:#includestruct studentint num;char name20;f
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 程序设计 自定义 数据类型

链接地址:https://www.31ppt.com/p-5426173.html