#include <stdio.h>
#include <string.h>
//dai6kai
typedef struct {
char name;
int english;
int math;
} Student;
void swap_student(Student *data1, Student *data2) {
Student temp = *data1;
*data1 = *data2;
*data2 = temp;
}
void sort_english(Student data[]) {
for (int i = 0; i < 5 ; i++) {
for (int j = i + 1; j < 5; j++) {
if (data[i].english > data[j].english) {
swap_student(&data[i], &data[j]);
}
}
}
}
void sort_math(Student data[]) {
for (int i = 0; i < 5 ; i++) {
for (int j = i + 1; j < 5; j++) {
if (data[i].math < data[j].math) {
swap_student(&data[i], &data[j]);
}
}
}
}
void sort_name(Student data[]) {
for (int i = 0; i < 5 ; i++) {
for (int j = i + 1; j < 5; j++) {
if (data[i].name > data[j].name) {
swap_student(&data[i], &data[j]);
}
}
}
}
int main() {
Student data[5] = {
{'e', 60, 70},
{'b', 80, 75},
{'a', 90, 72},
{'c', 70, 60},
{'d', 50, 56}
};
sort_english(data);
for (int i = 0; i < 5; i++) {
printf("name: %c, english: %d, math: %d\n", data
[i
].
name, data
[i
].
english, data
[i
].
math); }
sort_math(data);
for (int i = 0; i < 5; i++) {
printf("name: %c, english: %d, math: %d\n", data
[i
].
name, data
[i
].
english, data
[i
].
math); }
sort_name(data);
for (int i = 0; i < 5; i++) {
printf("name: %c, english: %d, math: %d\n", data
[i
].
name, data
[i
].
english, data
[i
].
math); }
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KIC8vZGFpNmthaQp0eXBlZGVmIHN0cnVjdCB7CiAgICBjaGFyIG5hbWU7ICAKICAgIGludCBlbmdsaXNoOwogICAgaW50IG1hdGg7Cn0gU3R1ZGVudDsKIAp2b2lkIHN3YXBfc3R1ZGVudChTdHVkZW50ICpkYXRhMSwgU3R1ZGVudCAqZGF0YTIpIHsKICAgIFN0dWRlbnQgdGVtcCA9ICpkYXRhMTsKICAgICpkYXRhMSA9ICpkYXRhMjsKICAgICpkYXRhMiA9IHRlbXA7Cn0KIAp2b2lkIHNvcnRfZW5nbGlzaChTdHVkZW50IGRhdGFbXSkgewogICAgZm9yIChpbnQgaSA9IDA7IGkgPCA1IDsgaSsrKSB7CiAgICAgICAgZm9yIChpbnQgaiA9IGkgKyAxOyBqIDwgNTsgaisrKSB7CiAgICAgICAgICAgIGlmIChkYXRhW2ldLmVuZ2xpc2ggPiBkYXRhW2pdLmVuZ2xpc2gpIHsKICAgICAgICAgICAgICAgIHN3YXBfc3R1ZGVudCgmZGF0YVtpXSwgJmRhdGFbal0pOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9CiAKdm9pZCBzb3J0X21hdGgoU3R1ZGVudCBkYXRhW10pIHsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgNSA7IGkrKykgewogICAgICAgIGZvciAoaW50IGogPSBpICsgMTsgaiA8IDU7IGorKykgewogICAgICAgICAgICBpZiAoZGF0YVtpXS5tYXRoIDwgZGF0YVtqXS5tYXRoKSB7ICAKICAgICAgICAgICAgICAgIHN3YXBfc3R1ZGVudCgmZGF0YVtpXSwgJmRhdGFbal0pOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9CiAKdm9pZCBzb3J0X25hbWUoU3R1ZGVudCBkYXRhW10pIHsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgNSA7IGkrKykgewogICAgICAgIGZvciAoaW50IGogPSBpICsgMTsgaiA8IDU7IGorKykgewogICAgICAgICAgICBpZiAoZGF0YVtpXS5uYW1lID4gZGF0YVtqXS5uYW1lKSB7ICAKICAgICAgICAgICAgICAgIHN3YXBfc3R1ZGVudCgmZGF0YVtpXSwgJmRhdGFbal0pOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9CiAKaW50IG1haW4oKSB7CiAKICAgIFN0dWRlbnQgZGF0YVs1XSA9IHsKICAgICAgICB7J2UnLCA2MCwgNzB9LAogICAgICAgIHsnYicsIDgwLCA3NX0sCiAgICAgICAgeydhJywgOTAsIDcyfSwKICAgICAgICB7J2MnLCA3MCwgNjB9LAogICAgICAgIHsnZCcsIDUwLCA1Nn0KICAgIH07CiAKICAgIHByaW50Zigic29ydF9lbmdsaXNoXG4iKTsKICAgIHNvcnRfZW5nbGlzaChkYXRhKTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgNTsgaSsrKSB7CiAgICAgICAgcHJpbnRmKCJuYW1lOiAlYywgZW5nbGlzaDogJWQsIG1hdGg6ICVkXG4iLCBkYXRhW2ldLm5hbWUsIGRhdGFbaV0uZW5nbGlzaCwgZGF0YVtpXS5tYXRoKTsKICAgIH0KIAogICAgcHJpbnRmKCJcbnNvcnRfbWF0aFxuIik7CiAgICBzb3J0X21hdGgoZGF0YSk7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IDU7IGkrKykgewogICAgICAgIHByaW50ZigibmFtZTogJWMsIGVuZ2xpc2g6ICVkLCBtYXRoOiAlZFxuIiwgZGF0YVtpXS5uYW1lLCBkYXRhW2ldLmVuZ2xpc2gsIGRhdGFbaV0ubWF0aCk7CiAgICB9CiAKICAgIHByaW50ZigiXG5zb3J0X25hbWVcbiIpOwogICAgc29ydF9uYW1lKGRhdGEpOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCA1OyBpKyspIHsKICAgICAgICBwcmludGYoIm5hbWU6ICVjLCBlbmdsaXNoOiAlZCwgbWF0aDogJWRcbiIsIGRhdGFbaV0ubmFtZSwgZGF0YVtpXS5lbmdsaXNoLCBkYXRhW2ldLm1hdGgpOwogICAgfQogCiAgICByZXR1cm4gMDsKfQ==
sort_english
name: d, english: 50, math: 56
name: e, english: 60, math: 70
name: c, english: 70, math: 60
name: b, english: 80, math: 75
name: a, english: 90, math: 72
sort_math
name: b, english: 80, math: 75
name: a, english: 90, math: 72
name: e, english: 60, math: 70
name: c, english: 70, math: 60
name: d, english: 50, math: 56
sort_name
name: a, english: 90, math: 72
name: b, english: 80, math: 75
name: c, english: 70, math: 60
name: d, english: 50, math: 56
name: e, english: 60, math: 70