定义结构体变量,用它表示考生的学号、姓名(用英文表示)以及语文、数学、外语成绩。输入两个考生的信息和各门课成绩并比较(按总分进行高低排序;当总分相同时,依次按语、数、外的顺序排序)。
时间: 2023-04-24 12:04:09 浏览: 95
定义一个结构体变量,包含考生的学号、姓名以及语文、数学、外语成绩。可以使用以下代码:
struct student {
int id; // 学号
char name[20]; // 姓名
float chinese; // 语文成绩
float math; // 数学成绩
float english; // 外语成绩
float total; // 总分
};
然后,输入两个考生的信息和各门课成绩,并计算总分。可以使用以下代码:
struct student s1, s2;
scanf("%d %s %f %f %f", &s1.id, s1.name, &s1.chinese, &s1.math, &s1.english);
scanf("%d %s %f %f %f", &s2.id, s2.name, &s2.chinese, &s2.math, &s2.english);
s1.total = s1.chinese + s1.math + s1.english;
s2.total = s2.chinese + s2.math + s2.english;
接下来,按照总分进行高低排序。如果总分相同,则按照语、数、外的顺序排序。可以使用以下代码:
if (s1.total > s2.total) {
printf("%d %s %.1f %.1f %.1f %.1f\n", s1.id, s1.name, s1.chinese, s1.math, s1.english, s1.total);
printf("%d %s %.1f %.1f %.1f %.1f\n", s2.id, s2.name, s2.chinese, s2.math, s2.english, s2.total);
} else if (s1.total < s2.total) {
printf("%d %s %.1f %.1f %.1f %.1f\n", s2.id, s2.name, s2.chinese, s2.math, s2.english, s2.total);
printf("%d %s %.1f %.1f %.1f %.1f\n", s1.id, s1.name, s1.chinese, s1.math, s1.english, s1.total);
} else {
if (s1.chinese > s2.chinese) {
printf("%d %s %.1f %.1f %.1f %.1f\n", s1.id, s1.name, s1.chinese, s1.math, s1.english, s1.total);
printf("%d %s %.1f %.1f %.1f %.1f\n", s2.id, s2.name, s2.chinese, s2.math, s2.english, s2.total);
} else if (s1.chinese < s2.chinese) {
printf("%d %s %.1f %.1f %.1f %.1f\n", s2.id, s2.name, s2.chinese, s2.math, s2.english, s2.total);
printf("%d %s %.1f %.1f %.1f %.1f\n", s1.id, s1.name, s1.chinese, s1.math, s1.english, s1.total);
} else {
if (s1.math > s2.math) {
printf("%d %s %.1f %.1f %.1f %.1f\n", s1.id, s1.name, s1.chinese, s1.math, s1.english, s1.total);
printf("%d %s %.1f %.1f %.1f %.1f\n", s2.id, s2.name, s2.chinese, s2.math, s2.english, s2.total);
} else if (s1.math < s2.math) {
printf("%d %s %.1f %.1f %.1f %.1f\n", s2.id, s2.name, s2.chinese, s2.math, s2.english, s2.total);
printf("%d %s %.1f %.1f %.1f %.1f\n", s1.id, s1.name, s1.chinese, s1.math, s1.english, s1.total);
} else {
if (s1.english > s2.english) {
printf("%d %s %.1f %.1f %.1f %.1f\n", s1.id, s1.name, s1.chinese, s1.math, s1.english, s1.total);
printf("%d %s %.1f %.1f %.1f %.1f\n", s2.id, s2.name, s2.chinese, s2.math, s2.english, s2.total);
} else {
printf("%d %s %.1f %.1f %.1f %.1f\n", s2.id, s2.name, s2.chinese, s2.math, s2.english, s2.total);
printf("%d %s %.1f %.1f %.1f %.1f\n", s1.id, s1.name, s1.chinese, s1.math, s1.english, s1.total);
}
}
}
}
这段代码中,首先判断两个考生的总分大小关系,如果s1的总分大于s2的总分,则输出s1的信息在前,否则输出s2的信息在前。如果两个考生的总分相同,则按照语、数、外的顺序进行比较,输出信息。
阅读全文