项目七用C语言图形函数实现动画8.结构体.pptx
浙江安防职业技术学院,C程序设计,项目七 用C语言函数实现动画,结构体,,,浙江安防职业技术学院,C程序设计,,,浙江安防职业技术学院,C程序设计,,1,学号:long num,2,姓名:char name[20],3,年龄:int age,4,性别:char sex,5,成绩:float score[3],6,地址:char addr[30],,打包,student,,便于在程序中操作,浙江安防职业技术学院,C程序设计,,struct Student{ long num; //学号 char name[20]; //姓名 int age; //年龄 char sex; //性别 float score[3]; //成绩 char addr[30]; //地址 },新的数 据类型,关键字,浙江安防职业技术学院,C程序设计,,1,定义结构体类型:student,struct student{ long num; char name[20]; int age; char sex; float score[3]; char addr[30]; },成员变量,,2,定义结构体变量: struct student stu,3,,使用:如 stu.age,num name[20] age sex score[3] addr[30],类似取物品,浙江安防职业技术学院,C程序设计,,,已知学生小明的基本信息如下表,编写程序存储信息并在控制台打印出来。,#include struct Student{ long num; char name[20]; int age; char sex; float score [3]; char addr[30]; };,void main(){ struct Student stu={2016091012,”小明”,19,’M’,89,93,91,”杭州黄龙\n”}; printf(“学号 姓名 年龄 性别 数学 英语 物理 地址”); printf(“%d %s %d”,stu.num,stu.name,stu.age); printf(“%c %f %f %f %s\n”,stu.sex,stu.score[0],stu.score[1],stu.score[2],stu.addr); },定义Student的结构体数据类型,定义Sutdent的结构体数据类型变量stu,1,2,使用stu的成员变量等,3,程序运 行结果,,struct Tree{ long num; char name[20]; float length; int annual_rings; char type[20]; };,1,编号:long num,2,名称:char name[20],3,高度:float length,4,年轮:int annual_rings,5,种类:char type[20],结构体还能为我们解决那些问题呢?,,定义结构体 数据类型Tree,1,浙江安防职业技术学院,C程序设计,,结构体还能为我们解决那些问题呢?,定义结构体数据类 型Tree的数组tree[100],1,Struct Tree tree[100];,num name[20] length annual_rings type[20],tree[20],,使用数组元 素的成员,2,for(int i=0;i100;i++){ printf(“输入编号:“); scanf(“%d“, },,tree[100],,数据库连接代码,,,数据库中,,struct Phone { char brand[20]; char model[20]; float length; float width; float thickness; char color[10]; char system[30]; };,1,型号: char model[20],2,品牌:char brand[20],3,长度:float length,4,6,颜色:char color[10],结构体还能为我们解决那些问题呢?,,宽度:float width,5,厚度:float thickness,6,系统:char system[30],,,1、结构体是一种数据类型,它由程序员自己定义,可以包含多个其他类型的数据,知识总结,,struct Tree{ long num; char name[20]; float length; int annual_rings; char type[20]; };,2、可以用结构体定义变量和数组,复杂数据类型或构造数据类型,int a,b;,struct Tree peach,pin[8];,num name[20] length annual_rings type[20],peach,pin[8],,浙江安防职业技术学院,C程序设计,,,,3、结构体将相关联的数据打包成一个整体便于操作,知识总结,编号:long num,名称:char name[20],高度:float length,年轮:int annual_rings,种类:char type[20],,struct Tree pine,,num name[20] length annual_rings type[20],,在程序中 便于操作,浙江安防职业技术学院,C程序设计,