|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ṹ<EFBFBD><E1B9B9><EFBFBD><EFBFBD><EFBFBD>洢ѧ<E6B4A2><D1A7><EFBFBD><EFBFBD>Ϣ
|
|
|
|
|
struct Student {
|
|
|
|
|
int id;
|
|
|
|
|
int class_num;
|
|
|
|
|
float grade1;
|
|
|
|
|
float grade2;
|
|
|
|
|
float grade3;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// <20>Ƚ<EFBFBD><C8BD><EFBFBD><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD>Ĵ<EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
int compareStudents(const void *a, const void *b) {
|
|
|
|
|
struct Student *studentA = (struct Student *)a;
|
|
|
|
|
struct Student *studentB = (struct Student *)b;
|
|
|
|
|
|
|
|
|
|
if (studentA->class_num < studentB->class_num) {
|
|
|
|
|
return -1;
|
|
|
|
|
} else if (studentA->class_num > studentB->class_num) {
|
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
// <20>༶<EFBFBD><E0BCB6>ͬ<EFBFBD><CDAC><EFBFBD>Ƚ<EFBFBD><C8BD>ܳɼ<DCB3>
|
|
|
|
|
float totalA = studentA->grade1 + studentA->grade2 + studentA->grade3;
|
|
|
|
|
float totalB = studentB->grade1 + studentB->grade2 + studentB->grade3;
|
|
|
|
|
|
|
|
|
|
if (totalA > totalB) {
|
|
|
|
|
return -1;
|
|
|
|
|
} else if (totalA < totalB) {
|
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
void insertStudent(struct Student students[], int *size, struct Student newStudent) {
|
|
|
|
|
int i;
|
|
|
|
|
// <20>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
|
|
|
|
|
for (i = 0; i < *size; i++) {
|
|
|
|
|
if (compareStudents(&newStudent, &students[i]) > 0) {
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20>ƶ<EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD>ڳ<EFBFBD><DAB3><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
|
|
|
|
|
for (int j = *size; j > i; j--) {
|
|
|
|
|
students[j] = students[j - 1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7>
|
|
|
|
|
students[i] = newStudent;
|
|
|
|
|
(*size)++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD>Ƿ<EFBFBD><C7B7>Ѵ<EFBFBD><D1B4><EFBFBD>
|
|
|
|
|
int checkIdExists(struct Student students[], int size, int id) {
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
|
if (students[i].id == id) {
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD>Ϣ
|
|
|
|
|
void printStudents(struct Student students[], int size) {
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
|
printf("%d %d %.1f %.1f %.1f", students[i].id, students[i].class_num, students[i].grade1, students[i].grade2, students[i].grade3);
|
|
|
|
|
if (i == size - 1) {
|
|
|
|
|
printf(" inserted\n");
|
|
|
|
|
} else {
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>
|
|
|
|
|
struct Student students[100];
|
|
|
|
|
students[0].id = 10001;
|
|
|
|
|
students[0].class_num = 11;
|
|
|
|
|
students[0].grade1 = 99.5;
|
|
|
|
|
students[0].grade2 = 88.5;
|
|
|
|
|
students[0].grade3 = 89.5;
|
|
|
|
|
|
|
|
|
|
students[1].id = 10002;
|
|
|
|
|
students[1].class_num = 12;
|
|
|
|
|
students[1].grade1 = 77.9;
|
|
|
|
|
students[1].grade2 = 56.5;
|
|
|
|
|
students[1].grade3 = 87.5;
|
|
|
|
|
|
|
|
|
|
students[2].id = 10003;
|
|
|
|
|
students[2].class_num = 11;
|
|
|
|
|
students[2].grade1 = 92.5;
|
|
|
|
|
students[2].grade2 = 99.0;
|
|
|
|
|
students[2].grade3 = 60.5;
|
|
|
|
|
|
|
|
|
|
int size = 3;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD>Ϣ
|
|
|
|
|
struct Student newStudent;
|
|
|
|
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD>ѧ<EFBFBD><EFBFBD> <20>༶ <20>ɼ<EFBFBD>1 <20>ɼ<EFBFBD>2 <20>ɼ<EFBFBD>3<EFBFBD><33><EFBFBD><EFBFBD>");
|
|
|
|
|
scanf("%d %d %f %f %f", &newStudent.id, &newStudent.class_num, &newStudent.grade1, &newStudent.grade2, &newStudent.grade3);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD>Ƿ<EFBFBD><C7B7>Ѵ<EFBFBD><D1B4><EFBFBD>
|
|
|
|
|
int existsIndex = checkIdExists(students, size, newStudent.id);
|
|
|
|
|
if (existsIndex!= -1) {
|
|
|
|
|
printf("<EFBFBD>Ѵ<EFBFBD><EFBFBD><EFBFBD>ѧ<EFBFBD>ŵ<EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD>\n");
|
|
|
|
|
printStudents(students, size);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD>Ϣ
|
|
|
|
|
insertStudent(students, &size, newStudent);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD>Ϣ
|
|
|
|
|
printStudents(students, size);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|