diff --git a/学生成绩管理系统.cpp b/学生成绩管理系统.cpp new file mode 100644 index 0000000..3ce1cf9 --- /dev/null +++ b/学生成绩管理系统.cpp @@ -0,0 +1,280 @@ +#include +#include +#include + +typedef struct { + int rank; // + char num[20]; // ѧ + char name[10]; // + int math_score; // ɼ + int datastruct_score; // ݽṹɼ + int English_score; // Ӣɼ + int physics_score; // ɼ + float average; // ƽ +} Student; + +typedef struct { + Student *data; + int length; +} funlist; + +// ʼԱ +void initlist(funlist *l) { + l->data = (Student *)malloc(100 * sizeof(Student)); + l->length = 0; +} + +// жǷΪձ +int isempty(funlist *l) { + return l->length == 0; +} + +// ȡֵ +int getelem(funlist l, int i, Student *e) { + if (i < 1 || i > l.length) { + printf("λòϷ\n"); + return 0; + } + *e = l.data[i - 1]; + return 1; +} + +// ƽ +float calculate_average(Student *s) { + return (s->math_score + s->datastruct_score + s->English_score + s->physics_score) / 4.0f; +} + +// ӡ +void printlist_no_rank(funlist l) { + int i; + printf("ɼΪ:\n"); + for (i = 0; i < l.length; i++) { + printf("ѧ:%s :%s :%d ݽṹ:%d Ӣ:%d :%d ƽ:%.2f\n", + l.data[i].num, l.data[i].name, l.data[i].math_score, + l.data[i].datastruct_score, l.data[i].English_score, + l.data[i].physics_score, l.data[i].average); + } +} + +// ӡ +void printlist_with_rank(funlist l) { + int i; + printf("ɼΪ:\n"); + for (i = 0; i < l.length; i++) { + printf(":%d ѧ:%s :%s :%d ݽṹ:%d Ӣ:%d :%d ƽ:%.2f\n", + l.data[i].rank, l.data[i].num, l.data[i].name, l.data[i].math_score, + l.data[i].datastruct_score, l.data[i].English_score, + l.data[i].physics_score, l.data[i].average); + } +} + +// ҲѧŲң +int locateelem1(funlist l, char *num) { + int i; + for (i = 0; i < l.length; i++) { + if (strcmp(l.data[i].num, num) == 0) + return i + 1; + } + return -1; +} + +// Ҳң +int locateelem2(funlist l, char *name) { + int i; + for (i = 0; i < l.length; i++) { + if (strcmp(l.data[i].name, name) == 0) + return i + 1; + } + return -1; +} + +// ɾ +int deleteelem(funlist *l, int i) { + int j; + if (i < 1 || i > l->length) { + printf("λòϷ\n"); + return 0; + } + for (j = i; j < l->length; j++) { + l->data[j - 1] = l->data[j]; + } + l->length--; + // µ + for (j = i - 1; j < l->length; j++) { + l->data[j].rank = j + 1; + } + return 1; +} + +// +int listinsert(funlist *l, int i, Student e) { + if (i < 1 || i > l->length + 1) { + printf("λòϷ\n"); + return 0; + } + if (l->length >= 100) { + printf("Ա\n"); + return 0; + } + int j; + for (j = l->length - 1; j >= i - 1; j--) { + l->data[j + 1] = l->data[j]; + // ƶ + l->data[j + 1].rank = l->data[j].rank + 1; + } + // 㲢ƽ + e.average = calculate_average(&e); + e.rank = i; + l->data[i - 1] = e; + l->length++; + return 1; // 1ʾɹ +} + +// ƽ +void sort_by_average(funlist *l) { + if (l->length <= 1) return; // + + int i, j; + for (i = 0; i < l->length - 1; i++) { + for (j = 0; j < l->length - i - 1; j++) { + if (l->data[j].average < l->data[j + 1].average) { + Student temp = l->data[j]; + l->data[j] = l->data[j + 1]; + l->data[j + 1] = temp; + } + } + } + // + for (i = 0; i < l->length; i++) { + l->data[i].rank = i + 1; + } + printf("ѧѰƽֽ\n"); +} + +int main() { + funlist L; + initlist(&L); + + int choice; + char num[20]; + char name[10]; + int math_score; + int datastruct_score; + int English_score; + int physics_score; + int i; + + printf("1.ѧ\n"); + printf("2.ͨѧŲѯѧ\n"); + printf("3.ͨѯѧ\n"); + printf("4.ɾѧ\n"); + printf("5.ƽѧ\n"); + printf("6.˳\n"); + + while (1) { + printf("ѡ:"); + scanf("%d", &choice); + switch (choice) { + case 1: { + Student e; + printf(":"); + scanf("%s", name); + strcpy(e.name, name); + + printf("ѧ:"); + scanf("%s", num); + strcpy(e.num, num); + + printf("ɼ:"); + scanf("%d", &math_score); + e.math_score = math_score; + + printf("ݽṹɼ:"); + scanf("%d", &datastruct_score); + e.datastruct_score = datastruct_score; + + printf("Ӣɼ:"); + scanf("%d", &English_score); + e.English_score = English_score; + + printf("ɼ:"); + scanf("%d", &physics_score); + e.physics_score = physics_score; + + printf("λ(1-%d):\n", L.length + 1); + scanf("%d", &i); + + if (listinsert(&L, i, e)) { + printf("ɹ\n"); + } else { + printf("ʧ\n"); + } + printlist_no_rank(L); + printf("\n"); + break; + } + + case 2: { + printf("ҵѧѧ:"); + scanf("%s", num); + i = locateelem1(L, num); + if (i != -1) { + Student e; + getelem(L, i, &e); + printf("ҵѧ: ѧ:%s :%s ƽ:%.2f\n", + e.num, e.name, e.average); + } else { + printf("δҵѧΪ%sѧ\n", num); + } + printf("\n"); + break; + } + + case 3: { + printf("ҵѧ:"); + scanf("%s", name); + i = locateelem2(L, name); + if (i != -1) { + Student e; + getelem(L, i, &e); + printf("ҵѧ: ѧ:%s :%s ƽ:%.2f\n", + e.num, e.name, e.average); + } else { + printf("δҵΪ%sѧ\n", name); + } + printf("\n"); + break; + } + + case 4: { + printf("ɾѧλ:"); + scanf("%d", &i); + if (deleteelem(&L, i)) { + printf("ɾɹ\n"); + } else { + printf("ɾʧ\n"); + } + printlist_no_rank(L); + printf("\n"); + break; + } + + case 5: { + sort_by_average(&L); + printlist_with_rank(L); + printf("\n"); + break; + } + + case 6: { + printf("˳ϵͳ\n"); + free(L.data); + return 0; + } + default: + printf("Чѡ\n"); + } + } + + return 0; +}