C语言英文课件Chapter.ppt
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 initialization10.3 structure array10.4 structure pointer10.5 link dispoing-application of structure pointer10.6 union and enumeration10.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 emergingSTUDENT SCORES MANAGE TABLE CANOT DEAL WITH 2D ARRAYS,DIFFERENT TYEPES!,二、structure type declarationCommon 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 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 declarated.eg.the type”birthday”in the eg.Of structure type std_info is a definited structure type-date,三、STRUCTURE VARIABLES DECLARATIONStructure type(user definition)Standart type(system definition)1、indirect declaration first declare the structure type,then the structure variables,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 directly;,(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 be 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 studentstruct date/*data structure type made of year,month and day*/int year;int month;int day;struct std_info/*student information 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 the 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,stu.name);printf(Sex:%cn,stu.sex);printf(Birthday:%d-%d-%dn,);,result:No:1002Name:Zhang SanSex:MBirthday:1980-9-20,1、INITIALIZATIONS OF STUCTURE variable structtype struct_variable=initial value listeg,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 structure variable member operator“.”while citing the member in structure,use operator”.”;citing format:structure variable.member eg,stu.num;stu.name,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.member.child-member.Lowest memberEg.: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:include all members of structure例10.3 use structure array to store and print the student scores manage table below.,stu0stu1stu2stu3,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=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 details of 4 students*/for(i=0;i4;i+)printf(%-5d,stui.num);printf(%-12s,stui.name);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 M 20 77 86,eg10.4use arrays of structure as funtion parameters#includestruct.hvoid 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,stui.name);printf(%-4c,stui.sex);printf(%-4d,stui.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(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 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.Name Sex Age soc1 sco2n);for(i=0;i4;i+)printf(%-5d,stui.num);printf(%-12s,stui.name);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 variablevoid 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 usefor(i=0;i4;i+,p+),then how to modify?,eg10.5 use pointer to call every member of the structure variablevoid 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(%-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 namethoughts:how to input every member of stui by keyboard?,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 points 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、mallocapply 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 every data area by ordereg: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*/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;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 0data 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,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 linklist(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 linklisteg10.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: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:after-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-null 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(insert:i,x=);scanf(%d%d,10.5.4 the delete operation to link listeg10.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 information2、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*/head=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 print(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();break;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,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;else 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,10.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: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 member5Citing 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 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 member.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 enumeration1Enumeration definition enum weekdays Sun,Mon,Tue,Wed,Thu,Fri,Sat;enum_name enum_element/enum_cv2Enumerated variable definition(1)indirect definition enum weekdays workday;(2)direct definition enum weekdays Sun,Mon,Tue,Wed,Thu,Fri,Sat workday;3demonstration(1)uesed for limited data.eg:1 week 7 days,1years 12months,5colors。(2)pick the element up in list as enumeration,which is explained by program.eg,not because write as“sun”,that stands for“Sunday”.actually,enum element can be standed by any name.,(3)Enum element is limited as literal start by 0 so comparation is valid.Comparation rules:the bigger the number is,the bigger it is.eg,in last instance,Sun=0、Mon=1、Sat=6,so MonSun、Sat is the biggest。(4)enum_element is changable.while definiting,it is specified by program.eg,definite enum weekdays Sun=7,Mon1,Tue,Wed,Thu,Fri,Sat;so Sun=7,Mon=,begin with Tue=2,increase by 1。,10.7 Typedef,C provides a facility called typedef for creating new data type names.For example,the declaration typedef float REAL;/*creat new datatype name real for float*/REAL x,y;/*definite the type of x,y as real*/*it means the type of x and y is float*/,eg10.10 definite the alias of structure struct date as DATE。struct date int year,month,day;typedef struct date DATE;DATA t,*p;equal to:typedef struct date int year,month,day;DATE;DATA t,*p;,demonstration:1、to definite an alias to a exist variable using typedef wont creat a new type.2、difference between typedef and#define:(1)typedef is disposing when compiling;(2)#define is dispoing when preprocessing,as a counterpart,