版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025北京市茶葉購買合同范本
- 2025合法的勞務(wù)聘用合同樣本
- 沙盤定制合同
- 課題申報參考:老齡化應(yīng)對政策、生育支持政策、健康社會研究
- 課題申報參考:考慮生態(tài)補(bǔ)償?shù)目缃绛h(huán)境污染協(xié)同治理績效測度與政策優(yōu)化研究
- 2024年工業(yè)涂料水性色漿項目資金申請報告代可行性研究報告
- 跨區(qū)域、跨領(lǐng)域的學(xué)校合作項目策劃實踐
- 科技在寵物健康監(jiān)測中的應(yīng)用
- 2023年柴油資金籌措計劃書
- 完整版秸稈炭化成型綜合利用項目可行性研究報告
- 油氣行業(yè)人才需求預(yù)測-洞察分析
- 《數(shù)據(jù)采集技術(shù)》課件-Scrapy 框架的基本操作
- 2025年河北省單招語文模擬測試二(原卷版)
- 高一化學(xué)《活潑的金屬單質(zhì)-鈉》分層練習(xí)含答案解析
- DB34∕T 4010-2021 水利工程外觀質(zhì)量評定規(guī)程
- 2024年內(nèi)蒙古中考英語試卷五套合卷附答案
- 2024年電工(高級)證考試題庫及答案
- 農(nóng)產(chǎn)品質(zhì)量評估與分級
- 儲能電站火災(zāi)應(yīng)急預(yù)案演練
- 人教版(新插圖)二年級下冊數(shù)學(xué) 第4課時用“進(jìn)一法”和“去尾法”解決簡單的實際問題 教學(xué)課件
評論
0/150
提交評論