大家好,這篇文章分享了C程式設計(譚浩強)第五版第九章課後題答案,所有程式已經測試能夠正常運行,如果小伙伴發現有錯誤的的地方,歡迎留言告訴我,我會及時改正!感謝大家的觀看!!! ...
1.定義一個結構體變數(包括年、月、日)。計算該日在本年中是第幾天,註意閏年問題。
#include <stdio.h>
struct Date{
int year;
int month;
int day;
};
int main()
{
struct Date date;
int i=0,no=0;
int Month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
printf("請輸入日期(年、月、日):");
scanf("%d %d %d",&date.year,&date.month,&date.day);
no+=date.day;
for(i=0;i<date.month-1;i++)
no+=Month[i];
if(date.year%4==0 && date.year%100!=0 || date.year%400==0)
no+=1;
printf("%d月%d日是%d年的第%d天!\n",date.month,date.day,date.year,no);
return 0;
}
2.寫一個函數days,實現第1 題的計算。由主函數將年、月、日傳遞給days函數,計算後將日子數傳回主函數輸出。
#include <stdio.h>
struct Date{
int year;
int month;
int day;
};
int main()
{
struct Date date;
int i=0,no;
int days(struct Date date);
printf("請輸入日期(年、月、日):");
scanf("%d %d %d",&date.year,&date.month,&date.day);
no=days(date);
printf("%d月%d日是%d年的第%d天!\n",date.month,date.day,date.year,no);
return 0;
}
int days(struct Date date)
{
int i,no=0;
int Month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
no+=date.day;
for(i=0;i<date.month-1;i++)
no+=Month[i];
if(date.year%4==0 && date.year%100!=0 || date.year%400==0)
no+=1;
return no;
}
3.編寫一個函數print,列印一個學生的成績數組,該數組中有5個學生的數據記錄,每個記錄包括num,name,score[3],用主函數輸人這些記錄,用print函數輸出這些記錄。
#include <stdio.h>
#define M 20
struct student{
int num;
char name[M];
int score[3];
};
int main()
{
int i;
struct student stu[5];
void myprint(struct student stu[5]);
printf("請按以下格式依次輸入5名學生的信息:\n");
printf("num name score1 score2 score3\n");
for(i=0;i<5;i++)
scanf("%d %s %d %d %d",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
myprint(stu);
return 0;
}
void myprint(struct student stu[5])
{
int i;
printf("num name score1 score2 score3\n");
for(i=0;i<5;i++)
printf("%3d %3s %3d %3d %3d\n",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2]);
}
4.在第3題的基礎上,編寫一個函數input,用來輸人5個學生的數據記錄。
#include <stdio.h>
#define M 20
struct student{
int num;
char name[M];
int score[3];
};
int main()
{
struct student stu[5];
void myprint(struct student stu[5]);
void input(struct student stu[5]);
input(stu);
myprint(stu);
return 0;
}
void myprint(struct student stu[5])
{
int i;
printf("num name score1 score2 score3\n");
for(i=0;i<5;i++)
printf("%3d %3s %3d %3d %3d\n",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2]);
}
void input(struct student stu[5])
{
int i;
printf("請按以下格式依次輸入5名學生的信息:\n");
printf("num name score1 score2 score3\n");
for(i=0;i<5;i++)
scanf("%d %s %d %d %d",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
}
5.有10個學生,每個學生的數據包括學號、姓名、3門課程的成績,從鍵盤輸人10個學生數據,要求輸出3門課程總平均成績,以及最高分的學生的數據(包括學號、姓名、3門課程成績、平均分數)。
#include <stdio.h>
#define M 10
struct student{
int num;
char name[20];
int score[3];
};
int main()
{
struct student stu[M];
void myprint(struct student stu[M]);
void input(struct student stu[M]);
void max_aver(struct student stu[M]);
input(stu);
myprint(stu);
max_aver(stu);
return 0;
}
void myprint(struct student stu[M])
{
int i;
printf("num name score1 score2 score3\n");
for(i=0;i<M;i++)
printf("%3d %3s %3d %3d %3d\n",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2]);
}
void input(struct student stu[M])
{
int i;
printf("請按以下格式依次輸入%d名學生的信息:\n",M);
printf("num name score1 score2 score3\n");
for(i=0;i<M;i++)
scanf("%d %s %d %d %d",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
}
void max_aver(struct student stu[M])
{
int i,j,id,max;
float sum;
for(i=0;i<3;i++)
{
sum=0;
max=0;
for(j=0;j<M;j++)
{
sum+=stu[j].score[i];
if(max<stu[j].score[i])
{
max=stu[j].score[i];
id=j;
}
}
printf("第%d門課的平均分是%.2f\n",i+1,sum/M);
printf("第%d門課的最高分是%d\n",i+1,max);
printf("這名學生的信息如下:\n");
printf("num name score1 score2 score3\n");
printf("%3d %3s %3d %3d %3d\n",stu[id].num,stu[id].name,stu[id].score[0],stu[id].score[1],stu[id].score[2]);
printf("\n");
}
}
6.13個人圍成一圈,從第1個人開始順序報號1,2,3。凡報到3者退出圈子。找出最後留在圈子中的人原來的序號。要求用鏈表實現。
解題思路:
創建一個環形鏈表,給鏈表中的每一個節點從1~13編號,然後開始淘汰過程,對於淘汰的節點,序號置為0,淘汰完成之後,找到序號不為0的即為最後留下的。
#include <stdio.h>
#define M 13
struct people{
int num;
struct people *next;
};
int main()
{
int i,count;
struct people peo[M],*head;
head=peo;
for(i=0;i<M;i++)
{
head->num =i+1;
head->next=&peo[i+1];
head=head->next;
}
peo[M-1].next=peo; //最後一個元素指向第一個元素
i=0;
count=M;
head=peo;
while(count>1)
{
i++;
if(head->num==0)
{
head=head->next;
continue;
}
if(i==3)
{
printf("%d號被淘汰\n",head->num);
head->num=0;
count--;
i=0;
}
head=head->next;
}
printf("\n");
while(head->num==0)
{
head=head->next;
if(head->num!=0)
printf("最後留下的是%d號\n",head->num);
}
return 0;
}
7.在第9章例9.9和例9.10的基礎上,寫一個函數del,用來刪除動態鏈表中指定的節點
解題思路:
首先創建一個帶頭的單鏈表,然後讓用戶輸入需要刪除的節點,調用del函數,找到需要刪除的節點,把待刪除節點的前驅和後繼重新鏈接。
#include <stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct LNode)
struct LNode{
long num;
struct LNode *next;
};
int n;
int main()
{
long m;
struct LNode *pt;
struct LNode * creat();
void print(struct LNode * head);
void del(struct LNode * head,long m);
printf("建立鏈表(輸入0停止建立)\n");
pt=creat();
print(pt);
printf("要刪除的節點是: \n");
scanf("%ld",&m);
del(pt,m);
print(pt);
return 0;
}
struct LNode * creat()
{
struct LNode *head,*p1,*p2;
head=(struct LNode *)malloc(LEN);
n=0;
p1=p2=(struct LNode *)malloc(LEN);
printf("請輸入第1個數據:");
scanf("%ld",&p1->num);
printf("\n");
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct LNode *)malloc(LEN);
printf("請輸入第%d個數據:",n+1);
scanf("%ld",&p1->num);
printf("\n");
}
p2->next=NULL;
return(head);
}
void print(struct LNode * head)
{
struct LNode *p;
printf("鏈表的數據如下:\n");
p=head;
if(head!=NULL)
do
{
printf("%ld ",p->num);
p=p->next;
}while(p!=NULL);
printf("\n");
}
void del(struct LNode * head,long m)
{
struct LNode *p1,*p2;
if(head==NULL)
printf("list null!\n");
p1=head;
while(p1->num!=m && p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->num==m)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
n=n-1;
}
else
printf("%ld 未找到\n",m);
}
8.寫一個函數insert,用來向一個動態鏈表插入結點
#include <stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct LNode)
struct LNode{
long num;
struct LNode *next;
};
int n;
int main()
{
int insert_id,insert0;
struct LNode * creat(),*pt;
void print(struct LNode * head);
void insert(struct LNode * head,int insert_id,int insert0);
printf("建立鏈表(輸入0停止建立)\n");
pt=creat();
print(pt);
printf("請輸入要插入的位置以及數據:");
scanf("%d %d",&insert_id,&insert0);
insert(pt,insert_id,insert0);
print(pt);
return 0;
}
struct LNode * creat()
{
struct LNode *head,*p1,*p2;
head=(struct LNode *)malloc(LEN);
n=0;
p1=p2=(struct LNode *)malloc(LEN);
printf("請輸入第1個數據:");
scanf("%ld",&p1->num);
printf("\n");
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct LNode *)malloc(LEN);
printf("請輸入第%d個數據:",n+1);
scanf("%ld",&p1->num);
printf("\n");
}
p2->next=NULL;
return(head);
}
void print(struct LNode * head)
{
struct LNode *p;
printf("鏈表的數據如下:\n");
p=head;
if(head!=NULL)
do
{
printf("%ld ",p->num);
p=p->next;
}while(p!=NULL);
printf("\n");
}
void insert(struct LNode * head,int insert_id,int insert0)
{
struct LNode *p1,*p2;
p1=head;
insert_id-=1;
while(--insert_id)
{
p1=p1->next;
}
p2=(struct LNode *)malloc(LEN);
p2->num=insert0;
p2->next=p1->next;
p1->next=p2;
}
9.綜合本章例9.9(建立鏈表的函數creat)、例9.10(輸出鏈表的函數print)和本章習題第7題(刪除鏈表中結點的函數del)、第8題(插入結點的函數insert),再編寫一個主函數,先後調用這些函數。用以上5個函數組成一個程式,實現鏈表的建立、輸出、刪除和插入,在主函數中指定需要刪除和插人的結點的數據。
#include <stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct LNode)
struct LNode{
long num;
struct LNode *next;
};
int n;
int main()
{
char c;
int insert_id,insert0,m,flag=1;
struct LNode * creat(),*pt;
void print(struct LNode * head);
void insert(struct LNode * head,int insert_id,int insert0);
void del(struct LNode * head,long m);
pt=creat();
print(pt);
while(flag==1)
{
printf("插入數據(1) 刪除數據(2) 結束(3):");
getchar();
scanf("%c",&c);
if(c=='1')
{
printf("請輸入要插入的位置以及數據:");
scanf("%d %d",&insert_id,&insert0);
insert(pt,insert_id,insert0);
print(pt);
}
if(c=='2')
{
printf("請輸入要刪除的數據:");
scanf("%ld",&m);
del(pt,m);
print(pt);
}
if(c=='3')
flag=0;
}
print(pt);
return 0;
}
struct LNode * creat()
{
struct LNode *head,*p1,*p2;
head=(struct LNode *)malloc(LEN);
n=0;
p1=p2=(struct LNode *)malloc(LEN);
printf("請輸入第1個數據:");
scanf("%ld",&p1->num);
printf("\n");
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct LNode *)malloc(LEN);
printf("請輸入第%d個數據:",n+1);
scanf("%ld",&p1->num);
printf("\n");
}
p2->next=NULL;
return(head);
}
void print(struct LNode * head)
{
struct LNode *p;
printf("鏈表的數據如下:\n");
p=head;
if(head!=NULL)
do
{
printf("%ld ",p->num);
p=p->next;
}while(p!=NULL);
printf("\n");
}
void insert(struct LNode * head,int insert_id,int insert0)
{
struct LNode *p1,*p2;
p1=head;
insert_id-=1;
while(--insert_id)
{
p1=p1->next;
}
p2=(struct LNode *)malloc(LEN);
p2->num=insert0;
p2->next=p1->next;
p1->next=p2;
}
void del(struct LNode * head,long m)
{
struct LNode *p1,*p2;
if(head==NULL)
printf("list null!\n");
p1=head;
while(p1->num!=m && p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->num==m)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
n=n-1;
}
else
printf("%ld 未找到\n",m);
}
10.已有a,b兩個鏈表,每個鏈表中的結點包括學號、成績。要求把兩個鏈表合併, 按學號升序排列。
#include <stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
struct student{
int num;
double grade;
struct student *next;
};
int main()
{
int i;
struct student a[4]={{1,99},{2,98.5},{5,99.1},{6,99.2}};
struct student b[5]={{3,99.3},{4,99.4},{9,99.6},{7,99.7},{8,99.8}};
struct student *Merge( struct student *L1,struct student *L2 );
struct student *head1,*head2,*p1,*p2,*p;
head1=a;
head2=b;
printf("a鏈表的數據如下:\n");
p1=head1;
for(i=1;i<=4;i++)
{
if(i<4)
p1->next=a+i;
else
p1->next=NULL;
printf("%d %.2lf\n",p1->num,p1->grade);
if(i<4)
p1=p1->next;
}
printf("b鏈表的數據如下:\n");
p2=head2;
for(i=1;i<=5;i++)
{
if(i<5)
p2->next=b+i;
else
p2->next=NULL;
printf("%d %.2lf\n",p2->num,p2->grade);
if(i<5)
p2=p2->next;
}
p=Merge(head1,head2);
printf("排序後的鏈表如下:\n");
while(p!=NULL)
{
printf("%4d %7f \n",p->num,p->grade);
p=p->next;
}
return 0;
}
struct student *Merge(struct student *a,struct student *b)
{
int num;
double grade;
struct student *head,*pre;
head=a;
while(a->next!=NULL)
a=a->next;
a->next=b;
pre=head;
while(pre->next!=NULL)
{
a=pre->next;
while(a!=NULL)
{
if(pre->num > a->num)
{
num=pre->num;
grade=pre->grade;
pre->num=a->num;
pre->grade=a->grade;
a->num=num;
a->grade=grade;
}
a=a->next;
}
pre=pre->next;
}
return head;
}
11.有兩個鏈表a和b,設結點中包含學號、姓名。從a鏈表中刪去與b鏈表中有相同學號的那些結點。
解題思路:
對於b鏈表中的每一個節點,都從a鏈表的表頭開始查找,如果可以找到,直接刪除,如果找不到,繼續從a鏈表表頭找下一個b的節點。
#include <stdio.h>
#include <string.h>
#define LA 4
#define LB 5
struct student{
int num;
char name[8];
struct student *next;
} a[LA],b[LB];
int main()
{
struct student a[LA]={{101,"Wang"},{102,"Li"},{105,"Zhang"},{106,"Wei"}};
struct student b[LB]={{103,"Zhang"},{104,"Ma"},{105,"Chen"},{107,"Guo"},{108,"lui"}};
int i;
struct student *p,*p1,*p2,*head1,*head2;
head1=a;
head2=b;
printf(" list A: \n");
p1=head1;
for(i=1;i<=LA;i++)
{
if(i<LA)
p1->next=a+i;
else
p1->next=NULL;
printf("%4d%8s\n",p1->num,p1->name);
if(i<LA)
p1=p1->next;
}
printf("\n list B:\n");
p2=head2;
for(i=1;i<=LB;i++)
{
if(i<LB)
p2->next=b+i;
else
p2->next=NULL;
printf("%4d%8s\n",p2->num,p2->name);
if(i<LB)
p2=p2->next;
}
p1=head1;
while(p1!=NULL)
{
p2=head2;
while((p1->num != p2->num) && (p2->next!=NULL)) //p1與p2的值依次比較
p2=p2->next;
if(p1->num == p2->num) //p1=p2時
{
if(p1==head1)
head1=p1->next;
else
{
p->next=p1->next;
p1=p1->next;
}
}
else //比較到結束,p1與p2沒有相同的
{
p=p1;
p1=p1->next;
}
}
printf("\nresult:\n");
p1=head1;
while(p1!=NULL)
{
printf("%4d %7s \n",p1->num,p1->name);
p1=p1->next;
}
return 0;
}
12.建立一個鏈表,每個結點包括:學號、姓名、性別、年齡。輸入一個年齡,如果鏈表中的結點所包含的年齡等於此年齡,則將此結點刪去。
#include <stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
struct student{
int num;
char name[20];
char sex[6];
int age;
struct student *next;
};
int n;
int main()
{
int years;
struct student * creat(),*pt;
void print(struct student * head);
void del(struct student * head,int delage);
pt=creat();
print(pt);
printf("請輸入要刪除的年齡:");
scanf("%d",&years);
del(pt,years);
print(pt);
return 0;
}
struct student * creat()
{
struct student *head,*p1,*p2;
head=(struct student *)malloc(LEN);
n=0;
p1=p2=(struct student *)malloc(LEN);
printf("請按以下格式輸入每名學生的信息(輸入0 停止錄入):\n");
printf("num name sex age");
printf("請輸入第1個學生信息:");
scanf("%d%s%s%d",&p1->num,p1->name,p1->sex,&p1->age);
printf("\n");
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);
printf("請輸入第%d個同學信息:",n+1);
scanf("%d%s%s%d",&p1->num,p1->name,p1->sex,&p1->age);
printf("\n");
}
p2->next=NULL;
return(head);
}
void print(struct student * head)
{
struct student *p;
printf("鏈表的數據如下:\n");
p=head;
if(head!=NULL)
do
{
printf("%3d %3s %3s %3d\n",p->num,p->name,p->sex,p->age);
p=p->next;
}while(p!=NULL);
printf("\n");
}
void del(struct student * head,int delage)
{
struct student *p1,*p2;
if(head==NULL)
printf("list null!\n");
p1=head;
while(p1->age!=delage && p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->age==delage)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
}
else
printf("%d 未找到\n",delage);
}