![《C語言》課內(nèi)實驗報告_第1頁](http://file2.renrendoc.com/fileroot_temp3/2021-9/30/34337133-8d08-4430-8882-8e40fc57ac4b/34337133-8d08-4430-8882-8e40fc57ac4b1.gif)
![《C語言》課內(nèi)實驗報告_第2頁](http://file2.renrendoc.com/fileroot_temp3/2021-9/30/34337133-8d08-4430-8882-8e40fc57ac4b/34337133-8d08-4430-8882-8e40fc57ac4b2.gif)
![《C語言》課內(nèi)實驗報告_第3頁](http://file2.renrendoc.com/fileroot_temp3/2021-9/30/34337133-8d08-4430-8882-8e40fc57ac4b/34337133-8d08-4430-8882-8e40fc57ac4b3.gif)
![《C語言》課內(nèi)實驗報告_第4頁](http://file2.renrendoc.com/fileroot_temp3/2021-9/30/34337133-8d08-4430-8882-8e40fc57ac4b/34337133-8d08-4430-8882-8e40fc57ac4b4.gif)
![《C語言》課內(nèi)實驗報告_第5頁](http://file2.renrendoc.com/fileroot_temp3/2021-9/30/34337133-8d08-4430-8882-8e40fc57ac4b/34337133-8d08-4430-8882-8e40fc57ac4b5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、c語言實驗報告c語言課內(nèi)實驗報告學(xué)生姓名:及 學(xué) 號:學(xué) 院:理學(xué)院班 級:數(shù)學(xué)111課程名稱:c語言實驗題目:結(jié)構(gòu)體的應(yīng)用指導(dǎo)教師姓名及職稱: 2012年06月05日- 13 -一、實驗題目:結(jié)構(gòu)體的應(yīng)用二、實驗?zāi)康模?進一步掌握結(jié)構(gòu)體變量、數(shù)組的定義和使用方法,掌握結(jié)構(gòu)體與指針的應(yīng)用。2學(xué)習(xí)共用體的概念和使用。3學(xué)習(xí)鏈表的概念和使用。三、實驗內(nèi)容:1有6個學(xué)生,每個學(xué)生的數(shù)據(jù)包括學(xué)號、姓名、性別、4門課的成績、總成績、平均成績。從鍵盤輸入每個學(xué)生信息及4門課成績,總成績及平均成績要通過4門課算出。然后用選擇排序法按照總成績由高到低對6個學(xué)生數(shù)據(jù)進行排序并輸出排序結(jié)果。要求輸入、排序、輸出用
2、3個自定義函數(shù)實現(xiàn)。編寫源程序,給出注釋及運行結(jié)果。(提示,請參閱教材上292頁例11.5及例11.6的程序)。2建立一個含有10個結(jié)點的單鏈表,每個節(jié)點包括:學(xué)號、姓名、性別、年齡和一門課程的成績。輸入一個學(xué)號,刪去等于此學(xué)號的結(jié)點;按學(xué)號排序向原單鏈表中插入兩個新結(jié)點。編寫源程序,給出注釋及運行結(jié)果。(提示,請參閱教材上297頁至308頁例11.8-例11.11的程序)。四、實驗結(jié)果:1.程序如下:#include /*連接庫函數(shù)stdio.h*/struct student /*聲明結(jié)構(gòu)體類型struct student,并定義此結(jié)構(gòu)體類型的數(shù)組stu6*/int s_no; /*定義整
3、形變量s_no于結(jié)構(gòu)體類型struct student中*/char s_name10; /*定義字符數(shù)組s_name于結(jié)構(gòu)體類型struct student中*/char s_sex; /*定義字符形變量s_sex于結(jié)構(gòu)體類型struct student中*/float s_score4; /*定義浮點形數(shù)組s_score于結(jié)構(gòu)體類型struct student中*/float sum; /*定義浮點形變量sum于結(jié)構(gòu)體類型struct student中*/float average; /*定義浮點形變量average于結(jié)構(gòu)體類型struct student中*/stu6;struct st
4、udent *input(struct student *stu,int n) /*定義函數(shù)input(struct student *stu,int n),此函數(shù)返回指向struct student類型數(shù)據(jù)的指針*/struct student *p; /*定義指向struct student類型數(shù)據(jù)的指針p*/int i; /*定義整型變量i*/p=stu; /* p=stu*/printf(please input the infomation of student:nn); /*輸出” please input the infomation of student”*/printf( n
5、o name sex score1 score2 score3 score4n); /*輸出”no name sex score1 score2 score3 score4”*/for(i=0;in;i+,p+) /*i=0;當(dāng)is_no,p-s_name,&p-s_sex,&p-s_score0,&p-s_score1,&p-s_score2,&p-s_score3); /*輸入struct student所需的數(shù)據(jù),儲存在p指向的變量的地址*/return(&stu0); /*函數(shù)返回&stu0的值*/void output(struct student *p,int n) /*定義空型函
6、數(shù)output(struct student *p,int n)*/int i; /*定義整型變量i*/printf(nnthe information of the students is:nn); /*輸出”the information of the students is:”*/printf(no name sex score1 score2 score3 score4 sum averagen); /*輸出no name sex score1 score2 score3 score4 sum average”*/ for(i=0;in;i+) /*i=0;當(dāng)is_no,pi-s_na
7、me,pi-s_sex,pi-s_score0,pi-s_score1,pi-s_score2,pi-s_score3,pi-sum,pi-average); /*輸出pi所指向變量的各個數(shù)據(jù)*/void count(struct student *stu,int n) /*定義空型函數(shù)count(struct student *stu,int n)*/struct student *p; /*定義指向struct student類型變量的指針p*/int i; /*定義整型變量i*/p=stu; /* p=stu*/for(i=0;in;i+,p+) /*i=0;當(dāng)isum=p-s_scor
8、e0+p-s_score1+p-s_score2+p-s_score3; /*計算p-sum的值*/p-average=p-sum/4; /* p-average=p-sum/4*/ void sorted(struct student *stu,struct student *p,int n) /*定義空型函數(shù)sorted(struct student *stu,struct student *p,int n)*/int i,j,k; /*定義整型變量i,j,k*/struct student *temp; /*定義指向struct student類型變量的指針temp*/ for(i=0;
9、in-1;i+) /*i=0;當(dāng)in-1時,執(zhí)行循環(huán),i自加*/k=i; /* k=i*/for(j=i+1;jn;j+) /*j=i+1;當(dāng)isumsum) /*如果pk-sumsum*/k=j; /* k=j*/if(k!=i) /*如果k!=i*/temp=pi; /* temp=pi; pi=pk; pk=temp;*/ pi=pk;pk=temp;void main() /*空型定義主函數(shù)*/int i; /*定義整型變量i*/struct student *p6; /*定義指向struct student類型變量的指針數(shù)組p*/for(i=0;i6;i+) /*i=0;當(dāng)i6時,執(zhí)
10、行循環(huán),i自加*/pi=stu+i; /* pi=stu+i*/input(stu,6); /*執(zhí)行函數(shù)input(stu,6)*/count(stu,6); /*執(zhí)行函數(shù)count(stu,6)*/sorted(stu,p,6); /*執(zhí)行函數(shù)sorted(stu,p,6)*/output(p,6); /*執(zhí)行函數(shù)output(p,6)*/程序運行結(jié)果如下:2.程序如下:#include /*連接庫函數(shù)stdio.h*/ #include/*連接庫函數(shù)malloc.h*/#define len sizeof(struct student) /*定義符號常量len,為sizeof(struct
11、 student)*/struct student /*聲明結(jié)構(gòu)體類型struct student*/int no; /*定義整形變量no于結(jié)構(gòu)體類型struct student中*/char name20; /*定義字符數(shù)組name20 于結(jié)構(gòu)體類型struct student中*/char sex5; /*定義字符數(shù)組sex5 于結(jié)構(gòu)體類型struct student中*/int age; /*定義整形變量age于結(jié)構(gòu)體類型struct student中*/float score; /*定義浮點型變量score于結(jié)構(gòu)體類型struct student中*/struct student *n
12、ext; /*定義指向結(jié)構(gòu)體變量struct student的指針變量next于結(jié)構(gòu)體類型struct student中*/;int n=0; /*定義全局整型變量n=0*/struct student *creat(void) /*定義函數(shù)student *creat(void),此函數(shù)返回指向struct student類型數(shù)據(jù)的指針*/struct student *p1,*p2,*head; /*定義指向struct student類型變量的指針p1,p1,head*/int k=1; /*定義整型變量k=1*/head=null; /* head=null ;n=0*/n=0;p1=
13、p2=(struct student *)malloc(len); /*開辟len大小的空間令p1,p2指向空間的首地址*/if(p1!=null) /*如果p1!=null*/printf(%sn,請輸入學(xué)生信息); /*輸出“請輸入學(xué)生信息”*/printf(%sn,學(xué)號 姓名 性別 年齡 成績); /*輸出“學(xué)號 姓名 性別 年齡 成績”*/ scanf(%d%s%s%d%f,&p1-no,p1-name,p1-sex,&p1-age,&p1-score);/*輸入struct student所需的數(shù)據(jù),儲存在p1指向的變量的地址*/while(k!=2) /*當(dāng)k!=2時,執(zhí)行循環(huán)*/
14、n=n+1; /* n=n+1*/if(n=1) head=p1; /*如果n=1,head=p1*/else p2-next=p1; /*否則p2-next=p1*/p2=p1; /* p2=p1*/printf(%s,是否繼續(xù) 1 是;2 否 ); /*輸出“是否繼續(xù) 1 是;2 否”*/scanf(%d,&k); /*輸入k的值*/if(k=2) break; /*如果k=2,跳出循環(huán)*/p1=(struct student *)malloc(len); /*開辟len大小的空間令p1指向空間的首地址*/if(p1!=null) /*如果p1!=null*/ scanf(%d%s%s%d
15、%f,&p1-no,p1-name,p1-sex,&p1-age,&p1-score);/*輸入struct student所需的數(shù)據(jù),儲存在p1指向的變量的地址*/ else goto loop; /*否則跳到loop標(biāo)記的程序*/ p2-next=null; /* p2-next=null*/return(head); /*返回head作為函數(shù)的返回值*/elseloop: printf(%s,內(nèi)存不夠,無法建立鏈表); /*否則輸出“內(nèi)存不夠,無法建立鏈表”并用loop標(biāo)記*/return(head); /*返回head作為函數(shù)的返回值*/struct student *insert(s
16、truct student *head)/*定義函數(shù)insert(struct student *head),此函數(shù)返回指向struct student類型數(shù)據(jù)的指針*/struct student *p1,*p2,*p0; /*定義指向struct student類型變量的指針p1,p1,p0*/p2=p1=head; /* p2=p1=head */if(head=null)printf(n%sn,請先建立學(xué)生信息); return(head); /*如果head=null,輸出“請先建立學(xué)生信息”,返回dead*/p0=(struct student *)malloc(len); /*開
17、辟len大小的空間令p0指向空間的首地址*/printf(%sn,請輸入學(xué)生信息); /*輸出“請輸入學(xué)生信息”*/ printf(%sn,學(xué)號 姓名 性別 年齡 成績); /*輸出“學(xué)號 姓名 性別 年齡 成績”*/ scanf(%d%s%s%d%f,&p0-no,p0-name,p0-sex,&p0-age,&p0-score); /*輸入struct student所需的數(shù)據(jù),儲存在p0指向的變量的地址*/if(head=null) /*如果(head=null*/head=p1;p1-next=null;n+; /* head=p1;p1-next=null;n+*/printf(n%
18、sn,學(xué)生信息添加成功); /*輸出“學(xué)生信息添加成功”*/else /*否則*/while(p0-nop1-no&p1-next!=null) /*當(dāng)p0-nop1-no且p1-next!=null執(zhí)行循環(huán)*/p2=p1; /* p2=p1; p1=p1-next;*/ p1=p1-next; if(p1-next=null) /*如果p1-next=null*/p1-next=p0; /* p1-next=p0;p0-next=null;n+;*/p0-next=null;n+;printf(n%sn,學(xué)生信息添加成功); /*輸出“學(xué)生信息添加成功”*/else /*否則*/p2-nex
19、t=p0; /* p2-next=p0;p0-next=p1;n+;*/p0-next=p1;n+;printf(n%sn,學(xué)生信息添加成功); /*輸出“學(xué)生信息添加成功”*/return(head); /*函數(shù)返回head*/struct student *del(struct student *head) /*定義函數(shù)insert(struct student *head),此函數(shù)返回指向struct student類型數(shù)據(jù)的指針*/struct student *p1,*p2; /*定義指向struct student類型變量的指針p1,p1*/int num; /*定義整型變量num
20、*/printf(%s,請輸入要刪除學(xué)生的學(xué)號:); /*輸出“請輸入要刪除學(xué)生的學(xué)號”*/scanf(%d,&num); /*輸入num的值*/p2=p1=head; /* p2=p1=head*/if(p1=null) /*如果p1=null*/ printf(n%sn,此鏈表為空,無法刪除); /*輸出此鏈表為空,無法刪除*/else /*否則*/while(p1-no!=num & p1-next!=null) /*當(dāng)(p1-no!=num且p1-next!=null時執(zhí)行循環(huán)*/p2=p1; /* p2=p1; p1=p1-next;*/p1=p1-next;if(p1-next=n
21、ull&p1-no!=num) /*如果p1-next=null且p1-no!=num*/printf(n%sn,無此學(xué)生的信息); /*輸出“無此學(xué)生的信息”*/else /*否則*/if(p1=p2) /*如果p1=p2*/head=p1-next;n-; /* head=p1-next;n-*/printf(n%sn,學(xué)生信息刪除成功); /*輸出“學(xué)生信息刪除成功”*/else /*否則*/p2-next=p1-next;n-; /* p2-next=p1-next;n-*/printf(n%sn,學(xué)生信息刪除成功); /*輸出“學(xué)生信息刪除成功”*/return(head); /*函
22、數(shù)返回head*/void output(struct student *head) /*定義空型函數(shù)output(struct student *head)*/struct student *p; /*定義指向struct student類型變量的指針p*/p=head; /* p=head*/printf(%s%d%snn,此鏈表有,n,條信息); /*輸出“此鏈表有,n,條信息”*/if(head!=null) /*如果head!=null*/printf(%sn,學(xué)號 姓名 性別 年齡 成績); /*輸出“學(xué)號 姓名 性別 年齡 成績”*/while(p!=null) /*當(dāng)p!=nu
23、ll時,執(zhí)行循環(huán)*/printf(%-4d%5s%-4s%-5d%-7.2fn,p-no,p-name,p-sex,p-age,p-score); /*輸出p1所指向變量的各個數(shù)據(jù)*/p=p-next; /* p=p-next*/int index() /*定義返回整形數(shù)據(jù)的函數(shù)index()*/int flag; /*定義整型變量flag*/printf(n*%s*nn,學(xué)生信息管理系統(tǒng)); /*輸出“*學(xué)生信息管理系統(tǒng)*”*/printf( 1%sn,建立學(xué)生信息); /*輸出“1建立學(xué)生信息”*/ printf( 2%sn,添加學(xué)生信息); /*輸出“2添加學(xué)生信息”*/printf( 3%sn,刪除學(xué)生信息); /*輸出“3刪除學(xué)生信息”*/printf( 4%sn,顯示學(xué)生信息); /*輸出“4顯示學(xué)生信息”*/printf( 0%snn,退出); /*輸出“0退出”*/printf( %s,請選擇: ); /*輸出“請選擇”*/scanf(%d,&flag); /*輸入flag的值*/ return(flag); /*函數(shù)返回flag*/void main() /*定義空型主函數(shù)*/ struct student *head=null; /*定義指向struct student類型變量的指針head=null*/ int f
溫馨提示
- 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 八年級歷史人教版下冊聽課評課記錄:第5課 三大改造
- 林地長期承包合同范本
- 鄉(xiāng)鎮(zhèn)精裝修商鋪出租合同范本
- 儲存場地租賃合同范本
- 廣告公司材料采購合同范本
- 二零二五年度無子女離婚協(xié)議書及子女教育資助合同
- 二零二五年度酒店會議室場地租賃及配套交通合同
- 二零二五年度酒吧租賃合同合同簽訂后的租賃物維護責(zé)任
- 2025年度商鋪轉(zhuǎn)讓三方合同附品牌使用權(quán)及營銷支持
- 夏令營代理商合作協(xié)議書范本
- 三星SHP-DP728指紋鎖說明書
- 預(yù)應(yīng)力錨索張拉及封錨
- 烤煙生產(chǎn)沿革
- GB 1886.227-2016食品安全國家標(biāo)準(zhǔn)食品添加劑嗎啉脂肪酸鹽果蠟
- 毛澤東思想課件-第七章 毛澤東思想的活的靈魂
- 公共關(guān)系效果的評估課件
- 建筑施工安全員理論考核試題與答案
- 高速公路用地勘測定界及放線定樁技術(shù)標(biāo)書
- 華萊士標(biāo)準(zhǔn)化體系
- 快捷smt全自動物料倉儲方案
- keysight眼圖和抖動噪聲基礎(chǔ)知識與測量方法
評論
0/150
提交評論