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.

93 lines
2.4 KiB

2 weeks ago
#include <stdio.h>
#include<stdlib.h>
typedef struct {
int id;
int classNum;
float score1;
float score2;
float score3;
} Student;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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; // <20><>ǰѧ<C7B0><D1A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Student newStudent;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
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);
// <20><><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD>Ƿ<EFBFBD><C7B7>Ѵ<EFBFBD><D1B4><EFBFBD>
int i;
for (i = 0; i < size; i++) {
if (students[i].id == newStudent.id) {
printf("ѧ<EFBFBD><EFBFBD> %d <20>Ѵ<EFBFBD><D1B4>ڡ<EFBFBD>\n", newStudent.id);
return 0;
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7>
insertStudent(students, &size, newStudent);
// <20><>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD>Ϣ
printStudents(students, size);
return 0;
}
// <20>ȽϺ<C8BD><CFBA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>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);
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7>
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)++;
}
// <20><>ӡѧ<D3A1><D1A7><EFBFBD><EFBFBD>Ϣ
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");
}
}
2 weeks ago
}