浙江大学C颜晖原版C9ppt课件_第1页
浙江大学C颜晖原版C9ppt课件_第2页
浙江大学C颜晖原版C9ppt课件_第3页
浙江大学C颜晖原版C9ppt课件_第4页
浙江大学C颜晖原版C9ppt课件_第5页
已阅读5页,还剩41页未读 继续免费阅读

下载本文档

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

文档简介

第九章结构,结构结构数组结构指针链表位运算自定义类型,学号,姓名,性别,出生地,出生年,出生月,出生日,数学,物理,程序设计,结构:同一个数据项的若干成分构成的一个整体。例如:学生档案,每个学生有学号、姓名、性别、出生地、出生年月、学业成绩等。,9.1结构,9.1.1结构的定义structstudentlongintnum;charname20;floatscore;定义一个结构类型:structstudent,9.1.2结构变量的定义,1、先定义结构类型,再定义变量structstudentlongintnum;charname20;floatscore;structstudentstu1,stu2;,2、定义结构类型的同时定义变量structstudentlongintnum;charname20;floatscore;stu1,stu2;,3、不指定类型名,只定义变量structlongintnum;charname20;floatscore;stu1,stu2;,9.1.3结构变量的初始化,只有全局变量或静态变量才能初始化。staticstructstudentstu2=200012,“Li”,94;,structstudentlongnum;charname20;floatscore;stu1=200011,Zhang,85;,9.1.4结构变量的使用,1、结构类型变量的整体引用(1)不能整体输入输出,但相同类型的变量可以互相赋值printf(%ld%s%f,stu1);非法stu2=stu1;合法(2)可以引用结构体变量的地址printf(%x,输出stu1的首地址,2、结构变量中分量的引用structstudentlongintnum;charname20;floatscore;stu1,stu2;,(1)结构变量.分量stu1.num=9901;printf(%s,);,(2)结构变量中的分量可以依据它的类型进行各种运算x=stu1.score;strcpy(,“Wang”);(3)可以引用结构变量中的分量的地址scanf(%ld,9.2结构数组,一个结构变量只能存放一个学生的资料。若班上有20个学生,需要用结构数组。即,数组中的每个元素都是结构类型。9.2.1定义structstudentlongintnum;charname20;floatscore;stu20;,9.2.2初始化structstudentlongintnum;charname20;floatscore;stu20=200011,”Zhang”,85,200012,”Li”,90;,9.2.3引用structstudentlongintnum;charname20;floatscore;stu20;stu0.score,程序举例,例1、输入某班30位学生的姓名及数学、英语成绩,计算并输出每位学生的平均分。structstudentcharname10;intmath,eng;floataver;,voidmain()structstudents30;inti;for(i=0;i30;i+)scanf(%s%d%d,,输入某班30位学生的姓名及数学、英语成绩,计算并输出每门课程的平均分。voidmain()structstudents30;inti;floataver_m=0,aver_e=0;,例2,for(i=0;inum=200011;ptr-score=85;strcpy(ptr-name,”Zhang”);当ptr=intm;intd;,structstudentlongintnum;charname20;structdaybirthday;floatscore;,structdayinty;intm;intd;structstudentlongintnum;charname20;structdaybirthday;floatscore;stu1,stu2;,或:structstudentlongintnum;charname20;structinty,m,d;birthday;floatscore;stu1,stu2;,structstudentlongintnum;charname20;structinty,m,d;birthday;floatscore;stu1=9901,“Zhao”,1980,10,30,80,stu2;stu2.birthday.y=1979;stu2.birthday.y=1;stu2.birthday.y=20;,9.4.2单向链表,structstudentlongintnum;floatscore;structstudent*next;结构的递归定义,1、动态内存分配函数,(1)void*malloc(unsignedsize)功能:在内存的动态存贮区中分配一块长度为size的连续空间。返回值:指针,存放被分配内存的起始地址。若未申请到空间,则返回NULL(0)。void*:指向任何类型的数据,在使用时,要进行强制类型转换。例如:(int*)malloc(sizeof(int)(structstudent*)malloc(sizeof(structstudent),(2)voidfree(void*ptr)功能:释放由malloc()申请的动态内存空间,ptr存放该空间的首地址。返回值:无。p=(structstudent*)malloc(sizeof(structstudent);free(p);,2、建立链表,编写一个函数,要求用单向链表建立学生档案,从键盘输入数据,如果学号为0,输入结束,并返回链表的头指针。structstudentlongintnum;floatscore;structstudent*next;;structstudent*head,*tail,*p;head=tail=NULL;,structstudent*head,*tail,*p;head=tail=NULL;size=sizeof(structstudent);p=(structstudent*)malloc(size);,p,head,tail,tail,head=tail=NULL;inputnum,scorewhilenum!=0p=(structstudent*)malloc(sizeof(size)p-num=num,p-score=score,p-next=NULLyhead=NULLnhead=ptail-next=ptail=pinputnum,score,#includestdio.hstructstudentintnum;floatscore;structstudent*next;structstudent*creat();main()structstudent*head;head=creat();,structstudent*creat()structstudent*head,*tail,*p;floatscore;intnum,size=sizeof(structstudent);head=tail=NULL;scanf(%d%f,3、遍历链表,ptr-numptr-score,for(ptr=head;ptr!=NULL;ptr=ptr-next)printf(“%ld,%f”,ptr-num,ptr-score);,ptr=ptr-next,#includestdio.hstructstudentintnum;floatscore;structstudent*next;structstudent*creat();voidprint(structstudent*head)main()structstudent*head;head=creat();print(head);,voidprint(structstudent*head)structstudent*ptr;if(head=NULL)printf(nNolistn);return;printf(nlistn);for(ptr=head;ptr;ptr=ptr-next)printf(%d%fn,ptr-num,ptr-score);,4、对链表的删除操作,ptr2=ptr1-nextptr1-next=ptr2-next,free(ptr2)ptr2=ptr1-next,4、对链表的插入操作,s-next=ptr-nextptr-next=s先连后断,9.5位运算,9.5.1位运算符1、位逻辑运算符按位取反(与!

温馨提示

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

评论

0/150

提交评论