习题9-5 通讯录排序 (20分)
代码:
#include <stdio.h>
#include <stdlib.h>
struct friends {
char name[11];
int birth;
char number[18];
} Friends[10];
int main()
{
int i = 0, j = 0, z = 0, n = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) scanf("%s %d %s", &Friends[i].name, &Friends[i].birth, &Friends[i].number);
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (Friends[i].birth > Friends[j].birth) {
struct friends *frie = (struct friends *) malloc (sizeof (struct friends));
*frie = Friends[i];
Friends[i] = Friends[j];
Friends[j] = *frie;
}
}
}
for (i = 0; i < n; i++) printf("%s %d %s\n", Friends[i].name, Friends[i].birth, Friends[i].number);
return 0;
}