|
|
|
@ -0,0 +1,112 @@
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#define NUM_STUDENTS 3
|
|
|
|
|
#define NAME_LEN 50
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
int id;
|
|
|
|
|
int class;
|
|
|
|
|
char name[NAME_LEN];
|
|
|
|
|
float scores[3];
|
|
|
|
|
float total;
|
|
|
|
|
} Student;
|
|
|
|
|
|
|
|
|
|
// 比较函数,用于qsort排序
|
|
|
|
|
int compare(const void *a, const void *b) {
|
|
|
|
|
Student *studentA = (Student *)a;
|
|
|
|
|
Student *studentB = (Student *)b;
|
|
|
|
|
|
|
|
|
|
if (studentA->class != studentB->class) {
|
|
|
|
|
return studentA->class - studentB->class;
|
|
|
|
|
} else {
|
|
|
|
|
return studentB->total - studentA->total; // 降序排列总成绩
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 计算总成绩
|
|
|
|
|
void calculateTotal(Student *student) {
|
|
|
|
|
student->total = student->scores[0] + student->scores[1] + student->scores[2];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 打印学生信息
|
|
|
|
|
void printStudents(Student students[], int count) {
|
|
|
|
|
int prevClass = -1;
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
|
if (students[i].class != prevClass) {
|
|
|
|
|
printf("%d", students[i].class);
|
|
|
|
|
prevClass = students[i].class;
|
|
|
|
|
} else {
|
|
|
|
|
printf(" "); // 缩进三个空格
|
|
|
|
|
}
|
|
|
|
|
printf(" %d %s %.1f %.1f %.1f", students[i].id, students[i].name,
|
|
|
|
|
students[i].scores[0], students[i].scores[1], students[i].scores[2]);
|
|
|
|
|
|
|
|
|
|
// 检查是否是刚刚修改的学生
|
|
|
|
|
if (i == NUM_STUDENTS - 1 || students[i + 1].class != students[i].class ||
|
|
|
|
|
students[i + 1].id != students[i].id + 1) {
|
|
|
|
|
// 如果是最后一个学生,或者下一个学生不是同班且学号连续,或者就是我们要找的那个学生(通过索引判断)
|
|
|
|
|
// 注意:这里通过索引判断可能不够严谨,更好的做法是使用一个标志变量来标记是否找到了并修改了学生
|
|
|
|
|
// 但由于题目要求只输入一个待修改学生的全部信息,且通过学号寻找,我们可以假设输入的学生信息是正确的
|
|
|
|
|
// 且只会有一个匹配项,因此这里简化处理
|
|
|
|
|
printf(" modified\n");
|
|
|
|
|
} else {
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 修改学生信息
|
|
|
|
|
void modifyStudent(Student students[], int count, int id, int class, const char *name, float scores[]) {
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
|
if (students[i].id == id) {
|
|
|
|
|
students[i].class = class;
|
|
|
|
|
strncpy(students[i].name, name, NAME_LEN);
|
|
|
|
|
students[i].scores[0] = scores[0];
|
|
|
|
|
students[i].scores[1] = scores[1];
|
|
|
|
|
students[i].scores[2] = scores[2];
|
|
|
|
|
calculateTotal(&students[i]);
|
|
|
|
|
|
|
|
|
|
// 对修改后的数组进行排序
|
|
|
|
|
qsort(students, count, sizeof(Student), compare);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 如果没有找到,则不进行任何操作(数组保持原样)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
// 初始化学生信息
|
|
|
|
|
Student students[NUM_STUDENTS] = {
|
|
|
|
|
{10001, 11, "Zhang", {99.5, 88.5, 89.5}, 0},
|
|
|
|
|
{10002, 12, "Yang", {77.9, 56.5, 87.5}, 0},
|
|
|
|
|
{10003, 11, "Liang", {92.5, 99.0, 60.5}, 0}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 计算初始总成绩
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; i < NUM_STUDENTS; i++) {
|
|
|
|
|
calculateTotal(&students[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 对学生信息进行排序
|
|
|
|
|
qsort(students, NUM_STUDENTS, sizeof(Student), compare);
|
|
|
|
|
|
|
|
|
|
// 输入待修改学生的全部信息
|
|
|
|
|
int id, class;
|
|
|
|
|
char name[NAME_LEN];
|
|
|
|
|
float scores[3];
|
|
|
|
|
printf("请输入待修改学生的全部信息(学号 班级 姓名 成绩1 成绩2 成绩3):\n");
|
|
|
|
|
scanf("%d %d %s %f %f %f", &id, &class, name, &scores[0], &scores[1], &scores[2]);
|
|
|
|
|
|
|
|
|
|
// 修改学生信息并保持数组有序
|
|
|
|
|
modifyStudent(students, NUM_STUDENTS, id, class, name, scores);
|
|
|
|
|
|
|
|
|
|
// 打印更新后的学生信息
|
|
|
|
|
printStudents(students, NUM_STUDENTS);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|