#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Student//声明结构体类型:struct student
{
char name[20];//以下四行为结构体的成员
double math;
double english;
double chinese;
};
voidinput_score(struct Student[]);//声明input_score函数
voidreorder(struct Student[]);//声明reoder函数
voidswitch_number(struct Student*, struct Student*);//声明swhich_number函数
intstudent_cmp(struct Student*, struct Student*);//声明student_cmp函数
voidstudnet_display(struct Student[]);//声明student_display函数
int main(){
struct Student stus[3];
input_score(stus);
studnet_display(stus);
reorder(stus);
studnet_display(stus);
}
voidinput_score(struct Student stu[]) {//定义input_score函数
int i;
for (i = 0;i <3;i++) {
printf("请输入第%d位同学的姓名:", i + 1);
scanf_s("%s", stu[i].name, 20);
printf("请输入第%d位同学的英语成绩:", i + 1);
scanf_s("%lf", &stu[i].english);
printf("请输入第%d位同学的数学成绩:", i + 1);
scanf_s("%lf", &stu[i].math);
printf("请输入第%d位同学的语文成绩:", i + 1);
scanf_s("%lf", &stu[i].chinese);
printf("\n");
}
}
intstudent_cmp(struct Student* stu1, struct Student*stu2) {//定义student_cmp函数
double sum1, sum2;
sum1= stu1->chinese + stu1->english + stu1->math;
sum2= stu2->chinese + stu2->english + stu2->math;
if (sum1 != sum2) {
return sum1 - sum2;
}
else if (stu1->math != stu2->math) {
return stu1->math - stu2->math;
}
else {
return stu1->chinese - stu2->chinese;
}
}
voidswitch_number(struct Student* stu1, struct Student* stu2) {//定义swhich_number函数
double ls;//定义中间变量
char sls[20];
ls= stu1->chinese;
stu1->chinese = stu2->chinese;
stu2->chinese = ls;
ls= stu1->math;
stu1->math = stu2->math;
stu2->math = ls;
ls= stu1->english;
stu1->english = stu2->english;
stu2->english = ls;
strcpy_s(sls,20, stu1->name);
strcpy_s(stu1->name, 20, stu2->name);
strcpy_s(stu2->name, 20, sls);
}
voidreorder(struct Student stu[]) {//定义reorder函数
int i, j;
for (i = 0;i <3;i++) {
for (j = i;j <3;j++) {
if (student_cmp(&stu[i], &stu[j]) < 0) {
switch_number(&stu[j], &stu[i]);
}
}
}
}
voidstudnet_display(struct Student stu[]) {//定义student_display函数
int i;
printf("%-20s%-20s%-20s%-20s\n", "姓名", "语文成绩", "英语成绩", "数学成绩");
for (i = 0;i <3;i++) {
printf("%-20s %-20.2f %-20.2f %-20.2f\n", stu[i].name, stu[i].chinese, stu[i].english, stu[i].math);//按要求输出学生的各科成绩
}
}