(完整版)C語(yǔ)言程序設(shè)計(jì)第四版第九章答案譚浩強(qiáng)_第1頁(yè)
(完整版)C語(yǔ)言程序設(shè)計(jì)第四版第九章答案譚浩強(qiáng)_第2頁(yè)
(完整版)C語(yǔ)言程序設(shè)計(jì)第四版第九章答案譚浩強(qiáng)_第3頁(yè)
(完整版)C語(yǔ)言程序設(shè)計(jì)第四版第九章答案譚浩強(qiáng)_第4頁(yè)
(完整版)C語(yǔ)言程序設(shè)計(jì)第四版第九章答案譚浩強(qiáng)_第5頁(yè)
已閱讀5頁(yè),還剩6頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

第九章9.1定義一個(gè)結(jié)構(gòu)體變量 (包括年、月、日)。計(jì)算該日在本年中是第幾天,

注意閏年問(wèn)題。解: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寫(xiě)一個(gè)函數(shù)

days,實(shí)現(xiàn)上面的計(jì)算。由主函數(shù)將年、月、日傳遞給

days

函數(shù),計(jì)算后將日數(shù)傳回主函數(shù)輸出。解: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編寫(xiě)一個(gè)函數(shù) print,打印一個(gè)學(xué)生的成績(jī)數(shù),該數(shù)組中有錄包括num、name、sore[3],用主函數(shù)輸入這些記錄,用解:

5個(gè)學(xué)生的數(shù)據(jù)記錄,每個(gè)記print 函數(shù)輸出這些記錄。#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在上題的基礎(chǔ)上,編寫(xiě)一個(gè)函數(shù)

input,

用來(lái)輸入

5個(gè)學(xué)生的數(shù)據(jù)記錄。解:#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個(gè)學(xué)生,每個(gè)學(xué)生的數(shù)據(jù)包括學(xué)號(hào)、姓名、 3門(mén)課的成績(jī),從鍵盤(pán)輸入 10個(gè)學(xué)生的數(shù)據(jù),要求打印出 3門(mén)課的總平均成績(jī),以及最高分的學(xué)生的數(shù)據(jù)(包括學(xué)號(hào)、姓名、 3門(mén)課成績(jī))解:#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編寫(xiě)一個(gè)函數(shù)new,對(duì)n個(gè)字符開(kāi)辟連續(xù)的存儲(chǔ)空間,此函數(shù)應(yīng)返回一個(gè)指針,指向字符串開(kāi)始的空間。New(n)表示分配n個(gè)字節(jié)的內(nèi)存空間。解:new函數(shù)如下:#defineNULL0#defineNEWSIZE1000charnewbuf[NEWSIZE];char*newp=newbuf;char*new(intn){if(newp+n<=newbuf+NEWSIZE){newp=newp+n;return(newp-n);}elsereturn(NULL);}9.7寫(xiě)一函數(shù) free,將上題用 new函數(shù)占用的空間釋放。 Free(p)表示將p指向的單元以后的內(nèi)存段釋放。解:#defineNullo#defineNEWSIZE1000charnewbuf[NEWSIZE];char*newp=newbuf;free(char*p){if((p>=newbuf)&&(p<newbuf+NEWSIZE))newp=p;}9.8已有a、b亮光鏈表,每個(gè)鏈表中的結(jié)點(diǎn)包括學(xué)好、成績(jī)。要求把兩個(gè)鏈表合并,按學(xué)號(hào)升序排列。解:#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個(gè)人圍成一圈,從第

1個(gè)人開(kāi)始順序報(bào)號(hào)

1、2、3。凡報(bào)到“

3”者退出圈子。找出最后留在圈子中的人原來(lái)的序號(hào)。解:#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“l(fā)astoneis”);for(I=1;ii<=N;I++)if(link[i].number)printf( “%3d”,lin[i].number);}9.10有兩個(gè)鏈表

a和

b,設(shè)結(jié)點(diǎn)中包含學(xué)號(hào)、姓名。從

1鏈表中刪去與

b鏈表中有相同學(xué)號(hào)的那些結(jié)點(diǎn)。解:#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( “l(fā)istan”:);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建立一個(gè)鏈表,每個(gè)結(jié)點(diǎn)包括:學(xué)號(hào)、姓名、性別、年齡。輸入一個(gè)年齡,如果鏈表中的結(jié)點(diǎn)所包含的年齡等于此年齡,則將此結(jié)點(diǎn)刪去。解:#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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論