当前位置:网站首页>Exercise 9-5 address book sorting (20 points)

Exercise 9-5 address book sorting (20 points)

2022-07-04 09:42:00 skeet follower

Input n A friend's information , Including name, 、 Birthday 、 Phone number , This question requires the preparation of procedures , Output the address book in order of age . The title ensures that everyone's birthday is different .

Input format :

Enter the first line to give a positive integer n(<10). And then n That's ok , Each line follows “ full name Birthday Phone number ” Give a friend's information in the format of , among “ full name ” It's no longer than 10 A string of English letters ,“ Birthday ” yes yyyymmdd Format date ,“ Phone number ” No more than 17 Digit numbers and +- Composed string .

Output format :

Output your friends' information according to their age , The format is the same as that of output .

sample input :

3
zhang 19850403 13912345678
wang 19821020 +86-0571-88018448
qian 19840619 13609876543

sample output :

wang 19821020 +86-0571-88018448
qian 19840619 13609876543
zhang 19850403 13912345678

The code is as follows :

#include<stdio.h>
#include<string.h>
int main()
{
struct friends{// Create structure 
    char name[11];// full name 
    long int birth;// Birthday 
    char number[18];// Phone number 
};
    struct friends s1[10];// Variable ;
    int i,j,n;
    int g[100];// Create an array to exchange information later ;
    scanf("%d\n",&n);
    for(i=0;i<n;i++){
        scanf("%s %ld %s\n",s1[i].name,&s1[i].birth,s1[i].number);
    }
    for(i=0;i<n-1;i++){// Bubble sort compare size ;
        for(j=0;j<n-i-1;j++){
            if(s1[j].birth>s1[j+1].birth){
                // Exchange birthdays , full name , Phone number ;
                int temp=s1[j].birth;
                s1[j].birth=s1[j+1].birth;
                s1[j+1].birth=temp;
                strcpy(g,s1[j+1].name);
                strcpy(s1[j+1].name,s1[j].name);
                strcpy(s1[j].name,g);
                strcpy(g,s1[j+1].number);
                strcpy(s1[j+1].number,s1[j].number);
                strcpy(s1[j].number,g);
            }
        }
    }
    for(i=0;i<n;i++){
        printf("%s %ld %s\n",s1[i].name,s1[i].birth,s1[i].number);
    }
    return 0;
}

summary : This question mainly examines the structure , Bubble sort and library functions strcpy() Usage of ; If you are not proficient in bubble sorting, you can read this blog Ten classic sorting algorithms ( Dynamic diagram demonstration ) - A pixel - Blog Garden ;strcpy() usage c Language strcpy() usage _diyun The blog of -CSDN Blog _strcpy;

原网站

版权声明
本文为[skeet follower]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141423487929.html