計(jì)二-c語言11結(jié)構(gòu)體公用_第1頁
計(jì)二-c語言11結(jié)構(gòu)體公用_第2頁
計(jì)二-c語言11結(jié)構(gòu)體公用_第3頁
計(jì)二-c語言11結(jié)構(gòu)體公用_第4頁
計(jì)二-c語言11結(jié)構(gòu)體公用_第5頁
免費(fèi)預(yù)覽已結(jié)束,剩余94頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

11.1概述結(jié)構(gòu)體變量的結(jié)構(gòu)體數(shù)11.6指向結(jié)構(gòu)體類型數(shù)據(jù)的指用指針處理鏈共用枚舉類用typedef定義類概概問題定義 圖11-int圖11-LiM概概 一個結(jié)構(gòu)體類型的一般形式為 {成員表列如:struct{

intnum;charname[20];charintage;floatscore;char}定義結(jié)定義結(jié)構(gòu)體類型變量的方圖11-圖11-先結(jié)構(gòu)體類型再定義變量例如:struct student1,FM定義定義結(jié)構(gòu)體類型變量的方注意定義定義結(jié)構(gòu)體類型變量的方在類型的同時定義變 {}定定義結(jié)構(gòu)體類型變量的方student類型的變struct intcharname[20];charsex;intage;floatscore;char變變11.2定義結(jié)構(gòu)體類變變直接定義結(jié)構(gòu)體類{

注意注意(注意(變定義結(jié)構(gòu)體類型變

一個struct構(gòu)例如:structdate 一個構(gòu)struct{intcharname[20];charsex;int

指 指

圖11-結(jié)構(gòu)體變量的 這個變量。但應(yīng)遵守以下規(guī)則:例如:已定義student1和student2為結(jié)構(gòu)體變11.3結(jié)構(gòu)體變量的結(jié)構(gòu)體變量名例如,student1.num表示student1變量中的num成員,即student1的num(學(xué)號)項(xiàng)??梢詫ψ兞康某蓡T賦值,例:student1.num=10010;“.”是成員(分量)運(yùn)算符,它在所有的運(yùn)算符中優(yōu)先級最高,因此可以把student1.num作為一個整體來看待。上面賦值語句的作用是將整數(shù)11.3結(jié)構(gòu)體變量的最最對上面定義的結(jié)構(gòu)體這樣各成員:

注意 birthday,因student1.birthday.montbirthday本身是以類以類11.3結(jié)構(gòu)體變量的 11.3結(jié)構(gòu)體變量的初初運(yùn)行結(jié)果address:初初運(yùn)行結(jié)果address:123Beijingvoid{struct{longintnum;charname[20];charsex;char}a={10101,″LiLin″,′M′,″123BeijingRoad″};/*對結(jié)構(gòu)體變量a賦初值*/printf(″No.:%l \ 結(jié)構(gòu)體數(shù)據(jù)(如一個學(xué)生的學(xué)號、、成績等結(jié)構(gòu)體數(shù)structdd};struct結(jié)構(gòu)體數(shù)也可以直接定義一個結(jié)構(gòu)體數(shù)組,例如struct t t圖11-圖11-結(jié)構(gòu)體數(shù)初對初對struct{intnum;charname[20]intage;floatscore;18,87.5,″10310102,″Zhang,″130Shanghai

圖11-結(jié)構(gòu)體數(shù)…{int…struct 結(jié)構(gòu)體數(shù)例1.2對候選人得票的統(tǒng)計(jì)程序。設(shè)有3個候選人,每次輸入一個得票的候選人的名字,要求最后輸出各人得票結(jié)果。#include<string.h>#include<stdio.h>{charname[20];inleader[3]={“Li”,0,“Zhang”,0,“例void{inti,j;charleader_name{}

運(yùn)行結(jié)果↙00 結(jié)構(gòu)體數(shù)圖11-name()和count(票數(shù))。在定圖11-000指向結(jié)構(gòu)體類型數(shù)據(jù)的指例11.3指向結(jié)構(gòu)體變量的指針#include<string.h>#include<stdio.h>

運(yùn)行結(jié)果用cu用cuvoid{struct

指向structstudent

structstudentststructstudent*p;

stu_1.sex=‘M’;sp指向的結(jié)=89.5;,stu-1.num,stu-

:%cprintf(″No.:%ld\nme: \}數(shù)據(jù)的指的指數(shù)據(jù)的指的程序分析始地址賦給指針變量p,圖11-指向結(jié)構(gòu)體類型數(shù)據(jù)的指以下3種形 。 指向結(jié)構(gòu)體類型數(shù)據(jù)的指例11.4指向結(jié)#include<stdio.hstructstudent{intnum;charnastructstudentstvoid

運(yùn)行結(jié)果構(gòu) 構(gòu) Zhang structstudentprintf(″No. printf(″%5d%-20s%2c%4d\n″,p->num,p->name,>sex,p-}數(shù)結(jié)行指向結(jié)數(shù)結(jié)行程序分析p是指向structstudent+1,p指向stu[1]出stu[2]的各成員值。在執(zhí)stu

圖11-指向結(jié)構(gòu)體類型數(shù)據(jù)的指注意 請注意以上二者的不同指向結(jié)構(gòu)體類型數(shù)據(jù)的指注意程序已定義了p是一個指向structstudent例如 student*)指向結(jié)構(gòu)體類型數(shù)據(jù)的指11.6.3指向結(jié)構(gòu)體類型數(shù)據(jù)的指例11.5有一個結(jié)構(gòu)體變量stu,內(nèi)含學(xué)生學(xué)號、 #include<stdio.h>structstudent{intcharname[20];floatscore[3];指向結(jié)構(gòu)體類型數(shù)據(jù)的指運(yùn)行結(jié)運(yùn)行結(jié):LiLi{voidprint(structstudent);structstudentstu;}voidprint(structstudent{printf(FORMAT,stu.num,,作運(yùn)行結(jié):LiLi例作運(yùn)行結(jié):LiLi#include<stdio.h>structstudent{intcharname[20];floatscore[3];};stu={12345,void{voidprint(structstudent voidprint(structstudent /*形參類型修改了 /*用指針變量調(diào)用各成員的值數(shù)體。個賦指向數(shù)體。個賦程序分析向structstudent類型數(shù)據(jù)

圖11-§11.7指針處理鏈圖11-圖11-§11.7指針處理鏈struct{intnum;floatscore;圖11-圖11-運(yùn)行結(jié)果用運(yùn)行結(jié)果#include<stdio.h>#defineNULL0struct{longnum; floatscore;structstudent*next;};{structstudentnum=99101;num=99103;num=99107; {printf("%ld%5.1f\n",p->num,p->score); }while(p!=NULL);}用指針處理鏈程序分析“c.next=NULL”的作用是使c.next不指向任何有 結(jié)點(diǎn),然后輸出a結(jié)點(diǎn)中的數(shù)據(jù),“p=p->next”是用指針處理鏈庫函數(shù)提供動態(tài)地開辟和釋放單元的其函數(shù)原型為void*malloc(unsignedintsize);其)用指針處理鏈其函數(shù)原型為void*calloc(unsignedn,unsignedsize);其作用是在內(nèi)存的動態(tài)區(qū)用指針處理鏈free其函數(shù)原型為voidfree(void*p);其作用ANSI鏈鏈立圖11-用指鏈鏈立圖11-例11.5用指針處理鏈于指為鏈于指為鏈

用指針處理鏈圖11-圖11-用指針處理鏈圖11-圖11-用指針處理鏈環(huán)圖11-環(huán)圖11-用指針處理鏈#include<stdio.h>#include<malloc.h>#defineNULL0//令NULL代表0,用它表#defineLENsizeof(struct //令LEN代表//student類型數(shù)據(jù)的長struct{longfloat structstudent};intn;//n為全局變量,本文件模塊中各函數(shù)均可使用11.7指針處理鏈structstudent{structstudent*head; structstudent*p1,*p2; p1=p2=(structstudent*)malloc(LEN);{n=n+1; elsep2->next=p1; p1=(structstudent*)malloc(LEN);} 11.7指針處理鏈一11.7指針處理鏈例11.9編寫一個輸出鏈表的函數(shù)voidprint(structstudent{structstudentprintf("\nNow,These%drecordsare:\n",n);{printf("%ld%5.1f\n",p->num,p->score);}11.7指針處理鏈來,只要撤銷原來的關(guān)系即可。11.7指針處理鏈例11.10寫一函數(shù)以刪除動態(tài)鏈表中指定的結(jié)點(diǎn).11.7指針處理鏈第一個結(jié)點(diǎn)。之前應(yīng)將p1的值賦給p2,使p2指向剛才檢查過的那個結(jié)點(diǎn)。11.7指針處理鏈注意11.7指針處理鏈注意next賦給p2->next,見圖11用用指針處理鏈圖11-算法圖11-11.7指針處理鏈刪除結(jié)點(diǎn)的函數(shù)structstudent*del(structstudent*head,long{structstudentif(head==NULL){printf("\nlistnull!\n");gotoend;}while(num!=p1->num&&p1- elsep2->next=p1->next; elseprintf("%ldnotbeenfound!\n",num);用用指針處理鏈對鏈表的操 為了能做到正確,必須解決兩個問題①怎樣找到的位置②怎樣實(shí)現(xiàn)p1->num,則待的結(jié)點(diǎn)不應(yīng)插在p1所指大,則應(yīng)使p1繼續(xù)后移,直到p0->p1num為如果 如u2> 0§§圖11-用指針處理鏈算法圖11-用用指針處理鏈例11.11結(jié)點(diǎn)的函數(shù)insert如下structstudent*insert(structstudent*head,structstudent{structstudent*p0,*p1,*p2; {head=p0;p0-else{while((p0->num>p1->num)&&(p1- {if(head==p1)head=p0;elsep2->next=p0;p0->next=p1;}else{p1->next=p0;p0->next=NULL;}} }void{structstudent*head,stu;longdel_num;prinf(″intputrecords:\n″);head=creat();print(head);printf(″\nintputthedeletedscanf(″%ld″,&del_num);head=del(head,del_num);printf(″\nintputthedeletednumber:\n″);scanf(″%ld″,&stu.num,&stu.score);}用用指針處理鏈Input (建立鏈表Now,these3recordsintputthedeletednumber (刪除Now,these4recordsare:inputtheinserted(第一個結(jié)點(diǎn)Now,these3records

inputtheinsertedrecord第二個結(jié)點(diǎn)Now,these4records出現(xiàn)以上結(jié)果的原因是stu結(jié)點(diǎn)到鏈表中,第二次若再用它來決這個問題,必須在每一個結(jié)點(diǎn)時新開,能多個結(jié)點(diǎn)(直到輸入要的學(xué)號{structstudentlongdel_num;printf("inputrecords:\n"); print(head);printf("\ninputthedeletednumber:");whileprint(head);printf("inputthedeletednumber:");scanf("%ld",&del_num);}printf("\ninputtheinsertedrecord:");stu=(structstudent*)malloc(LEN);while(stu->num!=0){head=insert(head,stu);printf("inputtheinsertedrecord:");stu=(structstudent*)malloc(LEN);的,每次指向一個新的structstudent變量。在運(yùn)行結(jié)果Now,These3records intputthedeleted Now,these4recordsare1010199.010105

intputthedeleted Now,these4recordsare1010199.0intputtheinputtheinsertedNow,these3records1010110104

inputtheinsertedNow,these3records1010110104 共共用圖11-圖11-共用體的概。一。一列 {union union{int {intcharch; charfloat float };uniondata共用共用體和結(jié)構(gòu)體的比較 共共用共用體變量的方只有先定義了共用體變量才能它,而且不能共用體變量,而只能共用體變量中的例如:前面定義了a、b、c為共用體變a.i 共用體變量中的整型變量 共用體變量中的字符變量a.f 共用體類型數(shù)據(jù)的特不能對共用體變量名賦值,也不能企圖變11.8用例11.12設(shè)有若干個的數(shù)據(jù),其中有學(xué)生和教師。學(xué)生的數(shù)據(jù)中包括:、號碼、、職業(yè)、班級圖11-、號碼、、職業(yè)、職務(wù)??梢钥磮D11-11.8用#include<stdio.h>{intcharname[10];charsex;charjob;{intchar 運(yùn)行情況如運(yùn)行情況如fWangmNo.fWangst{inti;{scanf("%d%s%c%c",& [i].num,& [i].sex,& [i].job==scanf("%d",& elseif( [i].job=='T') elseprintf(“Inputerror!”);}printf("\n");枚枚舉類enum

enumweekdayworkday,weekday;變量值只能是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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論