已閱讀5頁,還剩114頁未讀, 繼續(xù)免費閱讀
版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
第8章結構體和其他構造類型 8 1結構體類型變量的定義和使用8 2結構體和函數(shù)調用8 3提高部分 例題鏈接 例題鏈接 例8 1 例8 2 例8 3 例8 4 例8 5 例8 6 例8 7 例8 8 例8 9 例8 10 例8 11 例8 12 例8 13 8 1結構體類型變量的定義和使用 8 1 1結構體類型的概念和聲明8 1 2結構體類型變量的定義和使用 P250 8 1 1結構體類型的概念和聲明 1 結構體類型的概念把不同類型的數(shù)據(jù)組合在一個類型中 作為一個整體進行處理 結構體類型 P250 要表示日期structdate intyear month day 要表示學生情況structstudent charname 20 number 9 floats1 s2 s3 結構體類型 2 結構體類型的聲明結構體類型聲明 struct結構體名 類型名1成員名表1 類型名2成員名表2 類型名n成員名表n 關鍵字 也稱 域 8 1 2結構體類型變量的定義和使用 例8 1 假設學生基本情況包括學號和兩門課成績 編寫程序計算某學生兩門課的平均成績 并輸出該學生的有關信息 P251 聲明結構體類型structex1 longintnum floats1 floats2 等價于floats1 s2 編程點撥 在主函數(shù)中定義 structex1 類型變量wang和 float 類型變量ave main floatave 0 structex1wang 就象float int型一樣使用 計算平均成績并通過變量wang輸出有關信息 main floatave 0 structex1wang wang num 1000101 wang s1 89 5 wang s2 90 ave wang s1 wang s2 2 printf 10ld 8 1f 8 1f 8 1f n wang num wang s1 wang s2 ave ave占4個字節(jié) wang占多少字節(jié) main floatave 0 structex1wang wang num 1000101 wang s1 89 5 wang s2 90 ave wang s1 wang s2 2 printf 10ld 8 1f 8 1f 8 1f n wang num wang s1 wang s2 ave wang num s1 s2 4字節(jié) 4字節(jié) 4字節(jié) 1000101 89 5 90 0 12字節(jié) main floatave 0 structex1wang wang num 1000101 wang s1 89 5 wang s2 90 ave wang s1 wang s2 2 printf 10ld 8 1f 8 1f 8 1f n wang num wang s1 wang s2 ave wang num s1 s2 1000101 89 5 90 0 等價于 structex1wang 1000101 89 5 90 0 main floatave 0 structex1wang wang num 1000101 wang s1 89 5 wang s2 90 ave wang s1 wang s2 2 printf 10ld 8 1f 8 1f 8 1f n wang num wang s1 wang s2 ave ld f f 例8 2 假設學生基本情況包括學號和多門課成績 計算某學生的平均成績 defineN3structex2 longintnum floats N 要處理的成績較多時 main structex2wang p floatave 0 sum 0 x 0 inti 0 p 等價于wang num printf 學號 ld n p num for i 0 is i printf 8 1f p s i printf n ave sum N printf 平均成績 1f ave 等價于 p num wang num s 0 s 1 s 2 p p 結構體變量名 成員名 指針變量名 成員名指針變量名 成員名 例8 3 設有5名學生 而且每位學生基本情況都包括學號和多門課成績 計算各學生3門課平均成績 defineN3structex2 longintnum floats N main structex2stu 5 floatave 0 sum 0 k 0 inti 0 j 0 for i 0 i 5 i scanf ld for i 0 i 5 i printf 10ld stu i num sum 0 for j 0 j N j printf 8 1f stu i s j sum sum stu i s j ave sum N printf 8 1f n ave 注此行的位置 stu 0 stu 1 stu 2 stu 3 stu 4 num s 0 s 1 s 2 各學生平均分 86 2 67 7 64 2 81 0 91 5 例8 4 假設學生基本情況包括學號和五門課成績 找出某學生五門課成績中的最高成績 structex3 charn 10 floats 5 將學號定義為字符串 main structex3wang 1000101 89 5 90 0 79 0 99 0 69 0 floatmax 0 inti 0 for i 0 i 5 i if max wang s i max wang s i printf s號學生 5 1f wang n max scanf s wang n 功能 gets wang n 功能 strcpy wang n 1000101 功能 puts wang n 功能 能否用wang n 1000101 例8 5 假設學生基本情況包括學號和英語 C程序設計 數(shù)學三門課成績 輸出五個學生中 數(shù)學成績最高的學生所有信息 structex4 charn 10 floats 3 main structex4stu 5 1000101 89 5 90 0 79 0 1000102 68 0 80 0 55 0 1000103 56 5 59 0 77 0 1000104 81 0 88 0 74 0 1000105 90 5 95 0 89 0 inti 0 k 0 for i 1 i 5 i if stu k s 2 stu i s 2 k i printf s號學生 n stu k n for i 0 i 3 i printf 5 1f stu k s i 1000105號學生90 595 089 0 8 2結構體和函數(shù)調用 結構體類型變量的成員作實參 例8 6 結構體類型變量的成員作實參示例 structex2 longintnum floats 3 P257 long型 實型數(shù)組名 voidmyfun1 longintnum float p wang num s p num p p 1 p 2 1000101 printf 學號 ld 成績 num for i 0 i 3 i printf 5 1f p i printf n 100010189 590 079 0 wang num s p num p p 1 p 2 1000101 voidmyfun1 longintnum float p inti 0 將 wang num作實參 如何修改 結構體類型變量作實參 例8 7 結構體變量作實參的示例 structex2 longintnum floats 3 voidmyfun2 structex2xiaowang xiaowang num 1000105 xiaowang s 2 95 5 main structex2wang 1000101 89 5 90 0 79 0 inti 0 myfun2 wang printf 學號 ld 成績 wang num for i 0 i 3 i printf 5 1f wang s i printf n num s 100010189 590 079 0 wang xiaowang num s xiaowang num 1000105 1000105 xiaowang s 2 95 5 95 5 結構體類型變量地址作實參 例8 8 結構體變量地址作實參的示例 structex2 longintnum floats 3 voidmyfun3 structex2 p p num 1000105 p s 2 95 5 main structex2wang 1000101 89 5 90 0 79 0 inti 0 myfun3 printf 學號 ld 成績 wang num for i 0 i 3 i printf 5 1f wang s i printf n myfun3 voidmyfun3 structex2 p 1000105 95 5 結構體類型數(shù)組名作實參 例8 9 結構體類型數(shù)組名作實參的示例 structex2 longintnum floats 3 voidmyfun4 structex2 p inti 0 floatk 0 structex2 q NULL for q p qnum for i 0 is i k main inti 0 j 0 structex2stu 5 0 myfun4 stu for i 0 i 5 i printf ld號學生成績是 stu i num for j 0 j 3 j printf 5 1f stu i s j printf n 8 3提高部分 8 3 1結構體的進一步討論8 3 2鏈表 P261 8 3 1結構體的進一步討論 在一個結構體類型中可以含有另一個結構體類型 P261 例8 10 內(nèi)嵌結構體類型的示例 structdate inty intm intd structstudent charname 10 structdatebirthday floats 3 main structstudentLi LiLan 1982 12 25 88 75 85 5 printf s n Li name printf d d d n Li birthday y Li birthday m Li birthday d printf f f f n Li s 0 Li s 1 Li s 2 LiLan 1982 12 25 88 000000 75 000000 85 500000 y m d s 0 s 1 s 2 name birthday s 要改為1985 Li birthday y 1985 y m d s 0 s 1 s 2 name birthday s 要改為10 Li birthday m 10 y m d s 0 s 1 s 2 name birthday s 要輸入值 scanf d y m d s 0 s 1 s 2 name birthday s 要改為5 p birthday d 5 p p birthday d 5 y m d s 0 s 1 s 2 name birthday s 要輸入值 p scanf d scanf d 聲明可改為 structstudent charname 10 struct inty intm intd birthday floats 3 例8 11 內(nèi)嵌結構體類型為本結構體類型的示例 includestructlst intnum structlst next 能指向structlst類型變量 a num 1 a next b num 2 b next c num 3 c next NULL p printf 4d p num main structlsta b c p 1 2 3 0 p 1 a b c num next num next num next a num 1 a next b num 2 b next c num 3 c next NULL p printf 4d p num main p p next printf 4d p num 1 2 3 0 p 1 2 structlsta b c p a b c num next num next num next a num 1 a next b num 2 b next c num 3 c next NULL p printf 4d p num p p next printf 4d p num main p p next printf 4d p num 1 2 3 0 p 1 2 3 structlsta b c p 8 3 2鏈表 鏈表的概念 P264 非空鏈表結構 head 頭結點 結點1 結點2 尾結點 0 8 3 2鏈表 鏈表的概念 P264 鏈表特點 所有結點為相同結構體類型 至少一個成員為指針 該指針基類型與鏈表結點的類型相同 需解決問題 1 建立鏈表 2 輸出鏈表中各結點的值 3 在鏈表中插入一個結點 4 刪除鏈表中的一個結點 動態(tài)開辟和釋放函數(shù) malloc calloc realloc 動態(tài)開辟函數(shù) 釋放函數(shù) free include 例8 12 動態(tài)開辟和釋放存儲單元的示例 include include main int p NULL p int malloc 2 if p NULL printf 4d p free p p p 6 p 6 6 例8 12 動態(tài)開辟和釋放存儲單元的示例 include include main int p NULL p int malloc 2 if p NULL printf 4d p free p p int malloc sizeof int if p NULL printf 4d n p free p p 38 p 6 6 p 38 38 p int sizeof 動態(tài)鏈表 例8 13 調用函數(shù)完成鏈表的建立 輸出各結點的值 插入和刪除一個結點的功能 具體要求 調用mycreat函數(shù) 建立鏈表調用myinsert函數(shù) 插入一個結點調用mydelete函數(shù) 刪除值為m的結點調用myprint函數(shù) 輸出各結點 1 聲明結構體類型 structlst intnum structlst next typedefstructlstLST 可用LST代替structlst 2 編寫主函數(shù) 并先用空函數(shù)占被調函數(shù)的位置后測試 include includetypedefstructlst intnum structlst next LST LST mycreat voidmyprint voidmyinsert intmydelete main LST head NULL intk 0 m 0 choose 0 head mycreat printf 新建鏈表為 myprint head printf 請選擇 1 插入2 刪除 scanf d switch choose case1 printf 插入點 scanf d myinsert head m break case2 printf 刪除點 scanf d k mydelete head m if k 1 printf 刪除點后 myprint head elseprintf 不存在 n break 3 編寫mycreat 函數(shù) 并用此函數(shù)代替對應的空函數(shù)后運行程序 開辟頭結點 并用頭指針head指向它 head LST malloc sizeof LST head head 頭結點 q num next 使指針變量q也指向該頭結點 q head 3 編寫mycreat 函數(shù) 并用此函數(shù)代替對應的空函數(shù)后運行程序 head 頭結點 q p num next 開辟新的結點 并使指針變量p指向它 p LST malloc sizeof LST 3 編寫mycreat 函數(shù) 并用此函數(shù)代替對應的空函數(shù)后運行程序 head 頭結點 q num next 結點1 num next 連接新結點和當前鏈表的最后結點 q next p p 3 編寫mycreat 函數(shù) 并用此函數(shù)代替對應的空函數(shù)后運行程序 head 頭結點 q num next 結點1 num next 將數(shù)據(jù)賦予新結點的num成員 p num m 101 p 3 編寫mycreat 函數(shù) 并用此函數(shù)代替對應的空函數(shù)后運行程序 head 頭結點 q num next 結點1 num next 使q指向新鏈表的最后一個結點 101 p 3 編寫mycreat 函數(shù) 并用此函數(shù)代替對應的空函數(shù)后運行程序 head 頭結點 q num next 結點1 num next 使q指向新鏈表的最后一個結點 q p 101 p 3 編寫mycreat 函數(shù) 并用此函數(shù)代替對應的空函數(shù)后運行程序 head 頭結點 num next 結點1 num next 開辟新的結點 并使指針變量p指向它 101 p q 3 編寫mycreat 函數(shù) 并用此函數(shù)代替對應的空函數(shù)后運行程序 head 頭結點 num next 結點1 num next 新結點 num next 開辟新的結點 并使指針變量p指向它 101 p q p LST malloc sizeof LST 3 編寫mycreat 函數(shù) 并用此函數(shù)代替對應的空函數(shù)后運行程序 head 頭結點 num next 結點1 num next 新結點 num next 連接新結點和當前鏈表的最后結點 q next p 101 p 3 編寫mycreat 函數(shù) 并用此函數(shù)代替對應的空函數(shù)后運行程序 head 頭結點 num next 結點1 num next 新結點 num next 將數(shù)據(jù)賦予新結點的num成員 p num m 101 103 3 編寫mycreat 函數(shù) 并用此函數(shù)代替對應的空函數(shù)后運行程序 head 頭結點 num next 結點1 num next 新結點 num next 使q指向新鏈表的最后一個結點 101 103 3 編寫mycreat 函數(shù) 并用此函數(shù)代替對應的空函數(shù)后運行程序 head 頭結點 num next 結點1 num next 新結點 num next 使q指向新鏈表的最后一個結點 q p 101 103 3 編寫mycreat 函數(shù) 并用此函數(shù)代替對應的空函數(shù)后運行程序 head 頭結點 num next 結點1 num next 新結點 num next 鏈表的最后一個結點設為尾結點 q next NULL 101 103 0 3 編寫mycreat 函數(shù) 并用此函數(shù)代替對應的空函數(shù)后運行程序 返回鏈表頭結點的地址 returnhead head 頭結點 num next 結點1 num next 新結點 num next 101 103 0 3 編寫mycreat 函數(shù) 并用此函數(shù)代替對應的空函數(shù)后運行程序 LST mycreat intm 0 LST head NULL p NULL q NULL head LST malloc sizeof LST q head printf 建立鏈表 請輸入數(shù)值 n printf Inputm scanf d while m 1 p LST malloc sizeof LST q next p p num m q p printf Inputm scanf d q next NULL returnhead 4 編寫myprint 函數(shù) 并用此函數(shù)代替對應的空函數(shù)后運行程序 head 頭結點 結點1 結點2 尾結點 0 101 103 105 使指針p指向鏈表中結點1 p p head next head 頭結點 結點1 結點2 尾結點 0 101 103 105 輸出p所指結點的num成員值 p printf 5d p num 101 head 頭結點 結點1 結點2 尾結點 0 101 103 105 移動p 即使p指向下一個結點 p head 頭結點 結點1 結點2 尾結點 0 101 103 105 移動p 即使p指向下一個結點 p head 頭結點 結點1 結點2 尾結點 0 101 103 105 輸出p所指結點的num成員值 p printf 5d p num 103 head 頭結點 結點1 結點2 尾結點 0 101 103 105 移動p 即使p指向下一個結點 p head 頭結點 結點1 結點2 尾結點 0 101 103 105 移動p 即使p指向下一個結點 p p next head 頭結點 結點1 結點2 尾結點 0 101 103 105 輸出p所指結點的num成員值 p printf 5d p num 105 移動p 即使p指向下一個結點 head 頭結點 結點1 結點2 尾結點 0 101 103 105 p head 頭結點 結點1 結點2 尾結點 0 101 103 105 移動p 即使p指向下一個結點 不存在 所以p NULL p p next voidmyprint LST head LST p NULL p head next if p NULL printf 鏈表為空表 elsedo printf 5d p num p p next while p NULL printf n 5 編寫myinsert 函數(shù) 并用此函數(shù)代替對應的空函數(shù)后運行程序 head 頭結點 結點1 尾結點 結點2 新結點 s 101 103 105 0 指針s去開辟需插入的結點 s LST malloc sizeof LST head 頭結點 結點1 尾結點 結點2 新結點 s 101 103 105 0 104 需插入的數(shù)值賦予新結點的num成員 s num m head 頭結點 結點1 尾結點 結點2 新結點 s 101 103 105 0 104 q指向頭結點 q head p指向結點1 p head next head 頭結點 結點1 尾結點 結點2 新結點 s 101 103 105 0 104 q p 判斷是否找到插入點 if p num m head 頭結點 結點1 尾結點 結點2 新結點 s 101 103 105 0 104 q p q p都移到下一個結點 head 頭結點 結點1 尾結點 結點2 新結點 s 101 103 105 0 104 q p都移到下一個結點 q q next p head 頭結點 結點1 尾結點 結點2 新結點 s 101 103 105 0 104 q q p都移到下一個結點 q q next p p next head 頭結點 結點1 尾結點 新結點 s 101 103 105 0 104 q 判斷是否找到插入點 if p num m p 結點2 head 頭結點 結點1 尾結點 結點2 新結點 s 101 103 105 0 104 q p q p都移到下一個結點 head 頭結點 結點1 尾結點 結點2 新結點
溫馨提示
- 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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 醫(yī)院整形科合作協(xié)議書
- 2025年度個人財務規(guī)劃咨詢服務協(xié)議4篇
- 2025年度個人車輛購置貸款合同范本8篇
- 2025年度個人知識產(chǎn)權抵押轉讓合同2篇
- 2025年度個人股權激勵股份轉讓合同協(xié)議書3篇
- 2025年全球及中國強固型工業(yè)顯示器行業(yè)頭部企業(yè)市場占有率及排名調研報告
- 2025年度全國房地產(chǎn)個人居間服務協(xié)議合同范本4篇
- 2025版圖書倉儲物流配送服務合同3篇
- 2025個人借款合同信息披露與隱私保護4篇
- 2024版借款施工合同
- 《openEuler操作系統(tǒng)》考試復習題庫(含答案)
- 《天潤乳業(yè)營運能力及風險管理問題及完善對策(7900字論文)》
- 醫(yī)院醫(yī)學倫理委員會章程
- xx單位政務云商用密碼應用方案V2.0
- 2024-2025學年人教版生物八年級上冊期末綜合測試卷
- 2025年九省聯(lián)考新高考 語文試卷(含答案解析)
- 死亡病例討論總結分析
- 第二章 會展的產(chǎn)生與發(fā)展
- 空域規(guī)劃與管理V2.0
- JGT266-2011 泡沫混凝土標準規(guī)范
- 商戶用電申請表
評論
0/150
提交評論