版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、第九章 結(jié)構,結(jié)構 結(jié)構數(shù)組 結(jié)構指針 鏈表 位運算 自定義類型,學號,姓名,性別,出生地,出生年,出生月,出生日,數(shù)學,物理,程序設計,結(jié)構:同一個數(shù)據(jù)項的若干成分構成的一個整體。 例如:學生檔案,每個學生有學號、姓名、性別、出生地、出生年月、學業(yè)成績等。,9.1 結(jié)構,9.1.1 結(jié)構的定義 struct student long int num; char name20; float score; ; 定義一個結(jié)構類型: struct student,9.1.2 結(jié)構變量的定義,1、先定義結(jié)構類型,再定義變量 struct student long int num; char name2
2、0; float score; ; struct student stu1, stu2;,2、定義結(jié)構類型的同時定義變量 struct student long int num; char name20; float score; stu1, stu2;,3、不指定類型名,只定義變量 struct long int num; char name20; float score; stu1, stu2;,9.1.3 結(jié)構變量的初始化,只有全局變量或靜態(tài)變量才能初始化。 static struct student stu2=200012, “Li”, 94;,struct student long
3、num; char name20; float score; stu1=200011, Zhang, 85;,9.1.4 結(jié)構變量的使用,1、結(jié)構類型變量的整體引用 (1) 不能整體輸入輸出,但相同類型的變量可以互相賦值 printf(%ld%s%f, stu1); 非法 stu2=stu1; 合法 (2) 可以引用結(jié)構體變量的地址 printf(%x, 輸出stu1的首地址,2、結(jié)構變量中分量的引用 struct student long int num; char name20; float score; stu1, stu2;,(1) 結(jié)構變量.分量 stu1.num = 9901; p
4、rintf(%s, );,(2) 結(jié)構變量中的分量可以依據(jù)它的類型進行各種運算 x = stu1.score; strcpy(, “Wang”); (3) 可以引用結(jié)構變量中的分量的地址 scanf(%ld, ,9.2 結(jié)構數(shù)組,一個結(jié)構變量只能存放一個學生的資料。 若班上有20個學生,需要用結(jié)構數(shù)組。 即,數(shù)組中的每個元素都是結(jié)構類型。 9.2.1 定義 struct student long int num; char name20; float score; stu20;,9.2.2 初始化 struct student long int num; c
5、har name20; float score; stu20=200011,”Zhang”,85, 200012,”Li”,90;,9.2.3 引用 struct student long int num; char name20; float score; stu20; stu0.num stu0.score,程序舉例,例1、輸入某班30位學生的姓名及數(shù)學、英語成績,計算并輸出每位學生的平均分。 struct student char name10; int math, eng; float aver; ;,void main( ) struct student s30;
6、 int i; for(i=0; i30; i+) scanf(%s%d%d, , ,輸入某班30位學生的姓名及數(shù)學、英語成績,計算并輸出每門課程的平均分。 void main( ) struct student s30; int i; float aver_m=0, aver_e=0;,例2,for(i=0; i30; i+) scanf(%s%d%d, , ,輸入30位學生的姓名及數(shù)學、英語成績,輸出平均分最高的學生的姓名及其數(shù)學和英語成績。 struct student char name10; int math, eng; float aver; ;,例3,
7、void main( ) struct student s30; int i, sub; for(i=0; i30; i+) scanf(%s%d%d, , si.aver = (si.math+si.eng)/2.0 ,sub=0; for( i=1; i ssub.aver ) sub = k; printf(%s%d%dn, , ssub.math, ssub.eng); ,9.3 結(jié)構指針,9.3.1 定義 struct student long int num; char name20; float score; ;,struct student st
8、u1, *ptr; ptr = ,9.3.2 結(jié)構指針的使用,struct student stu1, *ptr = ,(2) - (*ptr).num = 200011; ptr-num = 200011; ptr-score = 85; strcpy(ptr-name,”Zhang”); 當ptr = int m; int d; ,struct student long int num; char name20; struct day birthday; float score; ,struct day int y; int m; int d; ; struct student long
9、int num; char name20; struct day birthday; float score; stu1, stu2;,或: struct student long int num; char name20; struct int y, m, d; birthday; float score; stu1, stu2;,struct student long int num; char name20; struct int y, m, d; birthday; float score; stu1=9901, “Zhao”, 1980, 10,30, 80, stu2; stu2.
10、birthday.y=1979; stu2.birthday.y=1; stu2.birthday.y=20;,9.4.2 單向鏈表,struct student long int num; float score; struct student *next; 結(jié)構的遞歸定義,1、動態(tài)內(nèi)存分配函數(shù),(1) void *malloc(unsigned size) 功能:在內(nèi)存的動態(tài)存貯區(qū)中分配一塊長度為size的連續(xù)空間。 返回值:指針,存放被分配內(nèi)存的起始地址。若未申請到空間,則返回 NULL( 0 )。 void *:指向任何類型的數(shù)據(jù),在使用時,要進行強制類型轉(zhuǎn)換。 例如:(int *)
11、malloc(sizeof(int) (struct student *) malloc(sizeof(struct student),int *p1,*p2,a; P1=,(2) void free(void *ptr) 功能:釋放由malloc()申請的動態(tài)內(nèi)存空間,ptr存放該空間的首地址。 返回值:無。 p=(struct student *) malloc(sizeof(struct student); free(p);,2、建立鏈表,編寫一個函數(shù),要求用單向鏈表建立學生檔案,從鍵盤輸入數(shù)據(jù),如果學號為0,輸入結(jié)束,并返回鏈表的頭指針。 struct student long int
12、 num; float score; struct student *next; ; struct student *head, *tail, *p; head=tail=NULL;,struct student *head, *tail, *p; head=tail=NULL; size=sizeof(struct student); p = (struct student *) malloc(size);,p,head,tail,tail,head=tail=NULL; input num, score while num!=0 p= (struct student *) malloc(s
13、izeof(size) p-num=num, p-score=score, p-next=NULL y head=NULL n head=p tail-next=p tail=p input num, score,# include stdio.h struct student int num; float score; struct student *next; ; struct student *creat( ); main( ) struct student *head; head = creat( ); ,struct student *creat() struct student *
14、head, *tail, *p; float score; int num, size = sizeof(struct student); head = tail = NULL; scanf(%d %f, ,3、遍歷鏈表,ptr-num ptr-score,for(ptr=head; ptr!=NULL; ptr=ptr-next) printf(“%ld, %f”, ptr-num, ptr-score);,ptr=ptr-next,# include stdio.h struct student int num; float score; struct student *next; ; s
15、truct student *creat( ); void print(struct student *head) main( ) struct student *head; head = creat(); print(head); ,void print(struct student *head) struct student *ptr; if(head = NULL) printf(n No listn); return; printf(n listn); for (ptr = head; ptr; ptr = ptr-next) printf( %d %fn, ptr-num, ptr-score); ,4、對鏈表的刪除操作,ptr2=ptr1-next ptr1-next=ptr2-next,free(ptr2) ptr2=ptr1-next,4、對鏈表的插入操作,s-next = ptr-next ptr-next = s 先連后斷,9.5 位運算,9.5.1 位運算符 1、位邏
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年能源管理與企業(yè)節(jié)能策略
- 第2單元雙休必讀經(jīng)典書
- 2026年劇本殺運營公司質(zhì)量問題整改管理制度
- 2026年劇本殺運營公司員工跨部門培訓管理制度
- 生成式人工智能在初中歷史課堂個性化教學中的應用探討教學研究課題報告
- 高中生對基因編輯技術科學證據(jù)的批判性思維訓練課題報告教學研究課題報告
- 護理部護理工作信息化建設匯報
- 健全消防安全制度
- 體育消費券制度
- 會員管理制度
- 220kv輸變電工程項目實施方案
- 中國近代學前教育
- 海上風電機組基礎結(jié)構-第三章課件
- 家庭教育講師培訓方法研究
- 《英語面試指南》招聘求職必備手冊
- DB12-T 601-2022 城市軌道交通運營服務規(guī)范
- 白油化學品安全技術說明書
- 砼澆筑工程技術交底
- 重慶園林工程師園林理論
- CTM-DI(B)磁力儀使用說明書
- GB/T 32545-2016鐵礦石產(chǎn)品等級的劃分
評論
0/150
提交評論