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.
84 lines
2.5 KiB
84 lines
2.5 KiB
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#define MAX_STUDENTS 3
|
|
#define MAX_NAME_LENGTH 20
|
|
|
|
struct Student {
|
|
char id[MAX_NAME_LENGTH];
|
|
int class;
|
|
char name[MAX_NAME_LENGTH];
|
|
float score1;
|
|
float score2;
|
|
float score3;
|
|
};
|
|
|
|
void printStudent(struct Student student, int isFirst) {
|
|
if (isFirst) {
|
|
printf("%d %s %s %.1f %.1f %.1f\n", student.class, student.id, student.name, student.score1, student.score2, student.score3);
|
|
} else {
|
|
printf(" %s %s %.1f %.1f %.1f\n", student.id, student.name, student.score1, student.score2, student.score3);
|
|
}
|
|
}
|
|
|
|
void printAllStudents(struct Student students[], int count) {
|
|
int isFirst = 1;
|
|
for (int i = 0; i < count; i++) {
|
|
printStudent(students[i], isFirst);
|
|
isFirst = 0;
|
|
}
|
|
}
|
|
|
|
int findStudent(struct Student students[], int count, char* search) {
|
|
for (int i = 0; i < count; i++) {
|
|
if (strcmp(students[i].id, search) == 0) {
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
void sortStudents(struct Student students[], int count) {
|
|
int i, j;
|
|
struct Student temp;
|
|
for (i = 0; i < count - 1; i++) {
|
|
for (j = 0; j < count - i - 1; j++) {
|
|
if (students[j].class > students[j + 1].class ||
|
|
(students[j].class == students[j + 1].class && students[j].score1 + students[j].score2 + students[j].score3 < students[j + 1].score1 + students[j + 1].score2 + students[j + 1].score3)) {
|
|
temp = students[j];
|
|
students[j] = students[j + 1];
|
|
students[j + 1] = temp;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
struct Student students[MAX_STUDENTS] = {
|
|
{"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}
|
|
};
|
|
int studentCount = MAX_STUDENTS;
|
|
|
|
char search[MAX_NAME_LENGTH];
|
|
printf("Enter student ID to modify: ");
|
|
scanf("%s", search);
|
|
|
|
int index = findStudent(students, studentCount, search);
|
|
if (index != -1) {
|
|
printf("Enter updated student information (class id name score1 score2 score3): ");
|
|
scanf("%d %s %s %f %f %f", &students[index].class, students[index].id, students[index].name, &students[index].score1, &students[index].score2, &students[index].score3);
|
|
|
|
sortStudents(students, studentCount);
|
|
printAllStudents(students, studentCount);
|
|
|
|
printf("modified\n");
|
|
} else {
|
|
printf("Student not found.\n");
|
|
printAllStudents(students, studentCount);
|
|
}
|
|
|
|
return 0;
|
|
}
|