You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.2 KiB

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
struct Student {
int student_id;
int class_id;
char name[50];
float math_score;
float physics_score;
float english_score;
float total_score;
};
struct Student students[3] = {
{10001, 11, "Zhang", 99.5, 88.5, 89.5},
{10002, 12, "Yang", 77.9, 56.5, 87.5},
{10003, 11, "Liang", 92.5, 99.0, 60.5}
};
void updateStudent(int id, int new_id, int class_id, char new_name[], float score1, float score2, float score3) {
int index = -1;
for (int i = 0; i < 3; i++) {
if (students[i].student_id == id) {
index = i;
break;
}
}
if (index != -1) {
students[index].student_id = new_id;
students[index].class_id = class_id;
strcpy(students[index].name, new_name);
students[index].math_score = score1;
students[index].physics_score = score2;
students[index].english_score = score3;
students[index].total_score = score1 + score2 + score3;
for (int i = 0; i < 3; i++) {
for (int j = i + 1; j < 3; j++) {
if (students[i].class_id > students[j].class_id ||
(students[i].class_id == students[j].class_id && students[i].total_score < students[j].total_score)) {
struct Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
for (int i = 0; i < 3; i++) {
if (i == 0 || students[i].class_id != students[i - 1].class_id) {
printf("%d %d %s %.1f %.1f %.1f\n", students[i].class_id, students[i].student_id, students[i].name, students[i].math_score, students[i].physics_score, students[i].english_score);
} else {
printf(" %d %s %.1f %.1f %.1f\n", students[i].student_id, students[i].name, students[i].math_score, students[i].physics_score, students[i].english_score);
}
}
printf(" modified\n");
} else {
printf("Student not found, no modifications made.\n");
}
}
void sortStudents() {
for (int i = 0; i < 3; i++) {
for (int j = i + 1; j < 3; j++) {
if (students[i].class_id > students[j].class_id ||
(students[i].class_id == students[j].class_id && students[i].total_score < students[j].total_score)) {
struct Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
for (int i = 0; i < 3; i++) {
if (i == 0 || students[i].class_id != students[i - 1].class_id) {
printf("%d %d %s %.1f %.1f %.1f\n", students[i].class_id, students[i].student_id, students[i].name, students[i].math_score, students[i].physics_score, students[i].english_score);
} else {
printf(" %d %s %.1f %.1f %.1f\n", students[i].student_id, students[i].name, students[i].math_score, students[i].physics_score, students[i].english_score);
}
}
}
int main() {
printf("Initial Student Information:\n");
sortStudents();
updateStudent(10002, 10005, 13, "Li", 80.0, 70.5, 90.0);
printf("\nUpdated Student Information:\n");
sortStudents();
return 0;
}