浙江大学C颜晖原版C9

上传人:资****亨 文档编号:136765962 上传时间:2020-07-02 格式:PPT 页数:46 大小:173KB
返回 下载 相关 举报
浙江大学C颜晖原版C9_第1页
第1页 / 共46页
浙江大学C颜晖原版C9_第2页
第2页 / 共46页
浙江大学C颜晖原版C9_第3页
第3页 / 共46页
浙江大学C颜晖原版C9_第4页
第4页 / 共46页
浙江大学C颜晖原版C9_第5页
第5页 / 共46页
点击查看更多>>
资源描述

《浙江大学C颜晖原版C9》由会员分享,可在线阅读,更多相关《浙江大学C颜晖原版C9(46页珍藏版)》请在金锄头文库上搜索。

1、第九章 结构,结构 结构数组 结构指针 链表 位运算 自定义类型,学号,姓名,性别,出生地,出生年,出生月,出生日,数学,物理,程序设计,结构:同一个数据项的若干成分构成的一个整体。 例如:学生档案,每个学生有学号、姓名、性别、出生地、出生年月、学业成绩等。,9.1 结构,9.1.1 结构的定义 struct student long int num; char name20; float score; ; 定义一个结构类型: struct student,9.1.2 结构变量的定义,1、先定义结构类型,再定义变量 struct student long int num; char name2

2、0; float score; ; struct student stu1, stu2;,2、定义结构类型的同时定义变量 struct student long int num; char name20; float score; stu1, stu2;,3、不指定类型名,只定义变量 struct long int num; char name20; float score; stu1, stu2;,9.1.3 结构变量的初始化,只有全局变量或静态变量才能初始化。 static struct student stu2=200012, “Li”, 94;,struct student long

3、num; char name20; float score; stu1=200011, Zhang, 85;,9.1.4 结构变量的使用,1、结构类型变量的整体引用 (1) 不能整体输入输出,但相同类型的变量可以互相赋值 printf(%ld%s%f, stu1); 非法 stu2=stu1; 合法 (2) 可以引用结构体变量的地址 printf(%x, 输出stu1的首地址,2、结构变量中分量的引用 struct student long int num; char name20; float score; stu1, stu2;,(1) 结构变量.分量 stu1.num = 9901; p

4、rintf(%s, stu2.name);,(2) 结构变量中的分量可以依据它的类型进行各种运算 x = stu1.score; strcpy(stu1.name, “Wang”); (3) 可以引用结构变量中的分量的地址 scanf(%ld, ,9.2 结构数组,一个结构变量只能存放一个学生的资料。 若班上有20个学生,需要用结构数组。 即,数组中的每个元素都是结构类型。 9.2.1 定义 struct student long int num; char name20; float score; stu20;,9.2.2 初始化 struct student long int num; c

5、har name20; float score; stu20=200011,”Zhang”,85, 200012,”Li”,90;,9.2.3 引用 struct student long int num; char name20; float score; stu20; stu0.num stu0.name stu0.score,程序举例,例1、输入某班30位学生的姓名及数学、英语成绩,计算并输出每位学生的平均分。 struct student char name10; int math, eng; float aver; ;,void main( ) struct student s30;

6、 int i; for(i=0; i30; i+) scanf(%s%d%d, si.name, ,输入某班30位学生的姓名及数学、英语成绩,计算并输出每门课程的平均分。 void main( ) struct student s30; int i; float aver_m=0, aver_e=0;,例2,for(i=0; inum = 200011; ptr-score = 85; strcpy(ptr-name,”Zhang”); 当ptr = int m; int d; ,struct student long int num; char name20; struct day birt

7、hday; float score; ,struct day int y; int m; int d; ; struct student long int num; char name20; struct day birthday; float score; stu1, stu2;,或: struct student long int num; char name20; struct int y, m, d; birthday; float score; stu1, stu2;,struct student long int num; char name20; struct int y, m,

8、 d; birthday; float score; stu1=9901, “Zhao”, 1980, 10,30, 80, stu2; stu2.birthday.y=1979; stu2.birthday.y=1; stu2.birthday.y=20;,9.4.2 单向链表,struct student long int num; float score; struct student *next; 结构的递归定义,1、动态内存分配函数,(1) void *malloc(unsigned size) 功能:在内存的动态存贮区中分配一块长度为size的连续空间。 返回值:指针,存放被分配内

9、存的起始地址。若未申请到空间,则返回 NULL( 0 )。 void *:指向任何类型的数据,在使用时,要进行强制类型转换。 例如:(int *) malloc(sizeof(int) (struct student *) malloc(sizeof(struct student),(2) void free(void *ptr) 功能:释放由malloc()申请的动态内存空间,ptr存放该空间的首地址。 返回值:无。 p=(struct student *) malloc(sizeof(struct student); free(p);,2、建立链表,编写一个函数,要求用单向链表建立学生档案

10、,从键盘输入数据,如果学号为0,输入结束,并返回链表的头指针。 struct student long int num; float score; struct student *next; ; struct student *head, *tail, *p; head=tail=NULL;,struct student *head, *tail, *p; head=tail=NULL; size=sizeof(struct student); p = (struct student *) malloc(size);,p,head,tail,tail,head=tail=NULL; input

11、 num, score while num!=0 p= (struct student *) malloc(sizeof(size) p-num=num, p-score=score, p-next=NULL y head=NULL n head=p tail-next=p tail=p input num, score,# include stdio.h struct student int num; float score; struct student *next; ; struct student *creat( ); main( ) struct student *head; hea

12、d = creat( ); ,struct student *creat() struct student *head, *tail, *p; float score; int num, size = sizeof(struct student); head = tail = NULL; scanf(%d %f, ,3、遍历链表,ptr-num ptr-score,for(ptr=head; ptr!=NULL; ptr=ptr-next) printf(“%ld, %f”, ptr-num, ptr-score);,ptr=ptr-next,# include stdio.h struct

13、student int num; float score; struct student *next; ; struct student *creat( ); void print(struct student *head) main( ) struct student *head; head = creat(); print(head); ,void print(struct student *head) struct student *ptr; if(head = NULL) printf(n No listn); return; printf(n listn); for (ptr = h

14、ead; ptr; ptr = ptr-next) printf( %d %fn, ptr-num, ptr-score); ,4、对链表的删除操作,ptr2=ptr1-next ptr1-next=ptr2-next,free(ptr2) ptr2=ptr1-next,4、对链表的插入操作,s-next = ptr-next ptr-next = s 先连后断,9.5 位运算,9.5.1 位运算符 1、位逻辑运算符 按位取反(与!同级) 单目右结合 int i, j; INTEGER i, j; 例2、 typedef int* POINT; int* p1; POINT p1;,定义方法 (1) 定义变量 int i (2)变量名新类型名 int INTEGER (3)加上typedef typedef int INTEGER (4) 用新类型名定义变量 INTEGER i; 例 int num10 int NUM10 typedef int NUM10 NUM a int a10,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号