《C程序設(shè)計(jì)》譚浩強(qiáng)第四版課后答案下載
由譚浩強(qiáng)教授著、清華大學(xué)出版社出版的《C程序設(shè)計(jì)》是一本公認(rèn)的學(xué)習(xí)C語言程序設(shè)計(jì)的經(jīng)典教材。以下是由陽光網(wǎng)小編整理關(guān)于《C程序設(shè)計(jì)》譚浩強(qiáng)第四版課后答案下載地址,希望大家喜歡!
點(diǎn)擊進(jìn)入:《C程序設(shè)計(jì)》譚浩強(qiáng)第四版課后答案下載
《C程序設(shè)計(jì)》譚浩強(qiáng)第四版課后答案預(yù)覽
11.1定義一個結(jié)構(gòu)體變量(包括年、月、日)。計(jì)算該日在本年中是第幾天,注意閏年問題。
解:Struct
{int year;
int month;
int day;
}date;
main()
{int days;
printf(“Input year,month,day:”);
scanf(“%d,%D,%d”,&date.year,&date.month,&date.day);
switch(date.month)
{case 1: days=date.day; break;
case 2: days=date.day+31; break;
case 3: days=date.day+59; break;
case 4: days=date.day+90; break;
case 5: days=date.day+120; break;
case 6: days=date.day+31; break;
case 7: days=date.day+181; break;
case 8: days=date.day+212; break;
case 9: days=date.day+243; break;
case 10: days=date.day+273; break;
case 11: days=date.day+304; break;
case 12: 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 is the %dth day in%d.”,date.month,data.day,days,date,year);
}
11.2寫一個函數(shù)days,實(shí)現(xiàn)上面的計(jì)算。由主函數(shù)將年、月、日傳遞給days 函數(shù),計(jì)算后將日數(shù)傳回主函數(shù)輸出。
解:struct y_m_d
{int year:
int month;
int day;
}date;
intdays(struct y_m_d date1)
{int sum;
switch(data.month)
{case 1:sum=date1.day; break;
case 2:sum=date1.day+31; break;
case 3:sum=date1.day+59; break;
case 4:sum=date1.day+90; break;
case 5:sum=date1.day+120; break;
case 6:sum=date1.day+151; break;
case 7:sum=date1.day+181; break;
case 8:sum=date1.day+212; break;
case 9:sum=date1.day+243; break
case 10:sum=date1.day+243; break
case 11:sum=date1.day+243; break
case 12:sum=date1.day+243; break
}
};
11.3編寫一個函數(shù)print,打印一個學(xué)生的成績數(shù),該數(shù)組中有5個學(xué)生的數(shù)據(jù)記錄,每個記錄包括num、name、sore[3],用主函數(shù)輸入這些記錄,用print函數(shù)輸出這些記錄。
解:
#define N 5
struct student
{char num[6];
char name[8];
int score[4];
}stu[N];
main()
{int I,j ;
for(I=0;I<N;I++)
{printf(“\Input score of student %d:\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(struct student stu[6])
{int I,j;
printf(“%5s%10s”,stu[i].num,stu[i].name);
for(j=0;j<3;j++)
printf(“%9d”,stu[i].score[j]);
print(“\n”);
}
11.4在上題的基礎(chǔ)上,編寫一個函數(shù)input,用來輸入5個學(xué)生的數(shù)據(jù)記錄。
解:
#define N 5
struct student
{char num[6];
char name[8];
int score[4]
}stu[N];
input(struct student stu[])
{int I,j;
for(I=0;I<N;I++)
{printf(“input scores of student %d:\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”);
}
}
11.5 有10個學(xué)生,每個學(xué)生的數(shù)據(jù)包括學(xué)號、姓名、3門課的成績,從鍵盤輸入10個學(xué)生的數(shù)據(jù),要求打印出3門課的總平均成績,以及最高分的學(xué)生的數(shù)據(jù)(包括學(xué)號、姓名、3門課成績)
解:#define N 10
struct student
{char num[6]
char name[8]
int score[4]
float avr;
}stu[N];
main()
{int I,j,max,maxi,sum;
float average;
for(I=0;I<N;I++)
{printf(“\nInput scores of student %d:\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. name score1 score2 score3 average\n”);
for(I=0;I<N;I++)
{printf(“%5s%10s”,stu[i].num, stu[i].name);
for(j=0;j<3;j++)
printf(“%9d”,stu[i].score[j]);
printf(“%8.2f\n”,stu[i].avr);
}
printf(“average=%6.2f\n”,average);
printf(“The highest score is:%s,score total:%d.”stu[maxi].name,max);
}
11.6 編寫一個函數(shù)new,對n個字符開辟連續(xù)的存儲空間,此函數(shù)應(yīng)返回一個指針,指向字符串開始的空間。New(n)表示分配n個字節(jié)的內(nèi)存空間。
解:new函數(shù)如下:
#define NULL 0
#define NEWSIZE 1000
char newbuf[NEWSIZE];
char *newp=newbuf;
char *new(int n)
{if (newp+n<=newbuf+ NEWSIZE)
{ newp= newp+n;
return(newp-n);
}
else
return(NULL);
}
11.7寫一函數(shù)free,將上題用new函數(shù)占用的'空間釋放。Free(p)表示將p指向的單元以后的內(nèi)存段釋放。
解:
#define Null o
#define NEWSIZE 1000
char newbuf[NEWSIZE];
char *newp=newbuf;
free(char *p)
{if((p>=newbuf)&&(p<newbuf+NEWSIZE))
newp=p;
}
11.8已有a、b亮光鏈表,每個鏈表中的結(jié)點(diǎn)包括學(xué)好、成績。要求把兩個鏈表合并,按學(xué)號升序排列。
解:
#include<stdio.h>
#define NULL 0
#define LENsizeof(struct student)
strut student
{long num;
int scor;
struct student *next
};
struct student listA,listB;
int n,sum=0;
main()
{struct student *creat(void);
struct student *insert(struct student *,struct student *);
void print(struct student *);
stuct student *ahead , *bhead,*abh;
printf(“\ninput list a:\n”);
ahead=creat();
sum=sum+|n;
abh=insert(ahead,bhead);
print(abh);
}
struct student *creat(void)
{struct student *p1,*p2,*head;
n=0;
p1=p2=(struct student *)malloc(LEN);
printf(“input number&scores of student:\n”);
printf(“if number Is 0,stop inputing.\n”);
scanf(“%ld,%d”,&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1)head=p1;
else p2->next =p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf(“%ld,,%d”,&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
struct student *insert(struct student *ah,struct student *bh)
{struct student *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;
else pa2->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);
}
void print(struct student *head)
{struct student *p;
printf(“%ld%d\n”,p->num,p->score);
p=p->next;
while(p!=NULL);
}
11.9 13個人圍成一圈,從第1個人開始順序報(bào)號1、2、3。凡報(bào)到“3”者退出圈子。找出最后留在圈子中的人原來的序號。
解:
#define N 13
struct person
{int number;
int nextop;
}link[N+1];
main()
{int I,count,h;
for(I=1;I<=N;I++)
{if(I==N)
link[i].nextp=1;
else
link[i].nextp=I+1;
link[i].number=I;
}
printf(“\n”);
count=0;
h=N;
printf(“sequence that person2 leave the circle:\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 last one is”);
for(I=1;ii<=N;I++)
if(link[i].number)
printf(“%3d”,lin[i].number);
}
11.10有兩個鏈表a和b,設(shè)結(jié)點(diǎn)中包含學(xué)號、姓名。從1鏈表中刪去與b鏈表中有相同學(xué)號的那些結(jié)點(diǎn)。
解:
#define LA 4
#define LB 5
#define NULL 0
struct student
{char nump[6];
char name[8];
struct student *next;
}A[LA],b[LB];
main()
{struct student a[LA]={{“101”,”Wang”},{“102”,”LI”},{“105”,”zhang”},{“106”,”Wei”}};
struct studentb[LB]={{“103”,”Zhang”},{“104”,”Ma”},{“105”,”Chen”},{“107”,”Guo”},
{“108”,”Lui”}};
int I,j;
struct student *p, *p1,*p2,*pt,*head1,*head2;
head1=a;
head2=b;
printf(“list a :\n”);
for(p1=head1,i=1;p1<a=LA;i++)
{p=p1;
p1->next=a+I;
printf(“%8s%8s\n”,p1->num,p1->name);
p1=p1->next;
}
p->next=NULL;
printf(“\n list b:\n”);
for(p2=head2,I=1;p2<b+LB;I++)
{p=p2;
p2->next=b+I;
printf(“%8s%8s\n”,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;
else
p->next=p1->next;
p=p1;
p1=p1->next;
}
p1=hedad1;
printf{“\n result:\n”};
while(p1!=NULL)
{printf(“%7s %7s\n”,p1->num,p1->name);
p1=p1->next;
}
}
11.11建立一個鏈表,每個結(jié)點(diǎn)包括:學(xué)號、姓名、性別、年齡。輸入一個年齡,如果鏈表中的結(jié)點(diǎn)所包含的年齡等于此年齡,則將此結(jié)點(diǎn)刪去。
解:#define NULL 0
#define LEN sizeof(struct student)
struct student
{char num[6];
char name[8];
char sex[2];
int age;
stuct student *next;
}stu[10];
main()
{struct student *p,*pt,*head;
int I,length,iage,flag=1;
int find=0;
while(flag==1)
{printf(“input length of list(<10):”);
scanf(“%d”,&length);
if(length<10)
flag=0;
}
for(I=0;I<lenth;I++)
{p=(struct student *)malloc(LEN);
if(I==0)
head=pt=p;
else
pt->next=p;
pt=p;
ptintf(“NO:”);
scanf(“%s”,p->num);
prntf(“name:”);
scanf(“%s”,p->name);
printf(“sex:”);
scanf(“%s”,p->sex);
printf(“age:”);
scanf(“%s”,p->age);
}
p->next=NULL;
p=head;
printf(“\n NO. name sex age\n”);
while(p!=NULL)
{printf(“%4s%8s%6s%6d\n”,p->num, p->name, p->sex, p->age);
p=p->next;
}
printf(“Input age:”);
scanf(“%d”,&iage);
pt=head;
p=pt;
if(pt->age==iage)
{p=pt->next;
head=pt=p;
find=1;
}
else
pt=pt->next;
while(pt!=NULL)
{if(pt->age==iage)
{p->next=pt->next;
find=1;
}
else p=pt;
pt=pt->next;
}
if(!find)
printf(“Not found%d.”,iage);
p=head;
printf(“\n NO.name sex age\n”);
while(p!=NULL)
{
printf(“%4s%8s”,p->num,p->name);
printf(“%6s%6d”,p->sex,p->age);
p=p->next;
}
}
11.12將一個鏈表按逆序排列,即將鏈頭當(dāng)鏈尾,鏈尾當(dāng)鏈頭。
解:
# define NULL 0
struct stu
{int num;
struct stu *next;
}
main()
{int len=1l
struct stu *p1,*p2,*head,*new,*newhead;
p1=p2=head=(struct stu * )malloc(sizeof(strct stu));
printf(“input number(0:list end):”);
scanf(“%d”,&p1->num);
while(p1->num!=o)
{p1=(struct stu*)malloc(sizeof(struct stu));
printf(“input number(n:listend):”);
scanf(“%d”,&p1->num);
if(p1->num==0)
p2->next=null;
else
{p2=>next=p1;
p2=p1;
len++;
}
}
p1=head;
pritnf(“\n the original list:\n”);
do
{printf(“%4d”,p1->num);
if(p1->next!=NULL)
p1=p1->next;
}
while(p1->next!=NULL)
{p2=p1;
p1=p1->next;
}
if(I==0)
newhead=new=p1;
else
new=nes->next=p1;
p2->next=NULL;
}
printf(\n\n The new listL\n);
p1=newhead;
for(I=0l;I<len;I++)
{pritf(“4d,p1->num”);
p1=p1->next,
}
printf(“\n”);
【《C程序設(shè)計(jì)》譚浩強(qiáng)第四版課后答案下載】相關(guān)文章:
1.C程序設(shè)計(jì)第四版(譚浩強(qiáng)著)課后答案下載
2.《C程序設(shè)計(jì)》(譚浩強(qiáng)著)課后答案下載
3.C程序設(shè)計(jì)第三版(譚浩強(qiáng))課后答案-WORD版 免費(fèi)下載
4.《C++面向?qū)ο蟪绦蛟O(shè)計(jì)》譚浩強(qiáng)課后習(xí)題答案完整版免費(fèi)下載 清華
5.C++面向?qū)ο蟪绦蛟O(shè)計(jì)(譚浩強(qiáng)版)課后題答案詳解