版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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
2、.5 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
3、DEAL 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
4、rules to 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
5、 been declarated. 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
6、,The same 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 direc
7、tly;,(1) struct 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 b
8、e same as other 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 informati
9、on structure type*/ 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 th
10、e student details #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
11、 San Sex: M Birthday: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、cit
12、ing rules of structure 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 form
13、at: structure variable. 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 structur
14、e. Every array element:include 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 scor
15、e2; ; struct stdent stu4=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;,Store in file“struct.h”,.,#includestruct.h” main() int i; /*print list head, “stands for one backspace*/ printf(No.NameSexAgesoc1 sco2n); /*output basic
16、 details of 4 students*/ for(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 F
17、ong M 21 86 90 1004 Li Xiaopen 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
18、.age); printf(%-5.1f,stui.score1); 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(
19、stu); printf(list after sorted:n); 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
20、 Wang Li F 20 98 96 1001 Zhang Xin M 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
21、. 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); printf(%-5.1fn,stui.score2); ,modify! Use pointer which point to structure as formal parameter,.,eg10.5 use pointer to call member of
22、 the structure variable void print_stulist1(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
23、 for(i=0;i4;i+,p+), then how to modify?,.,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).a
24、ge); printf(%-5.1f,(*p).score1); printf(%-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?
25、,.,10.5 linklist disposingapplication of structure 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 poi
26、nts to structure type 2、structure type including 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、 mallocappl
27、y memory storage void *malloc(unsigned int size) 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 e
28、very data area by order eg:input 1 3 2 5 0,create 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*
29、/ last-next=p;,/*4.last move backwards*/ last=p;,Repeat 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;
30、while(1) scanf(%d,#define NULL 0 #define LEN sizeof(struct 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
31、 of the linklist are:1 3 2 5,Q:make 2 functions for creating 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
32、, ,2、output linklist 1、install linklist 四、realizing by function:,.,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 linkli
33、st(end by =0):n); head=creat(); printf(the data of linklist: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. a
34、nalysis : comon situation pre-inserted: after-inserted: standart 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: afte
35、r-inserted : 3、i out-of -range(ithe number of node) disposing manner:(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-n
36、ull linklist*/ s-next=head; head=s; /*2、new node as new head node 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(
37、insert: i,x=); scanf(%d%d, ,.,10.5.4 the delete operation to link list 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
38、head=NULL error information 2、delete the head node pre-deleted: after-deleted: 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*/ he
39、ad=head-next; else q=head;p=q-next; for(; p-num!=x ,.,10.5.5 colligated example: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 p
40、rint(struct node *head); struct node *insert_sortedlist(struct node *head, int 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(); br
41、eak; case 2: printf(input a number to insert. x=); scanf(%d, ,.,/* =menu = */ char 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,
42、1,2,3,4):); ch=getchar(); return(ch); ,.,/* = insert = */ struct node *insert_sortedlist(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; e
43、lse q=head;p=q-next; /*lookup the place to insert*/ for(; p!=NULL ,.,Pre-inserted: After-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,1
44、0.6.1 union 1conception several variables of different type occupy the same segment of storage. 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
45、: union data un1,un2,un3; (2)direct definitondefinite variables when definiting type eg. union 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,.,5characteri
46、stic (1)System uses the overlaying techonology to realize memory sharing of the sharing variablesmembers,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 memb
47、er. eg,un1un1.iun1.chun1.f (3)Cannot initialize sharing variable(note:structure variable is 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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 平版印刷员岗前保密意识考核试卷含答案
- 临床试剂工安全知识评优考核试卷含答案
- 钟表部件组件装配工风险评估与管理能力考核试卷含答案
- 机制地毯挡车工安全理论测试考核试卷含答案
- 梳理缝编非织造布制作工安全知识强化考核试卷含答案
- 移栽机操作工岗前常识考核试卷含答案
- 2024年甘肃政法大学辅导员考试笔试真题汇编附答案
- 2024年隆化县幼儿园教师招教考试备考题库附答案
- 2025年三亚辅警协警招聘考试真题附答案
- 2025年电信网络运行维护操作手册
- 《机器学习》课件-第7章 神经网络与深度学习
- 2025年6月浙江省高考物理试卷真题(含答案解析)
- 2025-2030中国智能家居系统配置服务技术人才缺口评估报告
- 护士肺功能室进修汇报
- 物业工程维修培训内容
- 神经外科规培结业考试题库及答案
- 静脉输液十二种并发症及防治措施
- 广东省领航高中联盟2024-2025学年高一下学期第一次联合考试语文试卷(含答案)
- 肺栓塞的急救处理
- T/CCAS 007-2019水泥产能核定标准
- 胰腺炎中医护理方案
评论
0/150
提交评论