


下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、實驗十結(jié)構(gòu)體和共用體預備知識:(略)實驗程序:1.#includestructbb(intx;char*y;a=1,Pascal,2,Fortran;voidmain()即s=+(p-y);即c=*p-y,p+;即s=p-y,y+structbb*p=a;charc,*s;s=+p-y;/printf(%sn”,s);c=*p+-y;/printf(%cn,c);s=p-y+;/printf(%sn,s);printf(%sn,p-y);2#includeunioneeinta;intb;*p,s4;voidmain()intn=1,i;for(i=0;ia);printf(%dn,+p-a)
2、;3.#includestructstudent(intnum;charname12;floatscore3;floatave;voidmain()(structstudents2;inti,k;for(i=0;i2;i+)(scanf(%d”,&si.num);/輸入結(jié)束后回車getchar();/消耗緩沖區(qū)字符gets();/輸入結(jié)束后回車scanf(%f%f%f,&si.score0,&si.score1,&si.score2);si.ave=(si.score0+si.score1+si.score2)/3.0;k=0;for(i=1;i2;i+)(if(sk.avesi
3、.ave)k=i;printf(%d%s%f,sk.num,,sk.score0);printf(%f%f%fn”,sk.score1,sk.score2,sk.ave);4.#includestructdata(intbianhao;charname10;voidmain()(structdatab4=(1001,北京,(1002,天津,(1003,上海,(1004,重慶;structdata*p;p=b;5. for(p=b;pbianhao,p-name);按字母順序排序。#include#include#includestructproductcharname10;flo
4、atprice;voidinput(structproduct*p,intn)charstr10;inti;for(i=0;iname);printf(EnterthePriceofNo.%d:,i+1);gets(str);p-price=atof(str);voidoutput(structproducts)printf(%10st%10.2fn,,s.price);voidsort(structproduct*p,intn)structproducts;inti,j,k;for(i=0;in-1;i+)k=i;for(j=i+1;jname,(p+k)-name)0)k=j;
5、if(k!=i)s=*(p+i);*(p+i)=*(p+k);*(p+k)=s;voidmain()structproducts20;inti;input(s,10);sort(s,10);6. for(i=0;i10;i+)output(si);輸出最小值。#include#includevoidmain()(int*a,*b,*c,*min;a=(int*)malloc(sizeof(int);b=(int*)malloc(sizeof(int);c=(int*)malloc(sizeof(int);min=(int*)malloc(sizeof(int);scanf(%d%d%d”,a,
6、b,c);printf(輸入的3個數(shù)是:%d%d%dn,*a,*b,*c);*min=*a;if(*a*b)*min=*b;if(*min*c)*min=*c;printf(它們的最小值是:%dn,*min);7. free(a);free(b);free(c);free(min);對原題略著更改:有30個學生,每個學生的數(shù)據(jù)包括學號、姓名和3門課的成績,從鍵盤輸入每個學生的數(shù)據(jù)計算:每個學生的平均成績。計算30個學生每門課程的平均分。按學生平均分從代到高次序打印出每個學生的各課成績、3門課的平均成績。輸出每門課程的平均分。要求用input函數(shù)輸入,average1函數(shù)求每個學生3門課的平均分
7、,average2函數(shù)求30個學生每門課程的平均分,sort函數(shù)按學生平均分排序,output函數(shù)輸出總成績。#include#include#include#include#defineFsizeof(student)typedefstructscores(intenglish;intmath;intc_language;intall;TP;typedefstructstudents(charsid15;charname15;TPscore;structstudents*next;student;student*input()(student*head,*p1,*p2;intn=0;char
8、ch;/clrscr();head=(student*)malloc(F);head-next=NULL;do(n+;printf(nnPleaseinput%dstudentmessage:nn,n);printf(t%dstudentsid:,n);p1=(student*)malloc(F);p1-next=NULL;scanf(%s”,p1-sid);printf(nt%dstudentname:,n);scanf(%s,p1-name);printf(nt%dstudentscores(englesh,math,c_language):,n);scanf(%d,%d,%d”,&p1-
9、score.english,&p1-score.math,&p1-score.c_language);p1-score.all=p1-score.english+p1-score.math+p1-score.c_language;if(n=1)(head-next=p1;p2=p1;else(p2-next=p1;p2=p1;printf(nntttContinueorback(pressy/n):);ch=getchar();while(ch=y|ch=Y);returnhead;voidaverage1(student*head)(student*p;intj;/clrscr();p=he
10、ad-next;while(p)(j=p-score.all/3;printf(nnname:%staverage:%d,p-name,j);p=p-next;printf(nnnPressenykeyreturn.);getchar();voidaverage2(student*head)(student*p;intn=0,temp1=0,temp2=0,temp3=0;p=head-next;while(p)(temp1+=p-score.english;temp2+=p-score.math;temp3+=p-score.c_language;p=p-next;n+;printf(nna
11、verageenglishis:%dnaveragemathis:%dnaveragec_languageis:%dt”,temp1/n,temp2/n,temp3/n);student*sort(student*head)(student*head1,*p,*q,*r;inttemp1=0,temp2=0,temp3=0,temp4;chars15,n15;head1=head;for(p=head1-next;p-next!=NULL;p=p-next)(r=p;for(q=p-next;q;q=q-next)if(q-score.allr-score.all)r=q;if(r!=p)(s
12、trcpy(s,p-sid);strcpy(n,p-name);temp1=p-score.english;temp2=p-score.math;temp3=p-score.c_language;temp4=p-score.all;strcpy(p-sid,r-sid);strcpy(p-name,r-name);p-score.english=r-score.english;p-score.math=r-score.math;p-score.c_language=r-score.c_language;p-score.all=r-score.all;strcpy(r-sid,s);strcpy
13、(r-name,n);r-score.english=temp1;r-score.math=temp2;r-score.c_language=temp3;r-score.all=temp4;returnheadl;voidoutput(student*head)(student*head2,*p;inti=1;/clrscr();head2=sort(head);for(p=head2-next;p!=NULL;p=p-next)printf(nnname:%stsid:%stenglish:%dtmath:%dtc_language:%dtaverage:%dtmingci:%d”,p-na
14、me,p-sid,p-score.english,p-score.math,p-score.c_language,p-score.all/3,i+);average2(head);printf(nnnttPressenykeyback.);getchar();voidmain()(student*head,*p1,*p2;inti=0,j=1;head=input();do(/clrscr();printf(nn(1):average1.nn(2):average2.nn(3):sort.nn(4):output.nnnPleasechoose:);scanf(%d,&i);switch(i)
15、(case1:average1(head);break;case2:/clrscr();average2(head);printf(nnnPressenykeyretuen.);getchar();break;case3:/clrscr();p1=sort(head);for(p2=p1-next;p2!=NULL;p2=p2-next)printf(nttname:%stmingci:%d,p2-name,j+);printf(nnnPressenykeyback.);getchar();break;case4:output(head);break;default:printf(nYourc
16、hooseisnotright.);break;while(i!=-1);對原題略著更改:建立一個鏈表,每一個結(jié)點包括的成員為學生學號、平均成績。用malloc函數(shù)開辟新結(jié)點。要求鏈表包括8個結(jié)點,從鍵盤輸入結(jié)點的有效數(shù)據(jù)。要求用函數(shù)create來建立鏈表。實現(xiàn)下列操作:新增加一個學生的數(shù)據(jù)。這個新結(jié)點要求按學號順序插入。編寫一個函數(shù)insert來插入結(jié)點。刪除第五個結(jié)點,并從內(nèi)存中釋放。程序中要求驗證刪除的結(jié)點確已釋放。查找特定學生的信息。將鏈表結(jié)點數(shù)據(jù)輸出到屏幕上。#include#include#include#include#defineFsizeof(stu)typedefstruc
17、tstudentintsid;intaverage;structstudent*next;stu;stu*head;stu*create()stu*p1,*p2;intn=0;charch;head=(stu*)malloc(F);head-next=NULL;don+;printf(nnPleaseinput%dstudentmessage:nn,n);printf(t%dstudentsid:,n);p1=(stu*)malloc(F);p1-next=NULL;scanf(%d”,&p1-sid);printf(nt%dstudentaverage:,n);scanf(%d”,&p1-a
18、verage);if(n=1)head-next=p1;p2=p1;elsep2-next=p1;p2=p1;printf(nntttContinueorback(pressy/n):);ch=getch();while(ch=y|ch=Y);returnhead;stu*select(stu*head,intx)stu*s;s=head-next;while(s)(if(s-sid=x)break;s=s-next;returns;stu*insert(stu*head,intx,inty)(stu*p,*r;/,*q;/clrscr();p=head-next;r=(stu*)malloc
19、(sizeof(stu);r-sid=x;r-average=y;if(p=NULL)/*如果插入空表*/(p=r;r-next=NULL;printf(ninsertsuccess!);else(while(xp-sid)/*找到插入的位置,按學號大小。(找到位置或者到了表尾都會跳出循環(huán))*/(if(p-next=NULL)break;p=p-next;if(xsid)/*插到中間位置*/(r-sid=p-sid;r-average=p-average;p-sid=x;p-average=y;r-next=p-next;p-next=r;printf(ninsertsuccess!);els
20、eif(x=p-sid)/*學號不能相同*/printf(nError-yourinputthissamesid.);else/*插到末尾*/(p-next=r;r-next=NULL;printf(ninsertsuccess!);returnhead;stu*get(stu*head,intn)/*得到位置為n的結(jié)點的指針*/(stu*p;inti;p=head-next;if(n=0)returnhead;else(for(i=1;inext;returnp;stu*delete(stu*head,intsid)(stu*p,*q;inttemp=0,i=0;p=head-next;if
21、(!p)(printf(nlistisempty.pressenykeyback.”);getch();returnhead;/*表空*/else(while(p)/*查找學號為sid的結(jié)點的指針*/(i+;/*標記學號為sid的結(jié)點的位置*/if(p-sid=sid)(temp=1;break;/*temp=1標記找到了*/p=p-next;if(temp=1)/*如果有學號為sid的結(jié)點*/(q=get(head,i-1);/*得到sid的前一個結(jié)點的指針*/q-next=p-next;free(p);printf(nndeletesucess!);returnhead;else/*沒有找
22、到*/(printf(nnNOthisdata.n);returnhead;voidprint(stu*head)(stu*p;p=head-next;if(!p)(printf(nlistisempty.pressenykeyback.);getch();while(p)(printf(n%d:t%d”,p-sid,p-average);p=p-next;voidmain()(stu*p1;/*p2;charch1;intn,i=0,j=0;head=create();do(/clrscr();printf(n1.insert.);printf(n2.select.);printf(n3.delect.);printf(n4.printlist.);printf(n5.EXIT);printf(nchoice(1-5);ch1=getch();switch(ch1)(case1:(/clrscr();printf(npleaseinputinsertsid.andaverage(like1,1):);scanf(%d,%d,&i,&j);head=insert(head,i,j);printf(nnnPressenykeyback.);getch();break;case2:(/clrscr();printf(n
溫馨提示
- 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è)司機招聘及服務質(zhì)量保障協(xié)議
- 花椒種植與加工2025年度分紅合作合同
- 2025年度砂石運輸工程專項合同
- 二零二五年度商業(yè)街區(qū)停車位使用權轉(zhuǎn)讓及管理協(xié)議
- 二零二五年度施工安全培訓及應急預案協(xié)議
- 2025年度智能家居公寓裝修工程保修服務協(xié)議
- 2025年度旅游風景區(qū)特色鋪面經(jīng)營權轉(zhuǎn)讓協(xié)議
- 2025年度電子元器件抵押融資合同
- 二零二五年度美甲店形象設計與品牌推廣合同
- 二零二五年度汽車報廢注銷合同范本
- 15建設美麗中國【中職專用】高一思想政治《中國特色社會主義》(高教版2023基礎模塊)
- 人教版(2024)六年級全一冊 第17課 設計我的種植園
- 尊師重教講義
- 辦公用品及耗材采購服務投標方案(技術方案)
- 《十萬個為什么》整本閱讀指導(導讀)
- 2024年全國職業(yè)院校技能大賽高職組(智能節(jié)水系統(tǒng)設計與安裝賽項)考試題庫-下(多選、判斷題)
- (212題)2024綜合基礎知識考試題庫及解析
- 信息技術興趣小組活動記錄
- 第十二章目標識別課件
- 農(nóng)民田間學校規(guī)章制度
- 施工電梯基礎驗算
評論
0/150
提交評論