c-moodle/homework/11966.c

54 lines
1.6 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
struct student {
int id, c;
float x, y, z;
float sum;
} stu[3];
/**
* 交换两个结构体中的内容
* a、b待交换的结构体指针
*/
void swap(struct student* a, struct student* b) {
struct student c;
c = *a;
*a = *b;
*b = c;
}
/**
* 对结构体数组进行冒泡排序
* p待排序的结构体数组指针
* n结构体数组长度
*/
void sort(struct student* p, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
if ((p + j)->c > (p + j + 1)->c || ((p + j)->c == (p + j + 1)->c && (p + j)->sum < (p + j + 1)->sum)) {
swap(p + j, p + j + 1);
}
}
}
}
int main() {
// 使用指针读取输入数据
scanf("%d,%d,%f,%f,%f", &((stu + 0)->c), &((stu + 0)->id), &((stu + 0)->x), &((stu + 0)->y), &((stu + 0)->z));
scanf("%d,%d,%f,%f,%f", &((stu + 1)->c), &((stu + 1)->id), &((stu + 1)->x), &((stu + 1)->y), &((stu + 1)->z));
scanf("%d,%d,%f,%f,%f", &((stu + 2)->c), &((stu + 2)->id), &((stu + 2)->x), &((stu + 2)->y), &((stu + 2)->z));
// 计算总成绩
(stu + 0)->sum = (stu + 0)->x + (stu + 0)->y + (stu + 0)->z;
(stu + 1)->sum = (stu + 1)->x + (stu + 1)->y + (stu + 1)->z;
(stu + 2)->sum = (stu + 2)->x + (stu + 2)->y + (stu + 2)->z;
sort(stu, 3);
// 使用指针输出结果
printf("%d,%d,%.1f\n", (stu + 0)->c, (stu + 0)->id, (stu + 0)->sum);
printf("%d,%d,%.1f\n", (stu + 1)->c, (stu + 1)->id, (stu + 1)->sum);
printf("%d,%d,%.1f\n", (stu + 2)->c, (stu + 2)->id, (stu + 2)->sum);
return 0;
}