




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第九章9.1定義一個結構體變量 (包括年、月、日)。計算該日在本年中是第幾天,
注意閏年問題。解:Struct{intyear;intmonth;intday;}date;main(){intdays;printf( “Inputyear,month,day: ”);scanf(“%d,%D,%d”,&date.year,&date.month,&date.day);switch(date.month){case1:days=date.day; break;case2:days=date.day+31; break;case3:days=date.day+59; break;case4:days=date.day+90; break;case5:days=date.day+120; break;case6:days=date.day+31; break;case7:days=date.day+181; break;case8:days=date.day+212; break;case9:days=date.day+243; break;case10:days=date.day+273; break;case11:days=date.day+304; break;case12:days=date.day+334; break;}if((date.year%4==0&&date.year%100!=0||date.year%400==0)&&date.month>=3)days+=1;printf(
n%d/%d“isthe%dthdayin%d.
”,date.month,data.day,days,date,year);}9.2寫一個函數
days,實現上面的計算。由主函數將年、月、日傳遞給
days
函數,計算后將日數傳回主函數輸出。解:structy_m_d{intyear:intmonth;intday;}date;intdays(structy_m_ddate1){intsum;switch(data.month){case1:sum=date1.day;case2:sum=date1.day+31;case3:sum=date1.day+59;case4:sum=date1.day+90;case5:sum=date1.day+120;case6:sum=date1.day+151;
break;break;break;break;break;break;case7:sum=date1.day+181; break;case8:sum=date1.day+212; break;case9:sum=date1.day+243; breakcase10:sum=date1.day+243; breakcase11:sum=date1.day+243; breakcase12:sum=date1.day+243; break}};9.3編寫一個函數 print,打印一個學生的成績數,該數組中有錄包括num、name、sore[3],用主函數輸入這些記錄,用解:
5個學生的數據記錄,每個記print 函數輸出這些記錄。#defineN5structstudent{charnum[6];charname[8];intscore[4];}stu[N];main(){intI,j;for(I=0;I<N;I++){printf(
“
n”,I+1);printf(
“no.:
”);scanf(
“%s”,stu[i].num);printf(
“name:”);scanf( “%s”,stu[i].name);for(j=0;j<3;j++){printf( “score%d:”j+1);scanf( “%d”,&stu[i].score[j]);}printf( n“”);}print(stu);}print(structstudentstu[6]){intI,j;printf( “%5s%10s”,stu[i].num,stu[i].name);for(j=0;j<3;j++)printf(
“%9d”,stu[i].score[j]);print(
“n”);}9.4在上題的基礎上,編寫一個函數
input,
用來輸入
5個學生的數據記錄。解:#defineN5structstudent{charnum[6];charname[8];intscore[4]}stu[N];input(structstudentstu[]){intI,j;for(I=0;I<N;I++){printf( “inputscoresofstudent n”,I+1);printf( “NO.:”);scanf( “%s”,stu[i].num);printf( “name: ”);scanf( “%s”,stu[i].name);for(j=0;j<3;j++){printf( “score%d:”,j++);scanf( “%d”,&stu[i].score[j]);}}printf( n“”);}}9.5有10個學生,每個學生的數據包括學號、姓名、 3門課的成績,從鍵盤輸入 10個學生的數據,要求打印出 3門課的總平均成績,以及最高分的學生的數據(包括學號、姓名、 3門課成績)解:#defineN10structstudent{charnum[6]charname[8]intscore[4]floatavr;}stu[N];main(){intI,j,max,maxi,sum;floataverage;for(I=0;I<N;I++){printf(
“
n”,I+1);printf( “NO.:”);scanf( “%s”,stu[i].num);printf( “name”);scanf( “%s”,stu[i].name);for(j=0;j<3;j++){printf( “score%d: ”,j+1);scanf( “%d”,&stu[i].score[j]);}}average=0;max=0;maxi=0;for(i=0;i<3;i++){sum=0;for(j=0;j<3;j++)sum+=stu[i].score[j];stu[i].avr=sum/3.0;average+=stu[i].avr;if(sum>max){max=sum;maxi=I;}}average/=N;printf( “NO.namescore1score2score3averagen”);for(I=0;I<N;I++){printf( “%5s%10s”,stu[i].num,stu[i].name);for(j=0;j<3;j++)printf(printf(
“%9d”,stu[i].score[j]);“%8.n2f”,stu[i].avr);}printf(printf(
“average=%6.2fn”,average);“Thehighestscoreis:%s,scoretotal:%d.
”stu[maxi].name,max);}9.6編寫一個函數new,對n個字符開辟連續的存儲空間,此函數應返回一個指針,指向字符串開始的空間。New(n)表示分配n個字節的內存空間。解:new函數如下:#defineNULL0#defineNEWSIZE1000charnewbuf[NEWSIZE];char*newp=newbuf;char*new(intn){if(newp+n<=newbuf+NEWSIZE){newp=newp+n;return(newp-n);}elsereturn(NULL);}9.7寫一函數 free,將上題用 new函數占用的空間釋放。 Free(p)表示將p指向的單元以后的內存段釋放。解:#defineNullo#defineNEWSIZE1000charnewbuf[NEWSIZE];char*newp=newbuf;free(char*p){if((p>=newbuf)&&(p<newbuf+NEWSIZE))newp=p;}9.8已有a、b亮光鏈表,每個鏈表中的結點包括學好、成績。要求把兩個鏈表合并,按學號升序排列。解:#include<stdio.h>#defineNULL0#defineLENsizeof(structstudent)strutstudent{longnum;intscor;structstudent*next};structstudentlistA,listB;intn,sum=0;main(){structstudent*creat(void);structstudent*insert(structstudent*,structstudent*);voidprint(structstudent*);stuctstudent*ahead,*bhead,*abh;printf( “ n”);ahead=creat();sum=sum+|n;abh=insert(ahead,bhead);print(abh);}structstudent*creat(void){structstudent*p1,*p2,*head;n=0;p1=p2=(structstudent*)malloc(LEN);printf( “inputnumber&scoresofstudent:n”);printf( “ifnumberIs0,stopinputing.n”);scanf( “%ld,%d”,&p1->num,&p1->score);head=NULL;while(p1->num!=0){n=n+1;if(n==1)head=p1;elsep2->next=p1;p2=p1;p1=(structstudent*)malloc(LEN);scanf( “%ld,,%d”,&p1->num,&p1->score);}p2->next=NULL;return(head);}structstudent*insert(structstudent*ah,structstudent*bh){structstudent*pa1,*pa2,*pb1,*pb2;pa2=pa1=ah;pb2=pb1=bh;do{while((pb1->num>pa1->num)&&(pa1->next!=NULL)){pa2=pa1;pa1=pa1->next;}if(pb->num<=pa1->num){if(ah=pa1)ah=pb1;elsepa2->next=pb1;pb1=pb1->next;pb2->next=pa1;pa2=pb2;pb2=pb1;}}while((pa1->next!=NULL)||(pa1==NULL&&pb1!=NULL));if((pb1->num>pa1->num)&&(pa1->next==NULl))ap1->next=pb1;return(ah);}voidprint(structstudent*head){structstudent*p;printf( “%ld%dn”,p->num,p->score);p=p->next;while(p!=NULL);}9.913個人圍成一圈,從第
1個人開始順序報號
1、2、3。凡報到“
3”者退出圈子。找出最后留在圈子中的人原來的序號。解:#defineN13structperson{intnumber;intnextop;}link[N+1];main(){intI,count,h;for(I=1;I<=N;I++){if(I==N)link[i].nextp=1;elselink[i].nextp=I+1;link[i].number=I;}printf( n“”);count=0;h=N;printf( “sequencethatperson2leavethecircle:n”);while(count<N-1){I=0;while(I!=3){h=link[h].nextp;if(link[h].number)I++;}printf( “%4d”,link[h].number);link[h].number=0;count++;}printf( \nThe“lastoneis”);for(I=1;ii<=N;I++)if(link[i].number)printf( “%3d”,lin[i].number);}9.10有兩個鏈表
a和
b,設結點中包含學號、姓名。從
1鏈表中刪去與
b鏈表中有相同學號的那些結點。解:#defineLA4#defineLB5#defineNULL0structstudent{charnump[6];charname[8];structstudent*next;}A[LA],b[LB];main(){structstudenta[LA]={{structstudentb[LB]={{
“101”,”Wang”},{“103”,”Zhang”},{
“102”,”LI”},{“104”,”Ma”},{
“105”,”zhang”},{ “106”,”“105”,”Chen”},{ “107”,”Gu{“108”,”Lui”}};intI,j;structstudent*p,*p1,*p2,*pt,*head1,*head2;head1=a;head2=b;printf( “listan”:);for(p1=head1,i=1;p1<a=LA;i++){p=p1;p1->next=a+I;printf( “%8s%8sn”,p1->num,p1->name);p1=p1->next;}p->next=NULL;printf( “ n”);for(p2=head2,I=1;p2<b+LB;I++){p=p2;p2->next=b+I;printf(“%8s%8sn”,p2->num,p2->name);p2=pa->next;}p->next=NULL;printf( n“”);p1=head1;while(p1!=NULL){p2=head2;while(p2!=NULL&&strcmp(p1->num,p2->num)!=0)p2=p2->next;if(strcmp(p1->num,p2->num==0))if(p1==head1)head1=p1->next;elsep->next=p1->next;p=p1;p1=p1->next;}p1=hedad1;printf{ “ n”};while(p1!=NULL){printf( “%7s%7sn”,p1->num,p1->name);p1=p1->next;}}9.11建立一個鏈表,每個結點包括:學號、姓名、性別、年齡。輸入一個年齡,如果鏈表中的結點所包含的年齡等于此年齡,則將此結點刪去。解:#defineNULL0#defineLENsizeof(structstudent)structstudent{charnum[6];charname[8];charsex[2];intage;stuctstudent*next;}stu[10];main(){structstudent*p,*pt,*head;intI,length,iage,flag=1;intfind=0;while(flag==1){printf(“inputlengthoflist (<10):”);scanf( “%d”,&length);if(length<10)flag=0;}for(I=0;I<lenth;I++){p=(structstudent*)malloc(LEN);if(I==0)head=pt=p;elsept->next=p;pt=p;ptintf( “NO:”);scanf( “%s”->num);,pprntf( “name:”);scanf( “%s”->name);,pprintf( “sex:”);scanf( “%s”->sex);,pprintf( “age:”);scanf( “%s”->age);,p}p->next=NULL;p=head;printf( “ n”);while(p!=NULL){printf( “%4s%8s%6s%6dn”,p->num,p->name,p->sex,p->age);p=p->next;}printf( “Inputage: ”);scanf( “%d”,&iage);pt=head;p=pt;if(pt->age==iage){p=pt->next;head=pt=p;find=1;}elsept=pt->next;while(pt!=NULL){if(pt->age==iage){p->next=pt->next;find=1;}elsep=pt;pt=pt->next;}if(!find)printf( “Notfound%d. ”,iage);p=head;printf( /r/
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 山東省青島市超銀中學2025年初三新課程第三次適應性測試語文試題含解析
- 天津市津南區咸水沽三中學重點達標名校2025年初三下學期第一次摸擬試物理試題含解析
- 山西省晉中學市靈石縣重點中學2025屆初三下學期第一次模擬(網考)考試化學試題含解析
- 天津商業大學《冰雪運動》2023-2024學年第二學期期末試卷
- 西安電力高等專科學校《醫療服務營銷學》2023-2024學年第二學期期末試卷
- 四川文化藝術學院《建筑施工組織及BIM應用》2023-2024學年第一學期期末試卷
- 山東省青島市西海岸新區6中重點達標名校2025年初三第一次質量預測化學試題含解析
- 三峽旅游職業技術學院《珠寶首飾設計基礎》2023-2024學年第二學期期末試卷
- 石家莊鐵道大學《城市規劃與設計》2023-2024學年第二學期期末試卷
- 西北工業大學《口腔頜面外科學實驗二》2023-2024學年第二學期期末試卷
- 口腔門診6S管理
- 沉浸式體驗活動設計合同
- 易制毒化學品銷售人員崗位職責
- 2025四川九洲建筑工程有限責任公司招聘生產經理等崗位6人筆試參考題庫附帶答案詳解
- 2025-2030中國金紅石發展現狀及未來趨勢研究報告
- 結腸鏡檢查前后的護理
- 人工智能與人才測評融合-全面剖析
- 小區二次供水水箱清洗消毒的監督流程課件
- 2024年江蘇淮安中考滿分作文《這份考卷答的漂亮》2
- 1《神州謠》公開課一等獎創新教學設計
- GB/T 6433-2025飼料中粗脂肪的測定
評論
0/150
提交評論