123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- #include <stdio.h>
- #include <string.h>
- #define max 50
- #define length 20
- struct Student {
- int num;
- char name[length];
- int age;
- };
- int compareByName(const void *a, const void *b) {
- struct Student *studentA = (struct Student *)a;
- struct Student *studentB = (struct Student *)b;
- return strcmp(studentA->name, studentB->name);
- }
- int compareByAge(const void *a, const void *b) {
- struct Student *studentA = (struct Student *)a;
- struct Student *studentB = (struct Student *)b;
- if (studentA->age != studentB->age) {
- return studentA->age - studentB->age;
- } else {
- return strcmp(studentA->name, studentB->name);
- }
- }
- int main() {
- int n,i;
- scanf("%d", &n);
-
- struct Student students[max];
-
- for (i = 0; i < n; i++) {
- scanf("%d%s%d", &students[i].num, students[i].name, &students[i].age);
- }
-
- qsort(students, n, sizeof(struct Student), compareByName);
-
- for (i = 0; i < n; i++) {
- printf("%3d %6s %3d\n", students[i].num, students[i].name, students[i].age);
- }
-
- printf("\n");
-
- qsort(students, n, sizeof(struct Student), compareByAge);
- for (i = 0; i < n; i++) {
- printf("%3d %6s %3d\n", students[i].num, students[i].name, students[i].age);
- }
-
- return 0;
- }
|