




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
《C語言》實驗報告PAGE12-一、實驗題目:結構體的應用二、實驗目的:1.進一步掌握結構體變量、數組的定義和使用方法,掌握結構體與指針的應用。2.學習共用體的概念和使用。3.學習鏈表的概念和使用。三、實驗內容:1.有6個學生,每個學生的數據包括學號、姓名、性別、4門課的成績、總成績、平均成績。從鍵盤輸入每個學生信息及4門課成績,總成績及平均成績要通過4門課算出。然后用選擇排序法按照總成績由高到低對6個學生數據進行排序并輸出排序結果。要求輸入、排序、輸出用3個自定義函數實現。編寫源程序,給出注釋及運行結果。(提示,請參閱教材上292頁例11.5及例11.6的程序)。#include<stdio.h>/*包含stdio.h庫函數*/#include<string.h>/*包含string.h庫函數*/#defineN6/*宏定義用N表示6*/structstudent/*定義一個長度為6的student結構體數組stu[N]*/{ charname[10];/*結構體中包含長度為10的字符型數組name*/charnum[10];/*結構體中包含長度為10的字符型數組num*/ floatmath_score;/*結構體中包含浮點型數據math_score*/ floatenglish_score;/*結構體中包含浮點型數據english_score*/ floatcomputer_score;/*結構體中包含浮點型數據computer_score*/ floatC_program_score;/*結構體中包含浮點型數據C_program_score*/ floatsum_score;/*結構體中包含浮點型數據sum_score*/ floataverage_score;/*結構體中包含浮點型數據average_score*/}stu[N];voidmain()/*空類型主函數*/{ voidshow(structstudentstu[]);/*聲明空類型函數show函數包含形參structstudentstu[]*/ voidsort(structstudentstu[],intn);/*聲明空類型函數sort函數包含形參structstudentstu[]和intn*/ inti;/*定義整型變量i*/ printf("學生的個數:%d",N);/*標準輸出函數以整型輸出N的結果*/ for(i=0;i<N;i++)/*當i=0且i<N時i自加一*/ { stu[i].sum_score=0;/*把0賦值給stu[i].sum_score*/ printf("\n輸入學生成績%d:\n",i+1);/*以整形輸出i+1結果*/ printf("姓名:");/*輸出"姓名:"*/ scanf("%s",stu[i].name);/*以字符型輸入數據存到stu[i].name*/ printf("學號:");/*輸出"學號:"*/ scanf("%s",stu[i].num);/*以字符型輸出stu[i].num*/ printf("數學成績:");/*輸出"數學成績:"*/ scanf("%f",&stu[i].math_score);/*以浮點型輸入數據存到stu[i].math_score*/ printf("英語成績:");/*輸出"英語成績:"*/ scanf("%f",&stu[i].english_score);/*以浮點型輸入數據存到stu[i].english_score*/ printf("計算機成績:");/*輸出"計算機成績"*/ scanf("%f",&stu[i].C_program_score);/*以浮點型輸入數據存到stu[i].C_program_score*/ printf("c語言成績:");/*輸出"computer_score:"*/ scanf("%f",&stu[i].computer_score);/*以浮點型輸入數據存到stu[i].computer_score*/printf("\n");/*換行*/stu[i].sum_score=stu[i].math_score+stu[i].english_score+stu[i].computer_score+stu[i].C_program_score;/*把stu[i].math_score加stu[i].english_score加stu[i].computer_score加stu[i].C_program_score的結果賦給stu[i].sum_score*/ stu[i].average_score=stu[i].sum_score/4.0;/*把stu[i].sum_score除以4.0的結果賦給stu[i].average_score*/ }sort(stu,N);/*調用函數sort(stu,N)*/ show(stu);/*調用函數show(stu)*/}voidsort(structstudentstu[],intn)/*定義空類型函數sort其形參為structstudentstu[]和intn*/{ inti,j,k;/*定義整型變量i,j,k*/charg[10];/*定義有十個長度的字符型的數組g*/floatt;/*定義浮點型變量t*/ for(i=0;i<n-1;i++)/*當i=0且i<n-1時i自加一*/ { k=i;/*把i賦值給k*/ for(j=i+1;j<n;j++)/*當j=i+1且j<n時j自加一*/ if(stu[j].sum_score>stu[k].sum_score)/*如果stu[j].sum_score>stu[k].sum_score*/ k=j;/*把j賦值給k*/ t=stu[k].sum_score;/*把stu[k].sum_score賦值給t*/ stu[k].sum_score=stu[i].sum_score;/*把stu[i].sum_score賦值給stu[k].sum_score*/ stu[i].sum_score=t;/*把t賦值給stu[i].sum_score*/t=stu[k].math_score;/*把stu[k].math_score賦值給t*/ stu[k].math_score=stu[i].math_score;/*把stu[i].math_score賦值給stu[k].math_score*/ stu[i].math_score=t;/*把t賦值給stu[i].math_score*/ strcpy(g,stu[k].name);/*把stu[k].name賦值給g*/ strcpy(stu[k].name,stu[i].name);/*把stu[i].name賦值給stu[k].name*/ strcpy(stu[i].name,g);/*把g的值賦給stu[i].name*/ strcpy(g,stu[k].num);/*把stu[k].num賦值給g*/ strcpy(stu[k].num,stu[i].num);/*把stu[i].num賦值給stu[k].num*/ strcpy(stu[i].num,g);/*把g賦值給stu[i].num*/ t=stu[k].english_score;/*把stu[k].english_score賦值給t*/ stu[k].english_score=stu[i].english_score;/*把stu[i].english_score賦值給stu[k].english_score*/ stu[i].english_score=t;/*把t賦值給stu[i].english_score*/ t=stu[k].average_score;/*把stu[k].average_score賦值給t*/ stu[k].average_score=stu[i].average_score;/*把stu[i].average_score賦值給stu[k].average_score*/ stu[i].average_score=t;/*把t賦值給stu[i].average_score*/ t=stu[k].computer_score;/*把stu[k].computer_score賦值給t*/ stu[k].computer_score=stu[i].computer_score;/*把stu[i].computer_score賦值給stu[k].computer_score*/ stu[i].computer_score=t;/*賦值給stu[i].computer_score*/ t=stu[k].C_program_score;/*把stu[k].C_program_score賦值給t*/ stu[k].C_program_score=stu[i].C_program_score;/*把stu[i].C_program_score賦值給stu[k].C_program_score*/ stu[i].C_program_score=t;/*把t賦值給stu[i].C_program_score*/ }}voidshow(structstudentstu[])/*定義函數voidshow(structstudentstu[])*/{ inti;/*定義整型變量i*/ printf("\n姓名學號數學英語計算機C語言總成績平均成績\n");/*換行輸出”姓名學號數學英語計算機C語言總成績平均成績”再換行*/ for(i=0;i<N;i++)/*當i=0且i<N時i自加一*/ { printf("%s",stu[i].name);/*以字符串型輸出stu[i].name*/ printf("\t%s",stu[i].num);/*空出8列在以字符串型輸出stu[i].num*/ printf("\t%.2f",stu[i].math_score);/*空出8列再以浮點型保留兩位小數輸出stu[i].math_score*/ printf("%6.2f",stu[i].english_score);/*以字符行站6列寬度保留兩位小數輸出stu[i].english_score*/ printf("%8.2f",stu[i].computer_score);/*以字符行站8列寬度保留兩位小數輸出stu[i].computer_score*/ printf("%9.2f",stu[i].C_program_score);/*以字符行站9列寬度保留兩位小數輸出stu[i].C_program_score*/ printf("%11.2f",stu[i].sum_score);/*以字符行站11列寬度保留兩位小數輸出stu[i].sum_score*/ printf("%8.2f",stu[i].average_score);/*以字符行站8列寬度保留兩位小數輸出stu[i].average_score*/ printf("\n");/*換行*/ }}2.建立一個含有10個結點的單鏈表,每個節點包括:學號、姓名、性別、年齡和一門課程的成績。輸入一個學號,刪去等于此學號的結點;按學號排序向原單鏈表中插入兩個新結點。編寫源程序,給出注釋及運行結果。(提示,請參閱教材上297頁至308頁例11.8-例11.11的程序)。#include<stdio.h>/*包含stdio.h庫函數*/#include<malloc.h>/*包含malloc.h庫函數*/#defineNULL0/*宏定義用0代替NULL*/#defineLENsizeof(structstudent)/*宏定義用LEN代表structstudent的字節數*/structstudent/*定義結構體student*/{longno;/*定義長整型變量no為structstudent的成員*/charname[10];/*定義有十個長度的字符型數組name為structstudent的成員*/charsex;/*定義字符型變量sex為structstudent的成員*/intage;/*定義整型變量為structstudent的成員*/floatscore;/*定義浮點型變量score為structstudent的成員*/structstudent*next;/*定義指向structstudent的指針變量為structstudent的成員*/};intn;/*定義整型變量n*/structstudent*creat(void)/*定義指向結構體structstudent變量的指針變量*/{structstudent*head;/*定義指向structstudent的指針變量head為structstudent的成員*/structstudent*p1,*p2;/*定義指向structstudent的指針變量p1,p2為structstudent的成員*/n=0;/*把0賦值給n*/p1=p2=(structstudent*)malloc(LEN);/*開辟一個新單元*/printf("學號\t姓名\t性別\t年齡\t成績\n");/*輸出學號姓名性別年齡成績各占8個字節并換行*/scanf("%ld%s%c%d%f",&p1->no,&p1->name,&p1->sex,&p1->age,&p1->score);/*以長整型,字符串型,整型,浮點型分別輸入數據存到&stu->no,stu->name,&stu->sex,&stu->age,&stu->score中*/head=NULL;/*把NULL的值賦給head*/while(p1->no!=0)/*當p1->no不等于0*/{n=n+1;/*把n+1的值賦給n*/if(n==1)head=p1;/*如果n的值為1把p1賦給head*/else/*否則*/p2->next=p1;/*把p1的值賦給p2->next*/p2=p1;/*把p1賦給p2*/p1=(structstudent*)malloc(LEN);/*把malloc返回的值強制類型轉化為structstudent類型數據的指針變量再賦給p1*/printf("學號\t姓名\t性別\t年齡\t成績\n");/*輸出學號姓名性別年齡成績各占8個字節并換行*/scanf("%ld%s%c%d%f",&p1->no,&p1->name,&p1->sex,&p1->age,&p1->score);/*以長整型,字符串型,整型,浮點型分別輸入數據存到&stu->no,stu->name,&stu->sex,&stu->age,&stu->score中*/}p2->next=NULL;/*把NULL的值賦給p2->next*/return(head);/*返回head的值,返回了鏈表頭的指針*/}/*建立動態鏈表*/voidprint(structstudent*head)/*定義空類型的函數print(structstudent*head)*/{structstudent*p;/*定義指向structstudent的指針變量p*/printf("學號\t姓名\t性別\t年齡\t成績\n");/*輸出學號姓名性別年齡成績各占8個字節并換行*/p=head;/*把head的值賦給p*/if(head!=NULL)/*如果head不等于NULL*/do{printf("%-6ld\t%-6s\t%-4c\t%-5d\t%-6.2f\n",p->no,p->name,p->sex,p->age,p->score);/*輸出p->no,p->name,p->sex,p->age,p->score的結果并換行*/p=p->next;/*把p->next賦給p*/}while(p!=NULL);/*當p不等于NULL*/}structstudent*del(structstudent*head,longno)/*定義函數structstudent*del(structstudent*head,longno)*/{structstudent*p1,*p2;/*定義指向structstudent的指針變量p1,p2*/if(head==NULL)/*如果head等于NULL*/{printf("\n空\n");/*輸出換行并輸出"null"換行*/returnhead;}/*返回head的值,返回了鏈表頭的指針*/p1=head;/*把head賦給p1*/while(no!=p1->no&&p1->next!=NULL)/*當*p1指向的不是所要找的節點,且后面還有節點時*/{p2=p1;p1=p1->next;/*p1后移一個節點*/}if(no==p1->no)/*如果no不等于p1->no*/{if(p1==head)/*如果p1等于head把p1->next賦給head*/head=p1->next;/**p1若指向首節點,把第二各節點地址賦給head*/else/*否則*/p2->next=p1->next;/*將下一結點地址賦給前一節點*/n=n-1;/*把n-1的值賦給n*/}else/*否則*/printf("\n找不到相應學號\n");/*換行輸出"找不到相應學號"再換行*/return(head);/*返回head的值,返回了鏈表頭的指針*/}structstudent*insert(structstudent*head,structstudent*stud)/*定義函數structstudent*insert(structstudent*head,structstudent*stud)*/{structstudent*p0,*p1,*p2;/*定義指向structstudent的指針變量p0,p1,p2*/p1=head;/*使p1指向第一節點*/p0=stud;/*p0只想要插入的節點*/if(head==NULL)/*如果head的值為NULL的值*/{head=p0;p0->next=NULL;/*使p0指向的節點作為頭結點*/}else/*否則*/{ while((p0->no>p1->no)&&(p1->next!=NULL))/*當(p0->no大于p1->no)且(p1->next不等于NULL)*/{p2=p1;/*把p1的值賦給p2*/p1=p1->next;/*p1后移一個節點*/ }if(p0->no<=p1->no)/*如果p0->no小于等于p1->no*/{if(head==p1)head=p0;/*如果p1的值等于head把p0的值賦給head*/else/*否則*/p2->next=p0;/*把p0的值賦給p2->next*/p0->next=p1;/*把p1的值賦給p0->next*/ }else/*否則*/{p1->next=p0;/*把p0的值賦給p1->next*/p0->next=NULL;}/*把NULL賦給p0->next*/}n=n+1;/*把n+1的結果賦給n*/return(head);/*返回head的值,返回了鏈表頭的指針*/}voidmain()/*定義空類型主函數*/{structstudent*head,*stu;/*定義指向結構體students變量的指針變量*/longdel_num;/*定義長整型變量del_num*/printf("請輸入:\n");/*輸出"請輸入:"并換行*/head=creat();/*建立鏈表,返回頭指針*/print(head);/*調用函數print(head)*/printf("刪除學號為:");/*輸出"刪除學號為:"*/scanf("%ld",&del_num);
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 四川省南充市2025年中考英語真題附答案
- 2025年中國顆粒積木行業市場全景分析及前景機遇研判報告
- 2025年中國模塊電源行業發展潛力分析及投資方向研究報告
- 2025年中國馬飼料市場運行態勢及行業發展前景預測報告
- 泌尿外科??浦R
- 細化培訓課件
- 倉庫作業培訓課件
- 2025年 重慶兩江新區雁啟幼兒園招聘考試筆試試題附答案
- 2025-2031年中國農村網購行業市場全景監測及投資戰略咨詢報告
- 2025年中國烘手器市場運行態勢及行業發展前景預測報告
- 數據標注教學課件
- 涉密項目保密管理制度
- 東莞市招聘事業編制教職員筆試真題2024
- 小學數學老師德育論文
- CJ/T 303-2008穩壓補償式無負壓供水設備
- JG/T 346-2011合成樹脂裝飾瓦
- 腎性高血壓健康教育
- T/CAEPI 70-2023水泥窯協同處置生活垃圾焚燒飛灰水洗除鹽工藝技術要求
- 2025至2030年中國電梯能量回饋單元數據監測研究報告
- 2024年全國工會財務知識大賽備賽試題庫500(含答案)
- 四川省成都市青羊區2024-2025學年數學五下期末統考試題含答案
評論
0/150
提交評論