第九章课后编程题

1:定义一个结构体变量(包括年,月,日)。计算该日在本年中是第几天注意闰年的问题

#include <stdio.h>
#include <math.h>
struct Date{
    int y;
    int m;
    int d;
} date;
//判断是否是闰年 
int IsYear(int year){
    if((year%4==0&&year%100!=0)||year%400==0)
      return 1;
    else return 0; 
}
//判断是一年中的第几天 
int Days(){
    int days [13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    if(IsYear(date.y)==1)
        days[2]=29;
    else days[2]=28;
    int i,sum=0;
    for(i=1;i<date.m;i++) sum+=days[i];
    sum+=date.d;
    return sum; 
}
int main(){
    printf("请输入一个日期:\n");
    scanf("%d/%d/%d",&date.y,&date.m,&date.d);
    int sum=Days();
    printf("%d/%d/%d是一年中的第%d天\n",date.y,date.m,date.d,sum);
    return 0;
} 
View Code

 

2:定义一个结构体变量(包括年,月,日)。计算该日在本年中是第几天。注意闰年的问题 

写一个函数days(),由主函数将年月日传递给days函数,计算后将日子数传回主函数输出

#include <stdio.h>
#include <math.h>
struct Date{
    int y;
    int m;
    int d;
};
//判断是否是闰年 
int IsYear(int year){
    if((year%4==0&&year%100!=0)||year%400==0)
      return 1;
    else return 0; 
}
//判断是一年中的第几天 
int days(struct Date date){
    int table [13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    if(IsYear(date.y)==1)
        table[2]=29;
    else table[2]=28;
    int i,sum=0;
    for(i=1;i<date.m;i++) sum+=table[i];
    sum+=date.d;
    return sum; 
}
int main(){
    printf("请输入一个日期:\n");
    struct Date date;
    scanf("%d/%d/%d",&date.y,&date.m,&date.d);
    int sum=days(date);
    printf("%d/%d/%d是一年中的第%d天\n",date.y,date.m,date.d,sum);
    return 0;
} 
View Code

 

3-4:编写一个函数print,打印一个学生 的成绩数组,该数组中有5个学生的数据记录,每个记录包括num,name,score[3]

用主函数输入这些记录,用print函数输出这些记录。编写一个函数input,用来输入5个学生的数据记录

#include <stdio.h>
#include <math.h>
#define N 5 
struct student{
    int num;
    char name[20];
    double score[3];
}stu[N];
void input(){
    int i,j;
    printf("请输入5个学生的信息:\n");
    for(i=0;i<N;i++){
        scanf("%d%s",&stu[i].num,stu[i].name);
        for(j=0;j<3;j++){
            scanf("%d",&stu[i].score[j]);
        }
    }
}
void print(){
    int i,j;
    printf("学生的信息如下:\n");
    for(i=0;i<N;i++){
        printf("%d\t%s\t",stu[i].num,stu[i].name);
        for(j=0;j<3;j++){
            printf("%d\t",stu[i].score[j]);
        }
        printf("\n");
    }
}
int main(){
    input();
    print();
    return 0;
} 
View Code

 

5:有10个学生,每个学生的数据包括学号,姓名,3门课程的成绩,从键盘输入10个学生数据,要求输出3门课程总平均成绩,以及最高分的学生的数据。

#include <stdio.h>
#include <math.h>
#define N 10
struct student{
    int num;
    char name[20];
    double score[3];
    double sum;//总分
    double avg;//平均分 
}stu[N];
void input();
void showInfo();
void BestStu();
int main(){
    input();
    showInfo();
    BestStu();
    return 0;
}
void BestStu(){
    printf("最高分的学生信息:\n");
    int i,j,k;
    for(i=0,j=0;i<N;i++){
        if(stu[j].sum<stu[i].sum) j=i; 
    }
    printf("%d\t%s\t",stu[j].num,stu[j].name);
    for(i=0;i<3;i++){
        printf("%lf\t",stu[j].score[i]);
    }
    printf("%lf\t%lf\n",stu[i].sum,stu[i].avg);
}
void input(){
    int i,j;
    printf("请输入10个学生的信息:\n");
    double sum; 
    for(i=0;i<N;i++){
        sum=0.0;
        scanf("%d%s",&stu[i].num,stu[i].name);
        for(j=0;j<3;j++){
            scanf("%d",&stu[i].score[j]);
            sum+=stu[i].score[j];
        }
        stu[i].sum=sum;
        stu[i].avg=sum/3;
    }
}
//三门课的总分平均分信息如下 
void showInfo(){
    int i,j;
    printf("学生的信息如下:\n");
    for(i=0;i<N;i++){
        printf("%d\t%s\t%lf\t%lf\n",stu[i].num,stu[i].name,stu[i].sum,stu[i].avg);
    }
}
View Code

 6:13个人围成一圈,从第一个人开始顺序报号1,2,3。凡报到3者退出圈子。找出最后留在圈子中的人原来的序号。要求用链表处理。

#include <stdio.h>
#include <math.h>
#define N 13
struct person{
    int number;
    int nextp;
}link[N+1];
int 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;
    }
    count=0;
    h=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("\n");
    printf("最后一个离开的是:\n");
    for(i=1;i<=N;i++)
      if(link[i].number)
        printf("%3d",link[i].number);
    printf("\n");
    return 0;
} 
View Code

 7已知有a,b两个链表,每个链表中的结点包括学号,成绩。要求把两个链表合并,按学号升序排列

 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <malloc.h>
 4 #define LEN sizeof(struct student)
 5 struct student{
 6     long num;
 7     int score;
 8     struct student *next;
 9 };
10 struct student *lista,*listb;
11 int n,sum=0;
12 struct student *creat(){
13     struct student *p1,*p2,*head;
14     n=0;
15     p1=p2=(struct student*)malloc(LEN);
16     printf("input number&score of student:\n");
17     printf("if number is 0,stop inputing.\n");
18     scanf("%ld%ld",&p1->num,&p1->score);
19     head=NULL;
20     while(p1->num!=0){
21         n=n+1;
22         if(n==1) head=p1;
23         else p2->next=p1;
24         p2=p1;
25         p1=(struct student *)malloc(LEN);
26         scanf("%ld%ld",&p1->num,&p1->score);
27     }
28     p2->next=NULL;
29     return head;
30 }
31 //定义insert函数,用来合并两个链表
32 struct student *insert(struct student *ah,struct student *bh){
33     struct student *pa1,*pa2,*pb1,*pb2;
34     pa2=pa1=ah;
35     pb2=pb1=bh;
36     do{
37         while((pb1->num>pa1->num)&&(pa1->next!=NULL)){
38             pa2=pa1;
39             pa1=pa1->next;
40         }
41         if(pb1->num<=pa1->num){
42             if(ah==pa1) ah=pb1;
43             else pa2->next=pb1;
44             pb1=pb1->next;
45             pb2->next=pa1;
46             pa2=pb2;
47             pb2=pb1;
48         }
49     }while((pa1->next!=NULL)||(pa1==NULL&&pb1!=NULL));
50     if((pb1!=NULL)&&(pb1->num>pa1->num)&&(pa1->next==NULL))
51        pa1->next=pb1;
52     return ah;
53 }
54 //输出函数
55 void print(struct student *head){
56     struct student *p;
57     printf("There are %dcrecords:\n",sum);
58     p=head;
59     if(p!=NULL)
60     do{
61         printf("%ld %ld\n",p->num,p->score);
62         p=p->next;
63     }while(p!=NULL); 
64 }
65 int main(){
66     struct student *ahead,*bhead,*abh;
67     printf("input list a:\n");
68     ahead=creat();
69     sum=sum+n;
70     printf("input list b:\n");
71     bhead=creat();
72     sum=sum+n;
73     abh=insert(ahead,bhead);
74     print(abh);
75     return 0;
76 }
View Code

 

posted @ 2020-10-15 11:04  薄眠抛却陈年事。  阅读(220)  评论(0编辑  收藏  举报