文章目录
- 1.定义一个结构体变量(包括年,月,日)。计算该日在本年中是第几天,注意闰年问题。
- 2.写一函数days,实现上一题的计算。由主函数将年,月,日传递给days函数,计算后将日子数传回主函数输出。
- 3. 编写一个print函数,输出一个学生的成绩数组,该数组中有5个学生的数据记录,每个记录包括num,name,score[3],用主函数输入这些记录,用print函数输出这些记录。
- 4.在第3题的基础上,编写一个input函数,用来输入5个学生的数据记录。
- 5.有10个学生,每个学生的数据包括学号,姓名,3门课程的成绩,从键盘输入10个学生数据,要求输出3门课程总平均成绩,以及最高分的学生的数据(包括学号,姓名,3门课程成绩,平均成绩)。
- 6.13个人围成一圈,从第一个人开始顺序报号1,2,3。凡报到3者退出圈子。找出最后留在圈子里的人原来的序号。要求用链表处理。
- 7.写一个函数del,用来删除动态链表中指定的结点。
- 8.写一个insert函数,用来向一个动态链表插入结点。
- 9.写四个函数,实现链表的建立,输出,删除和插入,在主函数种指定需要删除和插入的结点。
- 10 已有a,b两个链表,每个链表中的结点包括学号,成绩,要求把两个链表合并,按学号升序排列。
- 11.有两个链表a和b。设结点包括学号,姓名。从a链表中与b链表中有相同学号的点。
- 12.建立一个链表,每个结点包括:学号姓名,性别,年龄。输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将此结点删去。
1.定义一个结构体变量(包括年,月,日)。计算该日在本年中是第几天,注意闰年问题。
解法1
#include <stdio.h>
struct
{
int year;
int month;
int day;
}date;
int 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+151; 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%100!=0&&date.year%400&&date.month>=3)
days+=1;
printf("%d %d is the %dth day in %d .\n",date.month,date.day,days,date.year);
}
解法2
#include <stdio.h>
struct
{
int year;
int month;
int day;
}date;
int main()
{
int i,days;
int day_tab[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
printf("input year, month,day\n");
scanf("%d, %d, %d",&date.year,&date.month,&date.day);
days=0;
for(i=1;i<date.month;i++)
days+=day_tab[i];
days+=date.day;
if(date.year%4==0 &&date.year%100!=0&&date.year%400==0&&date.month>=3)
days+=1;
printf("%d %d is the %dth day in %d .\n",date.month,date.day,days,date.year);
return 0;
}
2.写一函数days,实现上一题的计算。由主函数将年,月,日传递给days函数,计算后将日子数传回主函数输出。
#include <stdio.h>
struct y_m_d
{
int year;
int month;
int day;
}date;
int main()
{
int days(struct y_m_d date1);
printf("input year, month,day\n");
scanf("%d, %d, %d",&date.year,&date.month,&date.day);
printf("%d %d is the %dth day in %d .\n",date.month,date.day,days,date.year);
return 0;
}
int days(struct y_m_d date1)
{int days;
switch(date1.month)
{
case 1: days=date1.day; break;
case 2: days=date1.day+31; break;
case 3: days=date1.day+59; break;
case 4: days=date1.day+90; break;
case 5: days=date1.day+120; break;
case 6: days=date1.day+151; break;
case 7: days=date1.day+181; break;
case 8: days=date1.day+212; break;
case 9: days=date1.day+243; break;
case 10: days=date1.day+273; break;
case 11: days=date1.day+304; break;
case 12: days=date1.day+334; break;
}
if(date1.year%4==0 &&date1.year%100!=0&&date1.year%400==0&&date1.month>=3)
days+=1;
return days;
}
3. 编写一个print函数,输出一个学生的成绩数组,该数组中有5个学生的数据记录,每个记录包括num,name,score[3],用主函数输入这些记录,用print函数输出这些记录。
#include <stdio.h>
#define N 5
struct Student
{
char name[8];
char num[6];
int score[4];
}stu[N];
int main()
{
void print(struct Student stu[5]);
int i,j;
for(i=0;i<N;i++)
{printf("please input name and number\n");
scanf("%s%s",stu[i].name,stu[i].num);
printf("please input four courses' score:");
for(j=0;j<4;j++)
scanf("%d",&stu[i].score[j]);
print(stu);
}
return 0;
}
void print(struct Student stu[5])
{
int i,j;
for(i=0;i<N;i++)
{
printf("%s %s\n",stu[i].name,stu[i].num);
for(j=0;j<5;j++)
printf("%5d%",stu[i].score[j]);
printf("\n");
}
}
4.在第3题的基础上,编写一个input函数,用来输入5个学生的数据记录。
#include <stdio.h>
#define N 5
struct Student
{
char name[8];
char num[6];
int score[4];
}stu[N];
int main()
{
void print(struct Student stu[5]);
void input(struct Student stu[5]);
input(stu);
print(stu);
return 0;
}
void input(struct Student stu[5])
{
int i,j;
for(i=0;i<N;i++)
{printf("please input name and number\n");
scanf("%s%s",stu[i].name,stu[i].num);
printf("please input four courses' score:");
for(j=0;j<4;j++)
scanf("%d",&stu[i].score[j]);
}
}
void print(struct Student stu[5])
{
int i,j;
for(i=0;i<N;i++)
{
printf("%s %s\n",stu[i].name,stu[i].num);
for(j=0;j<5;j++)
printf("%5d%",stu[i].score[j]);
printf("\n");
}
}
5.有10个学生,每个学生的数据包括学号,姓名,3门课程的成绩,从键盘输入10个学生数据,要求输出3门课程总平均成绩,以及最高分的学生的数据(包括学号,姓名,3门课程成绩,平均成绩)。
#include <stdio.h>
#define N 10
struct Student
{
char name[8];
char num[6];
float score[3];
float ave;
}stu[N];
int main()
{
int i,j,k;
float sum,max,av;
for(i=0;i<N;i++)
{
printf("please input student's name, number and three courses' score\n");
scanf("%s %s",stu[i].name,stu[i].num);
for(j=0;j<3;j++)
scanf("%5f",&stu[i].score[j]);
printf("\n");
}
av=0;
for(i=0;i<N;i++)
{
sum=0;
for(j=0;j<3;j++)
sum+=stu[i].score[j];
stu[i].ave=sum/3;
}
k=0;
for(i=0;i<N-1;i++)
if(stu[k].ave<stu[i].ave)
k=j;
printf("NO. name score1 score2 score3 average\n");
for(i=0;i<N;i++)
{
printf("%s %s ",stu[i].num,stu[i].name);
for(j=0;j<3;j++)
printf("%f ",stu[i].score[j]);
printf("%f\n",stu[i].ave);
}
printf("The highest score is:\n");
printf("NO. name score1 score2 score3 average\n");
printf("%s %s ",stu[k].num,stu[k].name);
for(j=0;j<3;j++)
printf("%f ",stu[k].score[j]);
printf("%f\n",stu[k].ave);
return 0;
}
6.13个人围成一圈,从第一个人开始顺序报号1,2,3。凡报到3者退出圈子。找出最后留在圈子里的人原来的序号。要求用链表处理。
#include <stdio.h>
#define N 13
struct Person
{
int num;
int nextp;
}link[N+1];
int main()
{
int count,h,i;
for(i=1;i<=N;i++)
{
if(i==N)
link[i].nextp=1;
else
link[i].nextp=i+1;
link[i].num=i;
}
printf("\n");
count=0;
h=N;
printf("sequence leave the circle\n");
while(count <N-1)
{
i=0;
while(i!=3)
{
h=link[h].nextp;
if(link[h].num=0)
i++;
}
printf("%4d",link[h].num);
link[h].num=0;
count++;
}
printf("\n The last one is");
for(i=0;i<=N;i++)
if(link[i].num)
printf("%3d",link[i].num);
printf("\n");
return 0;
}
7.写一个函数del,用来删除动态链表中指定的结点。
#include <stdio.h>
struct Student
{
long num;
float score;
struct Student *next;
};
int n;
struct Student* del(struct Student *head,long num)
{
struct Student* p,*q;
if(head==NULL)
{
printf("\nlist null\n");
return head;
}
p=head;
while(head->num!= num&& p->next!=NULL)
{
q=p;
p=p->next;
}
if(num==p->num)
{
if(p==head)
head->next=p;
else
q->next=p->next;
printf("delete:%d\n",num);
n=n-1;
}
else printf("%ld not been found\n",num);
return head;
}
8.写一个insert函数,用来向一个动态链表插入结点。
#include <stdio.h>
struct Student
{
long num;
float score;
struct Student *next;
};
int n;
struct Student *insert(struct Student *head,struct Student *stu)
{
struct Student *p,*q,*p0;
p=head;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{
while((p0->num>p->num)&&(p->next!=NULL))
{
q=p;
p=p->next;
}
if(p0->num<p->num)
{
if(head=p)
head=p0;
else
q->next=p0;
p0->next=p;
}
else{
p->next=p0;
p0->next=NULL;
}
}
n=n+1;
return head;
}
9.写四个函数,实现链表的建立,输出,删除和插入,在主函数种指定需要删除和插入的结点。
#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student* next;
};
int n;
int main()
{
struct student* creat();
struct student* del(struct student*,long);
struct student* insert(struct student*,struct student*);
void print(struct student *);
student * head,stu;
long del_num;
printf("input records:\n");
head=creat();
printf("input the deleted number:");
scanf("%ld",&del_num);
head=del(head,del_num);
print(head);
printf("input inserted record:");
scanf("%ld,%f",&stu.num,&stu.score);
head=insert(head,&stu);
print(head);
return 0;
}
struct student* creat()
{
struct student* head;
struct student*p1,*p2;
n=0;
p1=p2=(struct student*)malloc(LEN);
scanf("%ld,%f",&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("%lf,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return head;
}
struct student* del(struct student* head,long num)
{
struct student* p1,* p2;
p1=head;
if(head== NULL)
{
printf("\n list null\n");
return head;
}
while((num!=p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(p1==head)head=p1->next;
else
p2->next=p1->next;
printf("delete %ld\n",num);
n= n-1;
}
else printf("%ld not been found\n",num);
return head;
}
struct student* insert(struct student* head,struct student* stud)
{
struct student* p1,*p2,*p0;
p0=stud;
p1=head;
if(head==NULL)
{
head=stud;
stud->next=NULL;
}
else
{
while((p1->num<p0->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p1->num>=p0->num)
{
if(head==p1)
head=p0;
else
p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
n=n+1;
return head;
}
void print(struct student* head)
{
struct student *p;
p=head;
printf("\n Now,these %d records are:\n");
if(head!=NULL)
do
{
printf("%ld %5.1f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}
10 已有a,b两个链表,每个链表中的结点包括学号,成绩,要求把两个链表合并,按学号升序排列。
#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct student)
struct student
{
long num;
int score;
struct student *next;
};
struct student lista,listb;
int n,sum=0;
int main()
{
struct student* creat(void);
struct student* insert(struct student*,struct student*);
void print(struct student*);
struct student* ahead,*bhead,*abh;
printf("input list a:\n");
ahead=creat();
sum=sum+n;
printf("input list b:\n");
bhead=creat();
sum=sum+n;
abh=insert(ahead,bhead);
print(abh);
return 0;
}
struct student* creat(void)
{
struct student* head;
struct student* p1,*p2;
n =0;
p1=p2=(struct student*)malloc(LEN);
printf("please input number and score:\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;
pa1=pa2=ah;
pb1=pb2=bh;
do{
while((pb1->num>pa1->num)&&(pa1->next!=NULL))
{
pa2=pa1;
pa1=pa1->next;
}
if(pb1->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!=NULL)&&(pb1->num>pa1->num)&&(pa1->next==NULL))
pa1->next=pb1;
return ah;
}
void print(struct student*head)
{
struct student* p;
printf("There are %d records:\n",sum);
p=head;
if(p!=NULL)
do
{
printf("%ld%d\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}
11.有两个链表a和b。设结点包括学号,姓名。从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,"cheng"},{107,"guo"},{108,"liu"}};
int i;
struct student *p,*p1,*p2,*head1,*head2;
head1=a;
head2=b;
printf("list A:\n");
for(p1=head1,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("list B:\n");
for(p2=head2,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))
{
p2=p2->next;
}
if(p1->num=p2->num)
{
if(p1==head1)
head1=p1->next;
else
{
p->next=p1->next;
p1=p1->next;
}
}
else{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 <malloc.h>
#define LEN sizeof(struct student)
struct student
{
char num[6];
char name[8];
char sex[2];
int age;
struct student* next;
}stu[10];
int 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<length;i++)
{
p=(struct student*)malloc(LEN);
if(i==0)
pt=head=p;
else
pt->next=p;
pt=p;
printf("NO:");
scanf("%s",p->num);
printf("name:");
scanf("%s",p->name);
printf("sex:");
scanf("%s",p->sex);
printf("age:");
scanf("%d",&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("\nNo.name sex age\n");
while(p!=NULL)
{
printf("%4s%8s",p->num,p->name);
printf("%6s%6d",p->sex,p->age);
p=p->next;
}
return 0;
}