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.

83 lines
2.3 KiB

#include <stdio.h>
#include <stdlib.h>
struct Student {
int id;
int classNum;
float score1;
float score2;
float score3;
} Student;
int compareStudents( struct Student studentA, struct Student studentB)
{
if (studentA.classNum < studentB.classNum) {
return -1;
} else if (studentA.classNum > studentB.classNum) {
return 1;
} else {
float totalScoreA = studentA.score1 + studentA.score2 + studentA.score3;
float totalScoreB = studentB.score1 + studentB.score2 + studentB.score3;
if (totalScoreA > totalScoreB) {
return -1;
} else if (totalScoreA < totalScoreB) {
return 1;
} else {
return 0;
}
}
}
int main() {
struct Student students[4];
students[0].id = 10001;
students[0].classNum = 11;
students[0].score1 = 99.5;
students[0].score2 = 88.5;
students[0].score3 = 89.5;
students[1].id = 10002;
students[1].classNum = 12;
students[1].score1 = 77.9;
students[1].score2 = 56.5;
students[1].score3 = 87.5;
students[2].id = 10003;
students[2].classNum = 11;
students[2].score1 = 92.5;
students[2].score2 = 99.0;
students[2].score3 = 60.5;
struct Student newStudent;
scanf("%d %d %f %f %f", &newStudent.id, &newStudent.classNum, &newStudent.score1, &newStudent.score2, &newStudent.score3);
int isExist = 0;
int i;
for (i = 0; i < 3; i++) {
if (newStudent.id == students[i].id) {
isExist = 1;
break;
}
}
if (isExist) {
printf("id had exsited");
printf("%d %d %.1f %.1f %.1f\n", students[0].id, students[0].classNum, students[0].score1, students[0].score2, students[0].score3);
return 0;
}
students[3] = newStudent;
qsort(students, 4, sizeof(Student), compareStudents);
for (i = 0; i < 4; i++) {
printf("%d %d %.1f %.1f %.1f", students[i].id, students[i].classNum, students[i].score1, students[i].score2, students[i].score3);
if (students[i].id == newStudent.id) {
printf(" inserted\n");
} else {
printf("\n");
}
}
return 0;
}