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.
91 lines
2.2 KiB
91 lines
2.2 KiB
#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 occur\n", newStudent.id);
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
insertStudent(students, &size, newStudent);
|
|
|
|
|
|
printStudents(students, size);
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
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");
|
|
}
|
|
}
|
|
} |