




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、chapter 10 structures and unions,a structure is a collection of one or more variables,possibly of different types,which are treated as a unit instead of as separate entities. 10.1 structure definition 10.2 structure variables citing and initialization 10.3 structure array 10.4 structure pointer 10.5
2、 link dispoing- application of structure pointer 10.6 union and enumeration 10.7 typedef,10.1 structure type and declaration of structure variables,the structure type ,in c language ,is the counterpart as “record” in other advanced language. 一、question emerging student scores manage table canot deal
3、 with 2d arrays,different tyepes!,二、 structure type declaration common usage: struct structure typename datatype member1; datatype member2; datatype membern; ;,struct student int num; char name20; char sex; int age; float score1; float score2; ;,type name,struct student,demonstration: 1、the rules to
4、 name“structure type name”and “member name” ,are same with the variables. 2、the definitions of variables can be seperated or in a same line like the instance below eg. struct date int year, month, day; ; 3、 the definitions of variables could be basic types or another structure type which has been de
5、clarated. eg.the type”birthday” in the eg. of structure type std_info is a definited structure type -date,三、 structure variables declaration structure type(user definition) standart type(system definition) 1、indirect declaration first declare the structure type,then the structure variables,the same
6、usage,struct student int num; char name20; char sex; int age; float score1; float score2; ;,struct student stu1, stu2;,num name sex age score1 score2,stu1,2、 direct declaration (1)definite the structure variable,when definiting the structure type; (2)definite the structure variable directly;,(1) str
7、uct student int num; char name20; float score2; stu1, stu2;,(2) struct int num; char name20; float score2; stu1, stu2;,3、demonstration (1)differ structure type and variable,which are completely different conception ,just as type int and variable of int (2)member names in structure ,may be same as ot
8、her variables in program, stand for different objects,eg10.1 creat a structure reffering to basic information of a student to store the details of a student struct date /*data structure type made of year,month and day*/ int year; int month; int day; ; struct std_info/*student information structure t
9、ype*/ int num; /*made of name,sex,birthday,etc*/ char name12; char sex; struct date birthday; float score1,score2; ;,10.2 citing and initializations of stucture variables,例10.2 make use of the former stucture type struct std_info,to definite a structure type stu to store and print the student detail
10、s #include“struct.h”struct std_info stu = 1002,”zhang san”,m, 1980,9,20,98.2,86.5;main() printf(no: %dn,stu.num); printf(name: %sn,); printf(sex: %cn,stu.sex); printf(birthday: %d-%d-%dn,stu.birthday.year, stu.birthday.month, stu.birthday.day); ,result: no: 1002 name: zhang san sex: m birthd
11、ay:1980-9-20,1、 initializations of stucture variable structtype struct_variable=initial value list eg,struct std_info stu = “000102”,”zhang san”,m, 1980,9,20; note:data type of initial value should be consistent with the members in structure variable,or there will be error. 2、citing rules of structu
12、re variable member operator“.” while citing the member in structure ,use operator”.” ; citing format:structure variable.member eg,stu.num; ,etc。,note:if a member itself is a structure type,a multilevel member operation is needed to cite the lowest member. expanding format: structure variable
13、. member.child-member. lowest member eg.: stu.birthday.year stu.birthday.month stu.birthday.day betaking the lowest member is equal to common variable of the same type.,10.3 structure arrays,structure arrays:an array made by structure type data which is as element in structure. every array element:i
14、nclude all members of structure 例10.3 use structure array to store and print the student scores manage table below.,stu0 stu1 stu2 stu3,struct student /*lay the definition of structure type outside function*/ int num; char name12; char sex; int age; float score1; float score2; ; struct stdent stu4=1
15、001,”zhang xin”,m,20, 87, 91, 1002,”wang li”, f,20, 98, 96, 1003,”chen fong”,m,21,86, 90, 1004,”li xiaopen”, m,20,77, 86;,store in file“struct.h”,#includestruct.h” main() int i; /*print list head, “stands for one backspace*/ printf(no.namesexagesoc1 sco2n); /*output basic details of 4 students*/ for
16、(i=0; i4; i+) printf(%-5d,stui.num); printf(%-12s,); printf(%-4c,stui.sex); printf(%-4d,stui.age); printf(%-5.1f,stui.score1); printf(%-5.1fn,stui.score2); ,running result: no. name sex age soc1 sco2 1001 zhang xin m 20 87 91 1002 wang li f 20 98 96 1003 chen fong m 21 86 90 1004 li xiaopen
17、 m 20 77 86,eg10.4use arrays of structure as funtion parameters #includestruct.h void print_stulist(struct student stu) int i; printf(no. name sex age soc1 sco2n); for(i=0; i4; i+) printf(%-5d,stui.num); printf(%-12s,); printf(%-4c,stui.sex); printf(%-4d,stui.age); printf(%-5.1f,stui.score1
18、); printf(%-5.1fn,stui.score2); ,use array of structure as formal parameter,void sort(struct student st) int i,j; struct student temp; for(i=0;i4-1;i+) for(j=i+1;j4;j+) if(sti.score1stj.score1) temp=sti; sti=stj; stj=temp; ,main() clrscr(); print_stulist(stu); sort(stu); printf(list after sorted:n);
19、 print_stulist1(stu); getch(); ,structure array as formal parameter,running result: no. name sex age soc1 sco2 1001 zhang xin m 20 87 91 1002 wang li f 20 98 96 1003 chen fong m 21 86 90 1004 li xiaopen m 20 77 86 list after sorted: no. name sex age soc1 sco2 1002 wang li f 20 98 96 1001 zhang xin m
20、 20 87 91 1003 chen fong m 21 86 90 1004 li xiaopen m 20 77 86,10.4 pointer pointed to data type of structure,pointer of structure variable:structure variable exists in the starting position of the storage. void print_stulist(struct student stu) int i; printf(no. name sex age soc1 sco2n); for(i=0; i
21、4; i+) printf(%-5d,stui.num); printf(%-12s,); printf(%-4c,stui.sex); printf(%-4d,stui.age); printf(%-5.1f,stui.score1); printf(%-5.1fn,stui.score2); ,modify! use pointer which point to structure as formal parameter,eg10.5 use pointer to call member of the structure variable void print_stuli
22、st1(struct student *p) int i; printf(no. name sex age soc1 sco2n); for(i=0; i4; i+) printf(%-5d,(*(p+i).num); printf(%-12s,(*(p+i).name); printf(%-4c,(*(p+i).sex); printf(%-4d,(*(p+i).age); printf(%-5.1f,(*(p+i).score1); printf(%-5.1fn,(*(p+i).score2); ,if use for(i=0;i4;i+,p+), then how to modify?,
23、eg10.5 use pointer to call every member of the structure variable void print_stulist1(struct student *p) int i; printf(no. name sex age soc1 sco2n); for(i=0; i4; i+,p+) printf(%-5d,(*p).num); printf(%-12s,(*p).name); printf(%-4c,(*p).sex); printf(%-4d,(*p).age); printf(%-5.1f,(*p).score1); printf(%-
24、5.1fn,(*p).score2); ,if the pointer has been pointing to structure variable v,then when: struct student var, *p= the 3 forms below are equal: (1) var.member name (2) (*p).member name (3) p-member name thoughts:how to input every member of stui by keyboard?,10.5 linklist disposingapplication of struc
25、ture pointer,10.5.1 linklist overview 一dynamic data structure element number fluctuates by program running. cf. data structure of static state(eg.array),confirm ing its size when definiting. 二 dynamic data structure implement in virtue of : 1、pointer points to structure type 2、structure type includi
26、ng pointer data linklist:a simple dynamic data structure. head,10.5.2 creating linklist 一、description of linklist node structureeg. struct node int num; /*data area*/ struct node *next; /*pointer area*/ ; 二、 function of disposing linklist 1、 mallocapply memory storage void *malloc(unsigned int size)
27、 eg. struct node *p; p=(struct node*)malloc(sizeof(struct node);,p,2、 freeset storage area free void free(void *p) 三、create linklist 例10.6according to a series of nonnegative integers, input by keyboard ,to create a linklist,and output the value of every data area by order eg:input 1 3 2 5 0,create
28、linklist such as:,/*1.creat new node,pointed by head pointer*/ head=(struct node*)malloc(len); last=head; /*tail pointer points to node of tail*/,/*2.input x;create new node p;store x to num area of node p*/ scanf(%d,/*3.add p to the linklist tail*/ last-next=p;,/*4.last move backwards*/ last=p;,rep
29、eat 24,when x0,/*set pointer area null which was pointed by tail node pointer,head pointer move backwards (deleting head node)*/ last-next=null; head=head-next;,main() struct node *head,*p,*last; int x; head=(struct node*)malloc(len); last=head; while(1) scanf(%d,#define null 0 #define len sizeof(st
30、ruct node) struct node int num; struct node *next; ;,p=head; head=head-next; free(p);,/*output linklist*/ printf(“data of the linklist are:”) p=head; while(p!=null) printf(%5d,p-num); p=p-next; printf(n); getch(); running result: 1 3 2 5 0 data of the linklist are:1 3 2 5,q:make 2 functions for crea
31、ting linklist and output linklist:,1.struct node *creat(void) 2.void print(struct node *head),struct node *creat(void)/*create linklist function*/ struct node *head,*p,*last; int x; head=(struct node*)malloc(len); last=head; while(1) scanf(%d, ,2、output linklist 1、install linklist 四、realizing by fun
32、ction:,void print(struct node *head)/*output linklist function*/ struct node *p; p=head; while(p!=null) /*traversing the linklist*/ printf(%5d,p-num); p=p-next; printf(n); getch(); main() struct node *head; printf(input data to creat linklist(end by =0):n); head=creat(); printf(the data of linklist:
33、n); print(head); ,q:computing the maxinum and mininum of the data node,10.5.3 insert operation to linklist eg10.8 create a function insert() to realize inserting a new node which data is x after to the ist node which is pointed by head. analysis : comon situation pre-inserted: after-inserted: standa
34、rt consideration: along the pointer area of the node to find the ist node,finally insert new node after the ist node by linklist head pointer,special situation: 1、null list pre=inserted:head=null after-inserted: 2、i=0 pre-inserted: after-inserted : 3、i out-of -range(ithe number of node) disposing ma
35、nner:(1)error (2)insert to the tail,struct node *insert(struct node *head, int i,int x) struct node *s,*p; s=(struct node*)malloc(len); s-num=x; if(head=null) head=s; s-next=null; /*1、insert to the null linklist*/ else if(i=0) /*non-null linklist*/ s-next=head; head=s; /*2、new node as new head node
36、of linklist*/ else p=head; /*lookup the ist node(pointed by p)*/ for(; p!=null ,main() struct node *head; int i,x; printf(input data to creat linklist(end by =0):n); head=creat(); printf(the data of linklist:n); print(head); printf(insert: i,x=); scanf(%d%d, ,10.5.4 the delete operation to link list
37、 eg10.8 design a function delete() ,to delete the node whose data is x in the link list。 common situation: pre-deleted: after-deleted: q=head; p=q-next;/*lookup the node whose value is x*/ for(; p-num!=x ,espescial: 1、null list head=null error information 2、delete the head node pre-deleted: after-de
38、leted: 3、no such node error information,struct node *delete_list(struct node *head,int x) struct node *p,*q; if(head=null) /*1、blank list*/ printf(error! the list is empty.n); else if(head-num=x) /*2、delete the first node*/ head=head-next; else q=head;p=q-next; for(; p-num!=x ,10.5.5 colligated exam
39、ple:menu program designing,/* = sorted link_list operater = */ /* designed by sun qiaoping */ #include stdio.h; #define null 0 #define len sizeof(struct node) struct node int num; struct node *next; ; char menu(void); void print(struct node *head); struct node *insert_sortedlist(struct node *head, i
40、nt x); struct node *creat_sortedlist(void); struct node *delete_list(struct node *head,int x);,main() struct node *head=null; int i,x,flag=1; char ch; do ch=menu(); clrscr(); switch (ch) case 1: head=creat_sortedlist(); break; case 2: printf(input a number to insert. x=); scanf(%d, ,/* =menu = */ ch
41、ar menu(void) char ch; clrscr(); printf( menu n); /*puts()*/ printf( =n); printf( 1. creat n); printf( 2. insert n); printf( 3. delete n); printf( 4. print n); printf( 0. exit n); printf( =n); printf(input your choice(0,1,2,3,4):); ch=getchar(); return(ch); ,/* = insert = */ struct node *insert_sort
42、edlist(struct node *head, int x) struct node *s,*p,*q; s=(struct node*)malloc(len); s-num=x; if(head=null) /*insert to a blank list*/ head=s; s-next=null; else if(xnum) /*insert to head of list*/ s-next=head; head=s; else q=head;p=q-next; /*lookup the place to insert*/ for(; p!=null ,pre-inserted: a
43、fter-inserted:,/* = creat = */ struct node *creat_sortedlist(void) struct node *head; int x; printf(input data to creat linklist(end by =0):n); head=null; while(1) scanf(%d, ,10.6 introduction to union and enum,10.6.1 union 1conception several variables of different type occupy the same segment of s
44、torage. 2definition of union common situation:union u_name member list ; eg. union data int i; char ch; float f; ;,same as definition of structure type,3definition of sharing variables (1)indirect definition eg: union data un1,un2,un3; (2)direct definitondefinite variables when definiting type eg. u
45、nion data int i; char ch; float f; un1, un2, un3; 4the storage length occupied by sharing variables:the length of longest member 5citing to sharing variablesciting one by one 例如:un1.i、un1.ch、un1.f,5characteristic (1)system uses the overlaying techonology to realize memory sharing of the sharing vari
46、ablesmembers,so anytime there is only one member value to store and betake. eg,if un1.i=1, un1.ch=c, un1.f=3.14, un1.f is the valid member. (2)the address of sharing variable is the same as that of every member. eg,un1un1.iun1.chun1.f (3)cannot initialize sharing variable(note:structure variable is
47、permitted); nor using sharing variable as function parameter,neither nor returning a sharing data by function,but its permitted to use a pointer pointed to sharing variable. (4)sahring type can be used in structure definition,the reverse is ok!,10.6.2 enumeration 1enumeration definition enum weekdays s
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 小车喷漆活动方案
- 小朋友设计活动方案
- 工信局深化活动方案
- 工会活动咖啡馆活动方案
- 少先队植树活动方案
- 工匠云直播活动方案
- 小班荷花画展活动方案
- 小说学校活动方案
- 干部培训活动策划方案
- 岗位能力建设活动方案
- (2025.06.12)领导干部任前应知应会党内法规和法律知识考试题库(2025年度)
- 2025年高考北京卷化学高考真题+答案(参考版)
- 2025至2030中国汽车滤清器行业市场发展分析及商业模式与投融资报告
- 医用光学技术和仪器使用
- 仗鼓舞比赛活动方案
- 南昌职业大学《影视配音创作》2023-2024学年第二学期期末试卷
- 2024年湖南融通资源循环产业有限公司技能岗位招聘真题
- 销售转正笔试题目及答案
- 树木砍伐合同简单协议书
- 2025年安徽省农业职业技能大赛(水生物病害防治员)备赛试题库(含答案)
- 安全大讲堂教学课件
评论
0/150
提交评论