|
|
@ -1,93 +1,93 @@
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include<stdlib.h>
|
|
|
|
#include<stdlib.h>
|
|
|
|
typedef struct {
|
|
|
|
typedef struct {
|
|
|
|
int id;
|
|
|
|
int id;
|
|
|
|
int classNum;
|
|
|
|
int classNum;
|
|
|
|
float score1;
|
|
|
|
float score1;
|
|
|
|
float score2;
|
|
|
|
float score2;
|
|
|
|
float score3;
|
|
|
|
float score3;
|
|
|
|
} Student;
|
|
|
|
} Student;
|
|
|
|
|
|
|
|
|
|
|
|
// 函数声明
|
|
|
|
// 函数声明
|
|
|
|
int compareStudents(const void *a, const void *b);
|
|
|
|
int compareStudents(const void *a, const void *b);
|
|
|
|
void insertStudent(Student students[], int *size, Student newStudent);
|
|
|
|
void insertStudent(Student students[], int *size, Student newStudent);
|
|
|
|
void printStudents(Student students[], int size);
|
|
|
|
void printStudents(Student students[], int size);
|
|
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
int main() {
|
|
|
|
Student students[4] = {
|
|
|
|
Student students[4] = {
|
|
|
|
{10001, 11, 99.5, 88.5, 89.5},
|
|
|
|
{10001, 11, 99.5, 88.5, 89.5},
|
|
|
|
{10002, 12, 77.9, 56.5, 87.5},
|
|
|
|
{10002, 12, 77.9, 56.5, 87.5},
|
|
|
|
{10003, 11, 92.5, 99.0, 60.5}
|
|
|
|
{10003, 11, 92.5, 99.0, 60.5}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
int size = 3; // 当前学生数量
|
|
|
|
int size = 3; // 当前学生数量
|
|
|
|
Student newStudent;
|
|
|
|
Student newStudent;
|
|
|
|
|
|
|
|
|
|
|
|
// 输入新学生的信息
|
|
|
|
// 输入新学生的信息
|
|
|
|
printf("");
|
|
|
|
printf("");
|
|
|
|
scanf("%d",&newStudent.id);
|
|
|
|
scanf("%d",&newStudent.id);
|
|
|
|
printf("");
|
|
|
|
printf("");
|
|
|
|
scanf("%d",&newStudent.classNum);
|
|
|
|
scanf("%d",&newStudent.classNum);
|
|
|
|
printf("");
|
|
|
|
printf("");
|
|
|
|
scanf("%f",&newStudent.score1);
|
|
|
|
scanf("%f",&newStudent.score1);
|
|
|
|
printf("");
|
|
|
|
printf("");
|
|
|
|
scanf("%f",&newStudent.score2);
|
|
|
|
scanf("%f",&newStudent.score2);
|
|
|
|
printf("");
|
|
|
|
printf("");
|
|
|
|
scanf("%f",&newStudent.score3);
|
|
|
|
scanf("%f",&newStudent.score3);
|
|
|
|
// 检查学号是否已存在
|
|
|
|
// 检查学号是否已存在
|
|
|
|
int i;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
if (students[i].id == newStudent.id) {
|
|
|
|
if (students[i].id == newStudent.id) {
|
|
|
|
printf("学号 %d 已存在。\n", newStudent.id);
|
|
|
|
printf("学号 %d 已存在。\n", newStudent.id);
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 插入新学生
|
|
|
|
// 插入新学生
|
|
|
|
insertStudent(students, &size, newStudent);
|
|
|
|
insertStudent(students, &size, newStudent);
|
|
|
|
|
|
|
|
|
|
|
|
// 打印所有学生信息
|
|
|
|
// 打印所有学生信息
|
|
|
|
printStudents(students, size);
|
|
|
|
printStudents(students, size);
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 比较函数,用于qsort
|
|
|
|
// 比较函数,用于qsort
|
|
|
|
int compareStudents(const void *a, const void *b) {
|
|
|
|
int compareStudents(const void *a, const void *b) {
|
|
|
|
Student *studentA = (Student *)a;
|
|
|
|
Student *studentA = (Student *)a;
|
|
|
|
Student *studentB = (Student *)b;
|
|
|
|
Student *studentB = (Student *)b;
|
|
|
|
if (studentA->classNum != studentB->classNum) {
|
|
|
|
if (studentA->classNum != studentB->classNum) {
|
|
|
|
return studentA->classNum - studentB->classNum;
|
|
|
|
return studentA->classNum - studentB->classNum;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
float totalA = studentA->score1 + studentA->score2 + studentA->score3;
|
|
|
|
float totalA = studentA->score1 + studentA->score2 + studentA->score3;
|
|
|
|
float totalB = studentB->score1 + studentB->score2 + studentB->score3;
|
|
|
|
float totalB = studentB->score1 + studentB->score2 + studentB->score3;
|
|
|
|
return (totalB > totalA) - (totalA > totalB);
|
|
|
|
return (totalB > totalA) - (totalA > totalB);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 插入新学生
|
|
|
|
// 插入新学生
|
|
|
|
void insertStudent(Student students[], int *size, Student newStudent) {
|
|
|
|
void insertStudent(Student students[], int *size, Student newStudent) {
|
|
|
|
Student temp[*size + 1];
|
|
|
|
Student temp[*size + 1];
|
|
|
|
int i;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < *size; i++) {
|
|
|
|
for (i = 0; i < *size; i++) {
|
|
|
|
temp[i] = students[i];
|
|
|
|
temp[i] = students[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
temp[*size] = newStudent;
|
|
|
|
temp[*size] = newStudent;
|
|
|
|
qsort(temp, *size + 1, sizeof(Student), compareStudents);
|
|
|
|
qsort(temp, *size + 1, sizeof(Student), compareStudents);
|
|
|
|
for (i = 0; i < *size + 1; i++) {
|
|
|
|
for (i = 0; i < *size + 1; i++) {
|
|
|
|
students[i] = temp[i];
|
|
|
|
students[i] = temp[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(*size)++;
|
|
|
|
(*size)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 打印学生信息
|
|
|
|
// 打印学生信息
|
|
|
|
void printStudents(Student students[], int size) {
|
|
|
|
void printStudents(Student students[], int size) {
|
|
|
|
int i;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
printf("%d %d %.1f %.1f %.1f", students[i].id, students[i].classNum, students[i].score1, students[i].score2, students[i].score3);
|
|
|
|
printf("%d %d %.1f %.1f %.1f", students[i].id, students[i].classNum, students[i].score1, students[i].score2, students[i].score3);
|
|
|
|
if (i == size-2) {
|
|
|
|
if (i == size-2) {
|
|
|
|
printf(" inserted\n");
|
|
|
|
printf(" inserted\n");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
printf("\n");
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|