2 num name struct student int num char

  • Slides: 44
Download presentation

一 结构体类型定义: (用于指定变量的成员名和成员的类型) 二 结构体类型的变量定义: (用于定义变量来存放数据) 变量 一般形式: 2字节 num name … 如,struct student

一 结构体类型定义: (用于指定变量的成员名和成员的类型) 二 结构体类型的变量定义: (用于定义变量来存放数据) 变量 一般形式: 2字节 num name … 如,struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }; Stu 1: 20字节 sex age 1字节 score 4字节 2字节 struct 结构体类型名 变量列表; 方式 1: struct student {char num[10]; char name[20]; char sex; int age; float score; char addr[30]; }; struct student stu 1, stu 2; stu 1, stu 2 方式 2: (!!!) struct student {char num[10]; char name[20]; char sex; int age; float score; char addr[30]; } stu 1, stu 2 ; addr …. . 如,struct student stu 1, stu 2; stu 1, stu 2 方式 3: struct {char num[10]; char name[20]; char sex; int age; float score; char addr[30]; } stu 1, stu 2 ; 30字节

三 结构体变量的引用 结构体成员的表示方法: 结构体变量名. 成员名 如, stu 1. num=123; 成员(分量)运算符 优先级: 1 结合性: 从左向右

三 结构体变量的引用 结构体成员的表示方法: 结构体变量名. 成员名 如, stu 1. num=123; 成员(分量)运算符 优先级: 1 结合性: 从左向右 四 结构体变量的初始化 例 11. 1 结构体变量的初始化 #include <stdio. h> void main() { struct student {long num; char name[20]; char sex; char addr[20]; }a={89031, 89031 "Li Lin", Lin" 'M', 'M' "148 jianshe Road"}; Road" //================ printf("NO. : %ldn name: %sn", a. num a. name); a. name printf("sex: %cn address: %sn", a. sex a. addr); a. addr }

§ 11. 5 结构体数组的定义、初始化和使用 结构体数组的每个元素都是结构体类型的变量 一 结构体数组的定义 }; ⑵ struct student ⑶ struct {

§ 11. 5 结构体数组的定义、初始化和使用 结构体数组的每个元素都是结构体类型的变量 一 结构体数组的定义 }; ⑵ struct student ⑶ struct { int num; char name[20]; char sex; int age; float score; char addr[30]; struct student stu[N 0]; } stu[N 0]; stu[0] stu[1] stu[2] stu[3]. . . ⑴ struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; stu[N 0 -1]

二 结构体数组的初始化 struct { int num; char name[20]; char sex; int age; float score;

二 结构体数组的初始化 struct { int num; char name[20]; char sex; int age; float score; char addr[30]; } stu[3]={ {10101, "李林", 'M', 18, 87. 5, "北京路 103 "}, {10102, "张奋", 'M', 19, 99, "上海路 130 "}, {10104, "王敏", 'F', 20, 78. 5, " 中山路 1010 "} }; 10101 "李林" 'M' 18 87. 5 "103 北京路" 10102 "张奋" 'M' 19 99 "130 上海路"

三 结构体数组的应用举例 例11. 2 选票的统计程序: 三个候选人, 10个选民. 算法分析: 1 存放选票的变量是结构体 2 统计过程: 10个选民每人一张选票, 和谁

三 结构体数组的应用举例 例11. 2 选票的统计程序: 三个候选人, 10个选民. 算法分析: 1 存放选票的变量是结构体 2 统计过程: 10个选民每人一张选票, 和谁 的名字一样, 谁的票数加 1. #include <stdio. h> #include <string. h> //定义统计选票的数组 struct person { char name[20]; int count; }leader[3]={"Li", 0, "zhang", 0, "Fu", 0}; void main() { int i, j; char leader_name[20]; //”投票”和统计票数 for(i=1; i<=10; i++) { printf("请投出你神圣的一票: n"); scanf("%s", leader_name); for(j=0; j<3; j++) if(strcmp(leader_name, leader[j]. name)==0) leader[j]·count++; } //输出结果 printf("n"); for(i=0; i<3; i++) printf("%5 s: %dn", leader[i]. name, leader[i]. count); }

§ 11. 6 指向结构体数据的指针 一指向结构体的指针变量 如, struct student stu, *p; p=&stu; (*p). num=89101; stu.

§ 11. 6 指向结构体数据的指针 一指向结构体的指针变量 如, struct student stu, *p; p=&stu; (*p). num=89101; stu. num=89101; p->num=89101; 例11. 3 利用指针法输出一个结构体变量 #include <stdio. h> #include <string. h> #define FORMAT " No: %ldn name: %sn sex: %cn score: %. 2 fnn" void main() 练习: 分析下列运算的含义: { struct student { long num; 1 p->n 另外注意到, C规定: char name[20]; 下面三种形式等价 2 p->n++ ( p->n)++ char sex; 1 结构体变量. 成员名 3 ++p->n ++(p->n) float score; }; 2 (*p). 成员名 struct student stu, *p; *p 3 p->成员名(指向运算符) p=&stu; 4 (“->”: 优先级为 1!!!) stu. num=89101; //还可以怎么表示? strcpy(stu. name, "Li. Li"); stu. sex='m'; stu. score=89. 5; printf(FORMAT, stu. num, stu. name, stu. sex, stu. score); printf(FORMAT, (*p). num, (*p). name, (*p). sex, (*p). score); printf(FORMAT, p->num, p->name, p->sex, p->score); }

二 用指针移动法访问结构体数组 p-> stu[0] stu[1] stu[2] stu[3]. . . 例11. 4 利用指针移动法输出结构体数组 输出 #include

二 用指针移动法访问结构体数组 p-> stu[0] stu[1] stu[2] stu[3]. . . 例11. 4 利用指针移动法输出结构体数组 输出 #include <stdio. h> #define FORMAT "%-5 d%-20 s%2 c%4 dn" struct student { int num; char name [20]; char sex; int age; }stu[3]={{10101, "Li. Lin", 'M', 18}, {10102, "Zhang Fun", 'M', 19}, {10104, "Wang Min", 'F', 20}}; //主函数 void main() { struct student *p; printf("No. Name sex agen"); for(p=stu; stu p<stu+3; p<stu+3 p++) p++ printf(FORMAT, p->num, p->name, p->sex, p->age); } stu[N 0 -1]

例11. 5结构体变量stu内含学号、姓名、三门成绩。在主函数中赋值,在函 数中输出。(结构体变量作为形参: 复制(传递)结构体变量中的所有数据) #include <stdio. h> #include <string. h> struct student { int

例11. 5结构体变量stu内含学号、姓名、三门成绩。在主函数中赋值,在函 数中输出。(结构体变量作为形参: 复制(传递)结构体变量中的所有数据) #include <stdio. h> #include <string. h> struct student { int num; char name[20]; float score[3]; }; void main( ) { void print(struct student); //函数说明 student struct student stu; stu. num=12345; strcpy(stu. Name, "Li Li"); stu. score[0]=67. 5; stu. score[1]=89; stu. score[2]=78. 6; print(stu); stu } //函数功能: 输出一个结构体变量 //入口参数: 结构体变量 (值传递!!!) void print(struct student stu) { int i; printf(" %dn %sn ", stu. num, stu. name); for (i=0; i<3; i++)printf(" %4. 1 f", stu. score[i]); printf("nn "); }

例11. 6 结构体变量stu内含学号、姓名、三门成绩。在函数中输出。 ( 结构体指针变量作为形参: 传递一个结构体变量的地址) 传递 #include <stdio. h> #include <string. h> struct

例11. 6 结构体变量stu内含学号、姓名、三门成绩。在函数中输出。 ( 结构体指针变量作为形参: 传递一个结构体变量的地址) 传递 #include <stdio. h> #include <string. h> struct student { int num; char name[20]; float score[3]; }; void main( ) { void print(struct student *); //函数说明 * struct student stu={12345, "Li Li", 67. 5, 89, 78. 6}; print(&stu); &stu } //函数功能: 输出一个结构体变量 //入口参数: 结构体变量的地址 (用结构体指针变量来传递地址!!!) 结构体指针变量 void print(struct student *p) {int i; printf(" %dn %sn ", p->num, p->name); for (i=0; i<3; i++)printf(" %4. 1 f", p->score[i]); printf("nn "); }

二 简单链表 #include <stdio. h> #define NULL 0 例11. 7 建立并输出由三个学生信 struct student //结点结构体类型

二 简单链表 #include <stdio. h> #define NULL 0 例11. 7 建立并输出由三个学生信 struct student //结点结构体类型 息结点构成的链表。学生信息包 { long num; float score; struct student *next; }; next 括:学号,成绩。 void main() { struct student a, b, c; 算法分析: struct student *head, *p; *head 1)定义链表结点的类型 //==建立链表 2)建立链表:建立结点, a. num=99101; a. score=89. 5; //给结点赋值 赋值,连接。 b. num=99103; b. score=90; c. num=99107; c. score=85; 3)输出链表:依次逐个结 head=&a; a. next=&b; //连接结点 点输出。 b. next=&c; c. next=NULL; //==输出链表 p=head; do{ printf("%ld %5. 1 fn", p->num, p->score); head &a A 99101 89. 5 &b B 99103 90 p=p->next; //前进指针 }while(p!=NULL); } C &c 99107 85

例11. 8 建立链表的函数 #include <stdio. h> #include <malloc. h> #define NULL 0 #define LEN

例11. 8 建立链表的函数 #include <stdio. h> #include <malloc. h> #define NULL 0 #define LEN sizeof(struct student) #define STUNODE struct student //--定义链表的结点类型 STUNODE { long num; float score; STUNODE *next; }; //-----------int n; //全局变量,放结点个数

对链表的综合操作 将前边 4个函数放在一 个c程序中,再加上一个 主程序就可以实现对链表 的综合处理: void main() { STUNODE *head, stu; stu long

对链表的综合操作 将前边 4个函数放在一 个c程序中,再加上一个 主程序就可以实现对链表 的综合处理: void main() { STUNODE *head, stu; stu long del_num; printf("n 输入学生记录: n"); head=create(); print(head); //--删除操作 printf("n 输入要删除学生的学号: n"); scanf("%ld", &del_num); head=del(head, del_num); print(head); //--插入操作 printf("n输入要插入学生的记录: n"); scanf("%ld%f", &stu. num, &stu. score); head=insert(head, &stu); print(head); }

八 对链表的多个结点删除与插入: void main() { STUNODE *head, *stu; stu long del_num; printf ("n 输入学生记录:

八 对链表的多个结点删除与插入: void main() { STUNODE *head, *stu; stu long del_num; printf ("n 输入学生记录: n"); head=create(); print (head); //==多个结点的插入 //==多个结点的删除 while(stu->num!=0) printf("n 输入要删除学生的学号: "); { head=insert(head, stu); scanf("%ld", &del_num); while(del_num!=0) { head=del(head, del_num); print(head); printf("n 输入要删除学生的学号: "); scanf("%ld", &del_num); } printf("n 输入要插入学生的记录: n"); stu=(STUNODE*)malloc(LEN); scanf("%ld%f", &stu->num, &stu->score); print (head); printf("n输入要插入学生的记录: n"); stu=(STUNODE*)malloc(LEN); scanf("%ld%f", &stu->num, &stu->score); } }

例11. 12一个学校的选举登记:其中有学生和教师 (共用体的应用) 学生登记信息为:号码 姓名 性别 职业 教师登记信息为:号码 姓名 性别 职业 struct { int

例11. 12一个学校的选举登记:其中有学生和教师 (共用体的应用) 学生登记信息为:号码 姓名 性别 职业 教师登记信息为:号码 姓名 性别 职业 struct { int num; char name[10]; char sex; char job; union { int class; char position[10]; }category; category }; 班级 职务 num name sex 1011 Li F 2086 Wang M job class S position 501 T prof

例11. 12一个学校的选举登记:其中有学生和教师 (共用体的应用) #include <stdio. h> #define N 0 2 #define struct FORMAT "%6

例11. 12一个学校的选举登记:其中有学生和教师 (共用体的应用) #include <stdio. h> #define N 0 2 #define struct FORMAT "%6 d%-10 s%-3 c%" { int num; char name[10]; char sex; char job; union { int class; char position[10]; } category; }p[N 0];

例11. 12 共用体的应用:程序 (接上页 1) void main( ) { int i; printf("请输入%d个人的信息: n", N

例11. 12 共用体的应用:程序 (接上页 1) void main( ) { int i; printf("请输入%d个人的信息: n", N 0); printf("学号 姓名 性别 作 : n"); for (i=0; i<N 0; i++) {scanf ("%d%s %c %c", &p[i]·num, p[i]·name, &p[i]·sex, &p[i]·job); if(p[i]·job=='s') {printf("请输入班级: n"); scanf("%d", &p[i]·category·class); } else if(p[i]·job=='t' ) {printf("请输入职称 : n"); scanf("%s", p[i]·category·position); } else printf("输入错误!n"); } printf("n学号 姓名 性别 作 班级/职称: n"); for(i=0; i<N 0; i++) { printf(FORMAT, p[i]·num, p[i]·name, p[i]·sex, p[i]·job); if(p[i]·job=='s')printf("%-6 dn", p[i]. category. class); else printf("%-6 sn", p[i]·category·position); }}

§ 11. 10 用typedef定义 新类型名 typedef 例 typedef int INTEGER; INTEGER 例 typedef float

§ 11. 10 用typedef定义 新类型名 typedef 例 typedef int INTEGER; INTEGER 例 typedef float (定义类型的别名 ) 例 INTEGER a, b, c; REAL f 1, f 2; REAL; REAL 类型名定义后, 与已有类型名一样使用 例 定义结构体类型 typedef struct { int month; int day; int year; }DATE; DATE int a, b, c; float f 1, f 2; 例 DATE d 1, d 2;