|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#define MAX_STUDENT_NUM 100
|
|
|
|
|
#define MAX_NAME_LENGTH 20
|
|
|
|
|
|
|
|
|
|
typedef struct student_info {
|
|
|
|
|
int id;
|
|
|
|
|
int cls;
|
|
|
|
|
float scores[3];
|
|
|
|
|
float total_score;
|
|
|
|
|
float avg_score;
|
|
|
|
|
int is_inserted; // <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>Dz<EFBFBD><C7B2><EFBFBD><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7>
|
|
|
|
|
} student;
|
|
|
|
|
|
|
|
|
|
void calc_score(student students[], int num) {
|
|
|
|
|
int i, j;
|
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
|
float total_score = 0;
|
|
|
|
|
for (j = 0; j < 3; j++) {
|
|
|
|
|
total_score += students[i].scores[j];
|
|
|
|
|
}
|
|
|
|
|
students[i].total_score = total_score;
|
|
|
|
|
students[i].avg_score = total_score / 3;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void insert_student(student students[], int* num, int id, int cls, float scores[3]) {
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD>Ϣ
|
|
|
|
|
students[*num].id = id;
|
|
|
|
|
students[*num].cls = cls;
|
|
|
|
|
students[*num].scores[0] = scores[0];
|
|
|
|
|
students[*num].scores[1] = scores[1];
|
|
|
|
|
students[*num].scores[2] = scores[2];
|
|
|
|
|
students[*num].is_inserted = 1; // <20><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7>
|
|
|
|
|
|
|
|
|
|
(*num)++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sort_students(student students[], int num) {
|
|
|
|
|
// ʹ<><CAB9>ð<EFBFBD><C3B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>cls<6C><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD>total_score<72><65><EFBFBD><EFBFBD>
|
|
|
|
|
for (int i = 0; i < num - 1; i++) {
|
|
|
|
|
for (int j = 0; j < num - i - 1; j++) {
|
|
|
|
|
if (students[j].cls > students[j + 1].cls ||
|
|
|
|
|
(students[j].cls == students[j + 1].cls && students[j].total_score < students[j + 1].total_score)) {
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
|
|
|
|
|
student temp = students[j];
|
|
|
|
|
students[j] = students[j + 1];
|
|
|
|
|
students[j + 1] = temp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
student students[MAX_STUDENT_NUM] = {
|
|
|
|
|
{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 num = 3; // <20><><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD>Ŀ
|
|
|
|
|
int id, cls, i;
|
|
|
|
|
float scores[3];
|
|
|
|
|
|
|
|
|
|
printf("Please enter new student information:\n");
|
|
|
|
|
scanf("%d %d %f %f %f", &id, &cls, &scores[0], &scores[1], &scores[2]);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD>Ƿ<EFBFBD><C7B7>Ѵ<EFBFBD><D1B4><EFBFBD>
|
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
|
if (students[i].id == id) {
|
|
|
|
|
printf("ѧ<EFBFBD><EFBFBD>Ϊ%d<><64>ѧ<EFBFBD><D1A7><EFBFBD>Ѵ<EFBFBD><D1B4><EFBFBD>\n", id);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
insert_student(students, &num, id, cls, scores);
|
|
|
|
|
calc_score(students, num);
|
|
|
|
|
sort_students(students, num);
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
|
printf("%d %d %.1f %.1f %.1f", students[i].id, students[i].cls, students[i].scores[0], students[i].scores[1], students[i].scores[2]);
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>Dz<EFBFBD><C7B2><EFBFBD><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> "insert"
|
|
|
|
|
if (students[i].is_inserted == 1) {
|
|
|
|
|
printf(" insert");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|