#define _CRT_SECURE_NO_WARNINGS #include #include #include typedef struct Student { char id[20]; char clas[20]; char name[20]; double score1; double score2; double score3; double score; struct Student* prev; struct Student* next; }student;// student* head, * tail; int tot; void ins(student *t) { t->prev = tail->prev; tail->prev->next = t; tail->prev = t; t->prev = tail; tot++; } void del(student* u) { student* v = u->next; u->next = v->next; v->next->prev = u; if (v == tail) tail = u; free(v); tot--; } void input() { student *t; t = (struct Student*)malloc(sizeof(struct Student)); printf("Id "); scanf("%s", t->id); printf("class "); scanf("%s", t->clas); printf("name "); scanf("%s", t->name); printf("score1 "); scanf("%lf", &t->score1); printf("score2 "); scanf("%lf", &t->score2); printf("score3 "); scanf("%lf", &t->score3); t->score = t->score1 + t->score2 + t->score3; ins(t); } void output() { for (student* u = head; u->next != NULL; u = u->next) { student* v = u->next; printf("%s,%s,%s,%.1lf,%.1lf,%.1lf,%.1lf\n", v->id, v->clas, v->name, v->score1, v->score2, v->score3, v->score); } } void deletl() { char s[10]; scanf("%s", s); student* u; for (u = head; u->next != NULL;) { student* v = u->next; if (strcmp(s, v->id) == 0 || strcmp(s, v->name) == 0) del(u); else u = u->next; } output(); } void select() { char s[10]; scanf("%s", s); student* u; int flag = 0; for ( u = head; u->next != NULL; u = u->next) { student* v = u->next; if (strcmp(s, v->clas) == 0 || strcmp(s, v->id) == 0) { printf("%s,%s,%s,%.1lf,%.1lf,%.1lf,%.1lf\n", v->id, v->clas, v->name, v->score1, v->score2, v->score3, v->score); flag++; } } if (flag == 0) printf("there is no eligible student\n"); } void sort(student* st, student* ed) { student t; for (student* i = st; i != ed; i++) for (student* j = ed - 1; j != i; j--) { int x = strcmp(j->clas, (j - 1)->clas); if (x < 0 || x == 0 && j->score >(j - 1)->score) { t = *j; *j = *(j - 1); *(j - 1) = t; } } } void order() { student* students = (student*)malloc(sizeof(student) * tot); int n = 0; while (head->next != NULL) { students[n++] = *(head->next); del(head); } sort(students, students + n); for (int i = 0; i < n; i++) ins((students+i)); free(students); output(); } int main() { tail = head = (student*)malloc(sizeof(student)); head->next = NULL; head->prev = NULL; tail->next = NULL; tail->prev = head; tot = 0; while (1) { printf("1.input\n" "2.delete\n" "3.select\n" "4.order\n" "5.output\n" "6.quit\n" "please input your option\n"); char op[10]; while (1) { scanf("%s", op); if (strlen(op) == 1 && op[0] >= '1' && op[0] <= '6') break; printf("Wrong input\n"); } if (op[0] == '6') break; while (1) { if (op[0] == '1') input(); else if (op[0] == '2') deletl(); else if (op[0] == '3') select(); else if (op[0] == '4') order(); else if(op[0]=='5') output(); if (op[0] == '4' || op[0] == '5') break; printf("continue?\n"); char buf[10]; while (1) { scanf("%s", buf); if (strcmp(buf, "yes") == 0 || strcmp(buf, "no") == 0) break; printf("Wrong input\n"); } if (strcmp(buf, "no") == 0) break; } } return 0; }