|
|
|
@ -1,93 +1,93 @@
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include<stdlib.h>
|
|
|
|
|
typedef struct {
|
|
|
|
|
int id;
|
|
|
|
|
int classNum;
|
|
|
|
|
float score1;
|
|
|
|
|
float score2;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
Student students[4] = {
|
|
|
|
|
{10001, 11, 99.5, 88.5, 89.5},
|
|
|
|
|
{10002, 12, 77.9, 56.5, 87.5},
|
|
|
|
|
{10003, 11, 92.5, 99.0, 60.5}
|
|
|
|
|
};
|
|
|
|
|
int size = 3; // 当前学生数量
|
|
|
|
|
Student newStudent;
|
|
|
|
|
|
|
|
|
|
// 输入新学生的信息
|
|
|
|
|
printf("");
|
|
|
|
|
scanf("%d",&newStudent.id);
|
|
|
|
|
printf("");
|
|
|
|
|
scanf("%d",&newStudent.classNum);
|
|
|
|
|
printf("");
|
|
|
|
|
scanf("%f",&newStudent.score1);
|
|
|
|
|
printf("");
|
|
|
|
|
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);
|
|
|
|
|
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;
|
|
|
|
|
if (studentA->classNum != studentB->classNum) {
|
|
|
|
|
return studentA->classNum - studentB->classNum;
|
|
|
|
|
} else {
|
|
|
|
|
float totalA = studentA->score1 + studentA->score2 + studentA->score3;
|
|
|
|
|
float totalB = studentB->score1 + studentB->score2 + studentB->score3;
|
|
|
|
|
return (totalB > totalA) - (totalA > totalB);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 插入新学生
|
|
|
|
|
void insertStudent(Student students[], int *size, Student newStudent) {
|
|
|
|
|
Student temp[*size + 1];
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; i < *size; i++) {
|
|
|
|
|
temp[i] = students[i];
|
|
|
|
|
}
|
|
|
|
|
temp[*size] = newStudent;
|
|
|
|
|
qsort(temp, *size + 1, sizeof(Student), compareStudents);
|
|
|
|
|
for (i = 0; i < *size + 1; i++) {
|
|
|
|
|
students[i] = temp[i];
|
|
|
|
|
}
|
|
|
|
|
(*size)++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 打印学生信息
|
|
|
|
|
void printStudents(Student students[], int size) {
|
|
|
|
|
int 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);
|
|
|
|
|
if (i == size-2) {
|
|
|
|
|
printf(" inserted\n");
|
|
|
|
|
} else {
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include<stdlib.h>
|
|
|
|
|
typedef struct {
|
|
|
|
|
int id;
|
|
|
|
|
int classNum;
|
|
|
|
|
float score1;
|
|
|
|
|
float score2;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
Student students[4] = {
|
|
|
|
|
{10001, 11, 99.5, 88.5, 89.5},
|
|
|
|
|
{10002, 12, 77.9, 56.5, 87.5},
|
|
|
|
|
{10003, 11, 92.5, 99.0, 60.5}
|
|
|
|
|
};
|
|
|
|
|
int size = 3; // 当前学生数量
|
|
|
|
|
Student newStudent;
|
|
|
|
|
|
|
|
|
|
// 输入新学生的信息
|
|
|
|
|
printf("");
|
|
|
|
|
scanf("%d",&newStudent.id);
|
|
|
|
|
printf("");
|
|
|
|
|
scanf("%d",&newStudent.classNum);
|
|
|
|
|
printf("");
|
|
|
|
|
scanf("%f",&newStudent.score1);
|
|
|
|
|
printf("");
|
|
|
|
|
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);
|
|
|
|
|
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;
|
|
|
|
|
if (studentA->classNum != studentB->classNum) {
|
|
|
|
|
return studentA->classNum - studentB->classNum;
|
|
|
|
|
} else {
|
|
|
|
|
float totalA = studentA->score1 + studentA->score2 + studentA->score3;
|
|
|
|
|
float totalB = studentB->score1 + studentB->score2 + studentB->score3;
|
|
|
|
|
return (totalB > totalA) - (totalA > totalB);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 插入新学生
|
|
|
|
|
void insertStudent(Student students[], int *size, Student newStudent) {
|
|
|
|
|
Student temp[*size + 1];
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; i < *size; i++) {
|
|
|
|
|
temp[i] = students[i];
|
|
|
|
|
}
|
|
|
|
|
temp[*size] = newStudent;
|
|
|
|
|
qsort(temp, *size + 1, sizeof(Student), compareStudents);
|
|
|
|
|
for (i = 0; i < *size + 1; i++) {
|
|
|
|
|
students[i] = temp[i];
|
|
|
|
|
}
|
|
|
|
|
(*size)++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 打印学生信息
|
|
|
|
|
void printStudents(Student students[], int size) {
|
|
|
|
|
int 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);
|
|
|
|
|
if (i == size-2) {
|
|
|
|
|
printf(" inserted\n");
|
|
|
|
|
} else {
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|