struct student int num char name20 float score

  • Slides: 22
Download presentation

结构体变量的初始化 定义时初始化:将各元素初值放在“{ }”里赋值给变量。 例: struct student{ int num; char name[20]; float score 1; float

结构体变量的初始化 定义时初始化:将各元素初值放在“{ }”里赋值给变量。 例: struct student{ int num; char name[20]; float score 1; float score 2; float score 3; float aver; } stu[3]={{001, "zhang 3", 80, 88, 75}, {002, "li 4", 90, 83, 84}, {003, "wang 5", 50, 62, 65}}; Ø 可以这样定义结构体变量 5

结构体变量的定义 存储: (1)结构体的所有成员各自占用不 同的内存单元 (2)一共占用多少字节存储单元? sizeof( struct student) struct student{ int num; char name[20];

结构体变量的定义 存储: (1)结构体的所有成员各自占用不 同的内存单元 (2)一共占用多少字节存储单元? sizeof( struct student) struct student{ int num; char name[20]; float score 1; float score 2; float score 3; float aver; }stu 1, stu 2; stu[3]; stu[0] stu 1 38字节 stu[1] stu 2 38字节 001 “zhang 3” 80 88 75 002 “li 4” 90 83 84 int num char name[20] float score 1 float score 2 float score 3 float aver stu[2] 38字节 6

结构体 变量引用 例11 -1要求计算平均成绩,并输出学生的学号,姓名和平均成绩 struct student{ int num; char name[20]; float score 1; float

结构体 变量引用 例11 -1要求计算平均成绩,并输出学生的学号,姓名和平均成绩 struct student{ int num; char name[20]; float score 1; float score 2; float score 3; float aver; }stu[3]={{001, "zhang 3", 80, 88, 75}, {002, "li 4", 90, 83, 84}, {003, "wang 5", 50, 62, 65}}; stu[0] stu[1 ] 001 “zhang 3” 80 88 75 002 “li 4” 90 83 84 num name[20] score 1 score 2 score 3 aver for(i=0; i<3; i++) { stu[i]. aver= stu[i]. score 1+ stu[i]. score 2+ stu[i]. score 3; stu[i]. aver /=3; } 成员的引用方式:结构体变量名. 成员名 7

结构体例题 A 0901 :#include<stdio. h> struct student{ int num; char name[20]; float score 1;

结构体例题 A 0901 :#include<stdio. h> struct student{ int num; char name[20]; float score 1; float score 2; float score 3; float aver; }; void main() { struct student stu[3]={{001, "zhang 3", 80, 88, 75}, {002, "li 4", 90, 83, 84}, {003, "wang 5", 50, 62, 65}}; int i; for(i=0; i<3; i++) { stu[i]. aver=stu[i]. score 1+stu[i]. score 2+stu[i]. score 3; stu[i]. aver /=3; } for(i=0; i<3; i++) printf("%5 d%20 s%8. 2 fn", stu[i]. num, stu[i]. name, stu[i]. aver); 9 }

指向结构体变量的指针 A 0901 :#include<stdio. h> struct student{ int num; char name[20]; float score 1;

指向结构体变量的指针 A 0901 :#include<stdio. h> struct student{ int num; char name[20]; float score 1; float score 2; float score 3; float aver; }stu={001, "zhang 3", 80, 88, 75}; P 001 “zhang 3” 80 88 75 struct student *p=&stu; stu. score 2+ p-> stu. score 3; p-> score 1+ p-> aver=stu. aver /=3; (*p). printf("%5 d%20 s%8. 2 fn", p-> stu. num, (*p). stu. name, (*p). stu. aver); 1、结构体类指针变量的定义:结构体类型名称 *指针变量名 2、通过指针变量引用成员: 1) 结构体类指针变量名->成员名 (常用) 2) (*结构体类指针变量名). 成员名 10

指向结构体数组的指针 A 0901 :#include<stdio. h> struct student{ }; int num; char name[20]; float score

指向结构体数组的指针 A 0901 :#include<stdio. h> struct student{ }; int num; char name[20]; float score 1; float score 2; float 001 aver; P score 3; float “zhang 3” stu[3]={{001, "zhang 3", 80, 88, 75}, {002, "li 4", 90, 83, 84}, {003, "wang 5", 50, 62, 65}}; struct stud *p=stu; p++ 指向数组中 的下一个元素 stu[0] 80 88 75 002 for(p=stu; p<stu+3; p++) “li 4” { p->aver=p->score 1+p->score 2+p->score 3; 90 stu[1] p->aver /=3; 83 84 } for(p=stu; p<stu 3; p++) printf("%5 d%20 s%8. 2 fn", p->num, p->name, p->aver); stu[2] 11

构建链表 void main() struct student { struct student a, b, c, *head, *p; {

构建链表 void main() struct student { struct student a, b, c, *head, *p; { int num; float score; a. num=001; a. score=89. 5; struct student *next; b. num=002; b. score=90; }; c. num=004; c. score=85; a b c head=&a; head a. next=&b; P 001 P 002 P 004 next b. next=&c; 89. 5 90 85 c. next=NULL; p=head; next while ( p!=NULL) { printf(“%d % f n”, p->num, p->score); NULL P p=p->next; } 14 }

链表操作:插入、删除 1. malloc 函数: //动态申请一个节点空间 内存动态存储区中分配一个长度 struct student *d; 为size的连续空间 int len=sizeof(struct student); 2.

链表操作:插入、删除 1. malloc 函数: //动态申请一个节点空间 内存动态存储区中分配一个长度 struct student *d; 为size的连续空间 int len=sizeof(struct student); 2. free函数 释放空间 d=(struct student *) malloc(len); d--> //为节点赋值 003 scanf("%d", &(d->num)); 78 scanf("%f", &(d->score)); next a b //将节点插入链表head c d->next=b. next; 004 001 002 next b. next=d; 85 89. 5 90 //链表删除元素d next b. next=d->next free(d); //释放空间 =NULL 15

main(){ struct person{ char name[20]; int count; }pers[3]={{"Zhang", 0}, {"Wang", 0}, {"Li", 0}}; int

main(){ struct person{ char name[20]; int count; }pers[3]={{"Zhang", 0}, {"Wang", 0}, {"Li", 0}}; int i=0, j=0; char nam[20]; for (; i<10; i++){ /*控制投票人数*/ scanf(“%s”, nam); /*输入候选人姓名*/ for(j=0; j<3; j++) /*控制候选人数*/ if(strcmp(nam, pers[j]. name)==0) pers[j]. count++; } printf("n"); for(i=0; i<3; i++) printf("%6 s %d", pers[i]. name, pers[i]. count); } 17

结构体变量嵌套 成员也可以是一个结构变量。 struct clock { int hour, minute, second; }; struct date { int

结构体变量嵌套 成员也可以是一个结构变量。 struct clock { int hour, minute, second; }; struct date { int year, month, day; struct clock time; } today, nextday ; 1. 单独引用结构体变量的成员 today. year=2004; today. time. second=15; 2. 结构体变量作为一个整体引用 nextday=today; 18

枚举类型 一、枚举类型的定义[格式]: enum 枚举类型名称 {枚举类成员名} 例:enum weekday{sun, mon, tue, wed, thu, fri, sat}; 0

枚举类型 一、枚举类型的定义[格式]: enum 枚举类型名称 {枚举类成员名} 例:enum weekday{sun, mon, tue, wed, thu, fri, sat}; 0 1 2 3 4 5 6 枚举元素从数值 0开始编号,按常量处理 例: main(){ enum weekday{sun, mon, tue, wed, thu, fri, sat}; enum weekday today=mon; printf(“today=%d ", today); } 运行结果: today=1 20

小结——自定义数据类型 struct student{ int num; char name[20]; float score 1; float score 2; float

小结——自定义数据类型 struct student{ int num; char name[20]; float score 1; float score 2; float score 3; float aver; }stu, s[3]; Ø结构体: Ø 结构体变量的引用 Ø结构体数组 Ø用指针访问结构体 Ø构建链表 Ø共用体 001 002 004 Ø枚举 89. 5 90 85 next 21