parent
6ca4d646c3
commit
805de089d0
@ -0,0 +1,141 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct {
|
||||
char id[20];
|
||||
char clas[20];
|
||||
char name[20];
|
||||
double score1;
|
||||
double score2;
|
||||
double score3;
|
||||
double score;
|
||||
} Student;
|
||||
|
||||
void inputStudentInfo(Student *student) {
|
||||
scanf("%s %s %s", student->id, student->clas, student->name);
|
||||
scanf("%lf %lf %lf", &student->score1, &student->score2, &student->score3);
|
||||
student->score = student->score1 + student->score2 + student->score3;
|
||||
}
|
||||
|
||||
void displayStudentInfo(Student student, int isFirstInClass) {
|
||||
if (isFirstInClass) {
|
||||
printf("%s %s %s %.1lf %.1lf %.1lf",
|
||||
student.clas, student.id, student.name,
|
||||
student.score1, student.score2, student.score3);
|
||||
} else {
|
||||
printf(" %s %s %.1lf %.1lf %.1lf",
|
||||
student.id, student.name,
|
||||
student.score1, student.score2, student.score3);
|
||||
}
|
||||
}
|
||||
|
||||
int findStudentById(Student *students, int studentCount, char *id) {
|
||||
for (int i = 0; i < studentCount; i++) {
|
||||
if (strcmp(students[i].id, id) == 0) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void modifyStudentInfo(Student *students, int studentCount, char *id) {
|
||||
int index = findStudentById(students, studentCount, id);
|
||||
if (index != -1) {
|
||||
printf("Please enter the modified information:\n");
|
||||
inputStudentInfo(&students[index]);
|
||||
} else {
|
||||
printf("The relevant student was not found, and modification is not possible\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void bubbleSortByClassAndScore(Student *students, int studentCount) {
|
||||
// 冒泡排序,先按班级从小到大,再按总成绩从大到小
|
||||
for (int i = 0; i < studentCount - 1; i++) {
|
||||
for (int j = 0; j < studentCount - i - 1; j++) {
|
||||
if (strcmp(students[j].clas, students[j + 1].clas) > 0 ||
|
||||
(strcmp(students[j].clas, students[j + 1].clas) == 0 && students[j].score < students[j + 1].score)) {
|
||||
// 交换两个学生的信息
|
||||
Student temp = students[j];
|
||||
students[j] = students[j + 1];
|
||||
students[j + 1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
Student students[100];
|
||||
int studentCount = 3;
|
||||
|
||||
// 初始化三个学生信息
|
||||
strcpy(students[0].id, "10001");
|
||||
strcpy(students[0].clas, "11");
|
||||
strcpy(students[0].name, "Zhang");
|
||||
students[0].score1 = 99.5;
|
||||
students[0].score2 = 88.5;
|
||||
students[0].score3 = 89.5;
|
||||
students[0].score = students[0].score1 + students[0].score2 + students[0].score3;
|
||||
|
||||
strcpy(students[1].id, "10002");
|
||||
strcpy(students[1].clas, "12");
|
||||
strcpy(students[1].name, "Yang");
|
||||
students[1].score1 = 77.9;
|
||||
students[1].score2 = 56.5;
|
||||
students[1].score3 = 87.5;
|
||||
students[1].score = students[1].score1 + students[1].score2 + students[1].score3;
|
||||
|
||||
strcpy(students[2].id, "10003");
|
||||
strcpy(students[2].clas, "11");
|
||||
strcpy(students[2].name, "Liang");
|
||||
students[2].score1 = 92.5;
|
||||
students[2].score2 = 99.0;
|
||||
students[2].score3 = 60.5;
|
||||
students[2].score = students[2].score1 + students[2].score2 + students[2].score3;
|
||||
|
||||
bubbleSortByClassAndScore(students, studentCount);
|
||||
|
||||
// 输出所有学生信息
|
||||
int isFirstInClass = 1;
|
||||
for (int i = 0; i < studentCount; i++) {
|
||||
if (i > 0 && strcmp(students[i].clas, students[i - 1].clas) == 0) {
|
||||
isFirstInClass = 0;
|
||||
} else {
|
||||
isFirstInClass = 1;
|
||||
}
|
||||
displayStudentInfo(students[i], isFirstInClass);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// 修改学生信息
|
||||
char id[20];
|
||||
printf("Please enter the student ID to be modified:");
|
||||
scanf("%s", id);
|
||||
modifyStudentInfo(students, studentCount, id);
|
||||
|
||||
// 再次排序并输出所有学生信息
|
||||
bubbleSortByClassAndScore(students, studentCount);
|
||||
int d=findStudentById(students, studentCount, id);
|
||||
isFirstInClass = 1;
|
||||
for (int i = 0; i < studentCount; i++) {
|
||||
if (i > 0 && strcmp(students[i].clas, students[i - 1].clas) == 0) {
|
||||
isFirstInClass = 0;
|
||||
} else {
|
||||
isFirstInClass = 1;
|
||||
}
|
||||
if (i == d) {
|
||||
|
||||
displayStudentInfo(students[i], isFirstInClass);
|
||||
printf(" modified\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
displayStudentInfo(students[i], isFirstInClass);
|
||||
printf("\n");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in new issue