1 / 26

第 9 章 用户自己建立数据类型

第 9 章 用户自己建立数据类型. 9.1 定义和使用结构体变量 结构体是 一种 构造 数据类型 用途:把 不同类型 的数据组合成一个整体------- 自定义 数据类型 结构体类型定义. 合法标识符 可省 : 无名结构. struct [ 结构 体 名 ] { 类型标识符 成员名; 类型标识符 成员名; ……………. } ;. 成员类型可以是 基本型或构造型. struct 是 关键字 , 不能省略. name[0]. name[1]. name[2]. name[3]. …….

daria
Télécharger la présentation

第 9 章 用户自己建立数据类型

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第9章 用户自己建立数据类型 • 9.1定义和使用结构体变量 • 结构体是一种构造数据类型 • 用途:把不同类型的数据组合成一个整体-------自定义数据类型 • 结构体类型定义 合法标识符 可省:无名结构 struct [结构体名] { 类型标识符 成员名; 类型标识符 成员名; ……………. }; 成员类型可以是 基本型或构造型 struct是关键字, 不能省略

  2. name[0] name[1] name[2] name[3] …… 保存10位同学的姓名和成绩(用两个数组分别保存学生的姓名和成绩) char name[10][80]; int grade[10],i; for( i=0; i<10; i++) { gets(name[i]); scanf("%d", &grade[i]); getchar(); } for( i=0;i<10;i++) { printf("%s %5d", name[i],grade[i] ); printf("\n"); } struct student { char name[20]; int grade; }; struct student stu[10];

  3. 例如: strcut card { int pips; char suit; }; 结构体类型定义描述结构 的组织形式,不分配内存 其中,struct是关键字,card是结构名,struct card结构类型有2个成员:int型的pips和char型的suit。 该结构用来描述一张扑克牌的属性:点数(pips)和花色(suit),点数用int型数1,2, …,13表示,花色用字符h,s,c,d表示红桃,黑桃,梅花和方块。

  4. 例 struct date { int month; int day; int year; }; struct student { int num; char name[20]; struct date birthday; }; 例 struct student { int num; char name[20]; struct date { int month; int day; int year; }birthday; }; birthday birthday num num name name month month day day year year • 结构体类型的定义是可嵌套的,即可用另一个结构体类型作为该结构成员的类型。 又例如:

  5. struct 结构体名 { 类型标识符 成员名; 类型标识符 成员名; ……………. }; struct 结构体名 变量名表列; 结构体变量的定义 • 先定义结构体类型,再定义结构体变量 • 一般形式:

  6. pips 4byte c1 suit 1byte 4byte pips c2 suit 1byte ….. 例如: strcut card { int pips; char suit; }; struct card c1,c2; c1,c2 结构体变量 内存中数据依次存储 • 说明:结构体类型与结构体变量概念不同 • 类型:不分配内存; 变量:分配内存 • 类型:不能赋值、存取、运算; 变量:可以

  7. struct 结构体名 { 类型标识符 成员名; 类型标识符 成员名; ……………. }变量名表列; • 定义结构体类型的同时定义结构体变量 一般形式: 例如: strcut card { int pips; char suit; }c1,c2;

  8. 直接定义结构体变量 一般形式: struct { 类型标识符 成员名; 类型标识符 成员名; ……………. }变量名表列; 用无名结构直接定义 变量只能一次 例如: strcut { int pips; char suit; }c1,c2;

  9. 结构体变量的引用 引用规则: 结构体变量不能整体引用,只能引用变量成员 引用方式: 结构体变量名.成员名 • struct card c1,c2; • c1.pips = 10; • c1.suits = ‘s’; //不能将一个结构体变量作为一个整体进行输入和输出 • c1 = {2,’c’};× • printf(“%d,%c\n”,c1); ×

  10. //成员变量输入、输出 • c1.pips=2; c1.suit=‘c’;√ • printf(“%d,%c\n”,c1.pips,c1.suit); √ //可以将一个结构体变量赋值给另一个结构体变量 • c1 = c2; //每个成员值相等

  11. struct 结构体名 { 类型标识符 成员名; 类型标识符 成员名; ……………. }; struct 结构名 结构体变量={初始数据}; 结构体变量的初始化 • 形式一: strcut card { int pips; char suit; }; struct card cr = {13, ‘d’};

  12. 形式二: struct 结构体名 { 类型标识符 成员名; 类型标识符 成员名; ……………. }结构体变量={初始数据}; strcut card { int pips; char suit; }cr = {13, ‘d’};

  13. 形式三: struct { 类型标识符 成员名; 类型标识符 成员名; ……………. }结构体变量={初始数据}; strcut { int pips; char suit; }cr = {13, ‘d’};

  14. #include<stdio.h> struct student { int num; char name[20]; char sex; char addr[20]; }; int main() { void fun(struct student x); struct student a={89031,"Li Lin",'M',"123 Beijing Road"}; printf("NO.:%d\nname:%s\nsex:%c\naddress:%s\n",a.num,a.name,a.sex,a.addr); fun(a); return 0; } void fun(struct student x) { printf("NO.:%d\nname:%s\nsex:%c\naddress:%s\n",x.num,x.name,x.sex,x.addr); }

  15. 10101 10102 Zhang Fun Li Lin 25B stu[0] M F 18 19 stu[1] 形式一: struct student { int num; char name[20]; char sex; int age; }; struct student stu[2]; • 9.2 结构体数组 • 结构体数组的定义 形式三: struct { int num; char name[20]; char sex; int age; }stu[2]; 形式二: struct student { int num; char name[20]; char sex; int age; }stu[2];

  16. 例: 记录班级100个学生基本信息(姓名,学号,成绩),统计成绩不合格人数

  17. #define CLASS_SIZE 100 • 定义新的数据类型 : struct student • struct student • { char name[10]; int stu_id; int grade; • };

  18. //定义结构体变量、数组 • struct student temp, class[CLASS_SIZE]; • //结构数组输入 • for( i=0; i<n; i++) • { scanf(“%s”,temp.name); //Read String scanf(“%d”,&temp.stu_id); scanf(“%d”, &temp. grade); class[i] = temp; • } • 注意: 使用 temp 变量的情况

  19. 注意: 不使用 temp 变量的情况 • //定义结构体变量、数组 struct student class[CLASS_SIZE]; //结构体数组初始化 for( i=0; i<n; i++) { scanf(“%s”, class[i] .name); scanf(“%d”, &class[i].stu_id); scanf(“%d”, &class[i]. grade); }

  20. 统计不合格人数的函数 • int count_fail( struct student s[],int n) • { int i, cnt=0; for(i=0;i<n;++i) { if ( s[i].grade < 60 ) cnt ++; } return cnt; • }

  21. #include<stdio.h> #define CLASS_SIZE 100 int count_fail( struct student s[],int n); struct student { char name[10]; int stu_id; int grade; };

  22. int main() {struct student temp, class[CLASS_SIZE]; int n,i,failcount; printf("请输入人数:"); scanf("%d",&n); for( i=0; i<n; i++) { scanf("%s",temp.name); scanf("%d",&temp.stu_id); scanf("%d",&temp.grade); class[i] = temp; } failcount=count_fail(class,n); for( i=0; i<n; i++) { printf("%10s ",class[i].name); printf("%6d ",class[i].stu_id); printf("%4d\n",class[i].grade);} } printf("不及格人数=%d\n",failcount); return 0;}

  23. int count_fail( struct student s[],int n) { int i, cnt=0; for(i=0;i<n;++i) if ( s[i].grade<60 )cnt ++; return cnt; }

  24. p num name struct student { int num; char name[20]; char sex; int age; }stu; struct student *p=&stu; stu sex age 存放结构体变量在内存的起始地址 • 9.3 结构体和指针 • 指向结构体变量的指针 • 定义形式:struct 结构体名 *结构体指针名; • 例 struct student *p; 例 int n; int *p=&n; *p=10; n=10

  25. (*结构体指针名).成员名 结构体指针名->成员名 结构体变量名.成员名 • 使用结构体指针变量引用成员形式 例 指向结构体的指针变量 struct student stu1; struct student *p=&stu1; stu1.num=101;  (*p).num=101;p->num=101;

  26. int main() { struct student { long int num; char name[20]; char sex; float score; }stu_1,*p; p=&stu_1; stu_1.num=89101; strcpy(stu_1.name,"Li Lin"); p->sex='M'; p->score=89.5; printf("\nNo:%ld\nname:%s\nsex:%c\nscore:%f\n", (*p).num,p->name,stu_1.sex,p->score); return 0; }

More Related