这道题主要是对生日进行排序,上一次我将生日表示成了int型,这次试了一下字符串表示,也可以。
也就是对结构体数组里的字符串进行排序,我使用了qsort快排,发现过不去编译,还以为我的cmp函数有问题,于是各种查CSDN,后来发现是我的最后的输出语句少写了一些变量
所以说,做事情不到最后一刻不能放松,不然可能会有大问题。
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
struct friends{
char name[15];
char birth[10];
char tel[20];
};
int cmp( void const *a, void const *b){
return strcmp(((struct friends*)a)->birth, ((struct friends*)b)->birth);
}
int main()
{
int n, i;
scanf("%d", &n);
struct friends arr[n];
for ( i = 0; i < n; i++)
scanf("%s %s %s", arr[i].name, arr[i].birth, arr[i].tel);
qsort(arr, n, sizeof(struct friends), cmp);
for ( i = 0; i < n; i++)
printf("%s %s %s\n", arr[i].name, arr[i].birth, arr[i].tel);
return 0;
}