|
|
|
@ -8,7 +8,7 @@ typedef struct {
|
|
|
|
|
float score3;
|
|
|
|
|
} Student;
|
|
|
|
|
|
|
|
|
|
// 函数声明
|
|
|
|
|
|
|
|
|
|
int compareStudents(const void *a, const void *b);
|
|
|
|
|
void insertStudent(Student students[], int *size, Student newStudent);
|
|
|
|
|
void printStudents(Student students[], int size);
|
|
|
|
@ -19,10 +19,10 @@ int main() {
|
|
|
|
|
{10002, 12, 77.9, 56.5, 87.5},
|
|
|
|
|
{10003, 11, 92.5, 99.0, 60.5}
|
|
|
|
|
};
|
|
|
|
|
int size = 3; // 当前学生数量
|
|
|
|
|
int size = 3;
|
|
|
|
|
Student newStudent;
|
|
|
|
|
|
|
|
|
|
// 输入新学生的信息
|
|
|
|
|
|
|
|
|
|
printf("");
|
|
|
|
|
scanf("%d",&newStudent.id);
|
|
|
|
|
printf("");
|
|
|
|
@ -33,25 +33,24 @@ int main() {
|
|
|
|
|
scanf("%f",&newStudent.score2);
|
|
|
|
|
printf("");
|
|
|
|
|
scanf("%f",&newStudent.score3);
|
|
|
|
|
// 检查学号是否已存在
|
|
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
|
if (students[i].id == newStudent.id) {
|
|
|
|
|
printf("学号 %d 已存在。\n", newStudent.id);
|
|
|
|
|
printf(" %d \n", newStudent.id);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 插入新学生
|
|
|
|
|
insertStudent(students, &size, newStudent);
|
|
|
|
|
|
|
|
|
|
// 打印所有学生信息
|
|
|
|
|
|
|
|
|
|
printStudents(students, size);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 比较函数,用于qsort
|
|
|
|
|
|
|
|
|
|
int compareStudents(const void *a, const void *b) {
|
|
|
|
|
Student *studentA = (Student *)a;
|
|
|
|
|
Student *studentB = (Student *)b;
|
|
|
|
@ -64,7 +63,7 @@ int compareStudents(const void *a, const void *b) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 插入新学生
|
|
|
|
|
|
|
|
|
|
void insertStudent(Student students[], int *size, Student newStudent) {
|
|
|
|
|
Student temp[*size + 1];
|
|
|
|
|
int i;
|
|
|
|
@ -79,7 +78,6 @@ void insertStudent(Student students[], int *size, Student newStudent) {
|
|
|
|
|
(*size)++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 打印学生信息
|
|
|
|
|
void printStudents(Student students[], int size) {
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
|