diff --git a/作业.cpp b/作业.cpp new file mode 100644 index 0000000..ad96b4c --- /dev/null +++ b/作业.cpp @@ -0,0 +1,206 @@ +#include +#include +#include + +#define MAX_STUDENTS 100 + +typedef struct { + int id; + char name[50]; + int score; +} Student; + +int displayMenu() { + printf("\nѧÉú³É¼¨¹ÜÀíϵͳ\n"); + printf("1. ¼ÈëѧÉúÐÅÏ¢\n"); + printf("2. ÏÔʾËùÓÐѧÉúÐÅÏ¢\n"); + printf("3. ²åÈëѧÉúÐÅÏ¢\n"); + printf("4. ɾ³ýѧÉúÐÅÏ¢\n"); + printf("5. ÐÞ¸ÄѧÉúÐÅÏ¢\n"); + printf("6. ²éѯѧÉúÐÅÏ¢\n"); + printf("7. Í˳öϵͳ\n"); + printf("ÇëÑ¡Ôñ²Ù×÷: "); + int choice; + scanf("%d", &choice); + return choice; +} + +void addStudent(Student students[], int *count) { + if (*count >= MAX_STUDENTS) { + printf("ѧÉúÐÅÏ¢ÒÑÂú£¬ÎÞ·¨Ìí¼Ó¸ü¶àѧÉú¡£\n"); + return; + } + printf("ÇëÊäÈëѧÉúID: "); + scanf("%d", &students[*count].id); + printf("ÇëÊäÈëѧÉúÐÕÃû: "); + scanf("%s", students[*count].name); + printf("ÇëÊäÈëѧÉú³É¼¨: "); + scanf("%d", &students[*count].score); + (*count)++; + printf("ѧÉúÐÅϢ¼Èë³É¹¦¡£\n"); +} +c + +void displayStudents(const Student students[], int count) { + if (count == 0) { + printf("ûÓÐѧÉúÐÅÏ¢¡£\n"); + return; + } + printf("ѧÉúID\tÐÕÃû\t³É¼¨\n"); + for (int i = 0; i < count; i++) { + printf("%d\t%s\t%d\n", students[i].id, students[i].name, students[i].score); + } +} + + +void insertStudent(Student students[], int *count) { + if (*count >= MAX_STUDENTS) { + printf("ѧÉúÐÅÏ¢ÒÑÂú£¬ÎÞ·¨Ìí¼Ó¸ü¶àѧÉú¡£\n"); + return; + } + Student newStudent; + printf("ÇëÊäÈëѧÉúID: "); + scanf("%d", &newStudent.id); + printf("ÇëÊäÈëѧÉúÐÕÃû: "); + scanf("%s", newStudent.name); + printf("ÇëÊäÈëѧÉú³É¼¨: "); + scanf("%d", &newStudent.score); + + int index = *count; + while (index > 0 &&students[index - 1].score < newStudent.score) { + students[index] = students[index - 1]; + index--; + } + students[index] = newStudent; + (*count)++; + printf("ѧÉúÐÅÏ¢²åÈë³É¹¦¡£\n"); +} + + +void deleteStudent(Student students[], int *count) { + if (*count == 0) { + printf("ûÓÐѧÉúÐÅÏ¢¡£\n"); + return; + } + int id; + printf("ÇëÊäÈëҪɾ³ýµÄѧÉúID: "); + scanf("%d", &id); + + int index = -1; + for (int i = 0; i < *count; i++) { + if (students[i].id == id) { + index = i; + break; + } + } + + if (index == -1) { + printf("δÕÒµ½Ö¸¶¨IDµÄѧÉú¡£\n"); + return; + } + + for (int i = index; i < *count - 1; i++) { + students[i] = students[i + 1]; + } + (*count)--; + printf("ѧÉúÐÅϢɾ³ý³É¹¦¡£\n"); +} + +void modifyStudent(Student students[], int count) { + if (count == 0) { + printf("ûÓÐѧÉúÐÅÏ¢¡£\n"); + return; + } + int id; + printf("ÇëÊäÈëÒªÐ޸ĵÄѧÉúID: "); + scanf("%d", &id); + + int index = -1; + for (int i = 0; i < count; i++) { + if (students[i].id == id) { + index = i; + break; + } + } + + if (index == -1) { + printf("δÕÒµ½Ö¸¶¨IDµÄѧÉú¡£\n"); + return; + } + + printf("ÇëÊäÈëеÄѧÉúÐÕÃû: "); + scanf("%s", students[index].name); + printf("ÇëÊäÈëеÄѧÉú³É¼¨: "); + scanf("%d", &students[index].score); + printf("ѧÉúÐÅÏ¢Ð޸ijɹ¦¡£\n"); + + // ÖØÐÂÅÅÐò + qsort(students, count, sizeof(Student), compareStudents); +} + +int compareStudents(const void *a, const void *b) { + return ((Student *)b)->score - ((Student *)a)->score; +} + +void queryStudent(const Student students[], int count) { + if (count == 0) { + printf("ûÓÐѧÉúÐÅÏ¢¡£\n"); + return; + } + int id; + printf("ÇëÊäÈëÒª²éѯµÄѧÉúID: "); + scanf("%d", &id); + + int found = 0; + for (int i = 0; i < count; i++) { + if (students[i].id == id) { + printf("ѧÉúID: %d, ÐÕÃû: %s, ³É¼¨: %d\n", students[i].id, students[i].name, students[i].score); + found = 1; + break; + } + } + + if (!found) { + printf("δÕÒµ½Ö¸¶¨IDµÄѧÉú¡£\n"); + } +} + +int main() { + Student students[MAX_STUDENTS]; + int count = 0; + int choice; + + do { + choice = displayMenu(); + switch (choice) { + case 1: + addStudent(students, &count); + break; + case 2: + displayStudents(students, count); + break; + case 3: + insertStudent(students, &count); + break; + case 4: + deleteStudent(students, &count); + break; + case 5: + modifyStudent(students, count); + break; + case 6: + queryStudent(students, count); + break; + case 7: + printf("Í˳öϵͳ¡£\n"); + break; + default: + printf("ÎÞЧµÄÑ¡Ôñ£¬ÇëÖØÐÂÊäÈë¡£\n"); + } + } while (choice != 7); + + return 0; +} +ÉÏÒ»·â ÏÂÒ»·â + +