ソートの連絡先(C言語)

N名前、生年月日、電話番号を含む、友人の情報を入力し、この質問は、出力接点の年齢の大きい順により、プログラミングを必要とします。トピックは、すべての誕生日が同じではないことを確認してください。

入力フォーマット:
最初の行の入力は、正の整数n(<10)で与えられます。そして、nは「名前」は、もはや「誕生日」からなるアルファベット文字列の10文字よりも「名前の誕生日、電話番号」の形式によれば、友人の所与の情報のライン「日付YYYYMMDD形式です電話番号は「17の以上の数値ではなく、+は、 -文字列からなります。

出力フォーマット:
年齢降順友人出力することにより、出力形式を持ちます。

サンプル入力:
3
張19850403 13912345678
王19821020 + 86-0571-88018448
銭19840619 13609876543

出力サンプル:
王19821020 + 86-0571-88018448
銭19840619 13609876543
張19850403 13912345678

#include <stdio.h>

struct colleague
{
    char name[11];
    int birth;
    char phone[18];
};

void sort_age(struct colleague *p, int n); 定义指向结构的指针

int main()
{
    struct colleague my_friend[10];
    int i, n;

    scanf("%d", &n);
    for (i = 0; i < n; i++)
        scanf("%s %d %s", my_friend[i].name, &my_friend[i].birth, my_friend[i].phone);
    sort_age(my_friend, n);

}

void sort_age(struct colleague *p, int n)
{
    struct colleague temp;
    int i, j;

    for (i = 0; i < n - 1; i++)    //冒泡排序
    {
        for (j = 0; j < n - 1 - i; j++)
        {
            if ((p+j)->birth > (p+j+1)->birth)
            {
                temp = *(p+j);				
                *(p+j) = *(p+j+1);
                *(p+j+1) = temp;
            }
        }
    }
    for (i = 0; i < n; i++)
        printf("%s %d %s\n", (p+i)->name, (p+i)->birth, (p+i)->phone);
}
公開された24元の記事 ウォンの賞賛0 ビュー154

おすすめ

転載: blog.csdn.net/qq_45624989/article/details/105084033