C语言程序设计基础第9章 结构体_第1页
C语言程序设计基础第9章 结构体_第2页
C语言程序设计基础第9章 结构体_第3页
C语言程序设计基础第9章 结构体_第4页
C语言程序设计基础第9章 结构体_第5页
已阅读5页,还剩42页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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

2、har name20; float score; ;struct student stu1, stu2; num namescorestu1200011Zhang85stu2200012Li942、定义结构类型的同时定义变量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 stude

3、nt stu2=200012, “Li”, 94; num namescorestu2200012Li94 num namescorestu1200011Zhang85struct student long 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); 输出stu1的首地址2、结

4、构变量中分量的引用struct student long int num; char name20; float score; stu1, stu2;(1) 结构变量.分量 stu1.num = 9901; printf(%s, ); num namescorestu1200011Zhang85stu2200012Li94(2) 结构变量中的分量可以依据它的类型进行各种运算x = stu1.score;strcpy(, “Wang”);(3) 可以引用结构变量中的分量的地址 scanf(%ld, &stu1.num);9.2 结构数组一个结构变量只能存放一个

5、学生的资料。若班上有20个学生,需要用结构数组。即,数组中的每个元素都是结构类型。9.2.1 定义 struct student long int num; char name20; float score; stu20;stu0200011Zhang85stu19200012Li90200029Zhao70stu19.2.2 初始化 struct student long int num; char name20; float score; stu20=200011,”Zhang”,85, 200012,”Li”,90;stu0200011Zhang85stu19200012Li902000

6、29Zhao70stu19.2.3 引用 struct student long int num; char name20; float score; stu20;stu0.scorestu0200011Zhang85stu19200012Li90200029Zhao70stu1程序举例例1、输入某班30位学生的姓名及数学、英语成绩,计算并输出每位学生的平均分。struct student char name10; int math, eng; float aver; ;void main( ) struct student s30; int i; for(i

7、=0; i30; i+) scanf(%s%d%d, , &si.math, &si.eng); si.aver = (si.math+si.eng)/2.0 printf(%s%f, , si.aver); s0s29s1Zhang8085Li7790wang6078输入某班30位学生的姓名及数学、英语成绩,计算并输出每门课程的平均分。void main( ) struct student s30; int i; float aver_m=0, aver_e=0;例2 for(i=0; i30; i+) scanf(%s%d%d, , &si.mat

8、h, &si.eng); aver_m += si.math; aver_e += si.eng; printf(%f%f, aver_m/30, aver_e/30); 输入30位学生的姓名及数学、英语成绩,输出平均分最高的学生的姓名及其数学和英语成绩。struct student char name10; int math, eng; float aver; ;例3void main( ) struct student s30; int i, sub; for(i=0; i30; i+) scanf(%s%d%d, , &si.math, &si.eng); si.aver

9、= (si.math+si.eng)/2.0 sub=0; for( i=1; i ssub.aver ) sub = k; printf(%s%d%dn, , ssub.math, ssub.eng);9.3 结构指针9.3.1 定义struct student long int num; char name20; float score; ;struct student stu1, *ptr;ptr = &stu1; num namescorestu1200011Zhang85ptr9.3.2 结构指针的使用struct student stu1, *ptr = &stu

10、1;stu1.num=200011; stu1.score=85; strcpy(,”Zhang”);通过指针 ptr 访问结构分量(1) *ptr (*ptr).num = 200011; (*ptr).score = 85; strcpy(*ptr).name,”Zhang”); num namescorestu1200011Zhang85ptr(2) - (*ptr).num = 200011; ptr-num = 200011; ptr-score = 85; strcpy(ptr-name,”Zhang”);当ptr = &stu1时stu1.num(*ptr).n

11、umptr-num 三者等价 num namescorestu1200011Zhang85ptrptr-num + 等价于 (ptr-num) + ptr-num 等价于 + (ptr-num)(+ ptr)-num num namescorestu1200011Zhang85ptr9.4 链表9.4.1结构的嵌套定义struct day int y; int m; int d;年月日成绩出生日期姓名学号struct student long int num; char name20; struct day birthday; float score; ymdscorebirthdayname

12、numstruct day int y; int m; int d;struct student long int num; char name20; struct day birthday; float score; stu1, stu2;ymdscorebirthdaynamenum或: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,

13、 d; birthday; float score; stu1=9901, “Zhao”, 1980, 10,30, 80, stu2;stu2.birthday.y=1979;stu2.birthday.y=1;stu2.birthday.y=20;ymdscorebirthdaynamenum9.4.2 单向链表struct student long int num; float score; struct student *next;结构的递归定义head990180990290990375NULL1、动态内存分配函数(1) void *malloc(unsigned size)功能:在

14、内存的动态存贮区中分配一块长度为size的连续空间。返回值:指针,存放被分配内存的起始地址。若未申请到空间,则返回 NULL( 0 )。 void *:指向任何类型的数据,在使用时,要进行强制类型转换。例如:(int *) malloc(sizeof(int)(struct student *) malloc(sizeof(struct student)int *p1,*p2,a;P1=&a;*p1=345;P2=(int *)malloc(n*sizeof(int);*p2=123; p2+; *p2=456; p2+; *p2=999;(2) void free(void *ptr)功能:

15、释放由malloc()申请的动态内存空间,ptr存放该空间的首地址。返回值:无。p=(struct student *) malloc(sizeof(struct student);free(p);2、建立链表编写一个函数,要求用单向链表建立学生档案,从键盘输入数据,如果学号为0,输入结束,并返回链表的头指针。struct student long int num; float score; struct student *next;;struct student *head, *tail, *p;head=tail=NULL;struct student *head, *tail, *p;h

16、ead=tail=NULL;size=sizeof(struct student);p = (struct student *) malloc(size);pnumscorenextheadtailptailhead=tail=NULL;input num, scorewhile 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

17、.hstruct student int num;float score;struct student *next;struct student *creat( );main( )struct student *head;head = creat( );struct student *creat() struct student *head, *tail, *p; float score; int num, size = sizeof(struct student); head = tail = NULL; scanf(%d %f, &num, &score); while (num) p =

18、 (struct student *)malloc(size); p-num = num; p-score = score; p-next = NULL; if(head = NULL) head = p; else tail-next = p; tail = p; scanf(%d %f, &num, &score); return head; 3、遍历链表ptr-numptr-scorehead990180990290990375NULLptrptrfor(ptr=head; ptr!=NULL; ptr=ptr-next) printf(“%ld, %f”, ptr-num, ptr-s

19、core);ptr=ptr-next# include stdio.hstruct 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 = head; ptr; ptr = ptr-next) printf( %d %fn, ptr-num, ptr-score);4、对链表的删除操作ptr2=ptr1-nextptr1-next=ptr2-nextheadptr1ptr2headptr1free(ptr2)ptr2=ptr1-nextptr24、对链表的插入操作s-next = ptr-nextptr-next = s先连后断h

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论