From 06d00f2933a031399ebe53c990ad98852299e06f Mon Sep 17 00:00:00 2001 From: p8uklna2v <3500701770@qq.com> Date: Thu, 21 Nov 2024 21:57:06 +0800 Subject: [PATCH] ADD file via upload --- 学籍管理 | 330 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 330 insertions(+) create mode 100644 学籍管理 diff --git a/学籍管理 b/学籍管理 new file mode 100644 index 0000000..5388b49 --- /dev/null +++ b/学籍管理 @@ -0,0 +1,330 @@ +#include +#include +#include + +// ѧṹ +struct Student { + char *id; + char *clas; + char *name; + double score1; + double score2; + double score3; + double score; +}; + +// +void displayMenu(); +void inputStudentInfo(struct Student students[], int *count); +void outputStudentInfo(struct Student students[], int count); +void orderStudentInfo(struct Student students[], int count); +void insertStudentInfo(struct Student students[], int *count); +void deleteStudentInfo(struct Student students[], int *count); +void modifyStudentInfo(struct Student students[], int count); +void searchStudentInfo(struct Student students[], int count); + +int main() { + struct Student students[100]; + int count = 0; + int option; + + do { + displayMenu(); + scanf("%d", &option); + + switch (option) { + case 1: + inputStudentInfo(students, &count); + break; + case 2: + outputStudentInfo(students, count); + break; + case 3: + orderStudentInfo(students, count); + break; + case 4: + insertStudentInfo(students, &count); + break; + case 5: + deleteStudentInfo(students, &count); + break; + case 6: + modifyStudentInfo(students, count); + break; + case 7: + searchStudentInfo(students, count); + break; + case 8: + printf("Exiting the program.\n"); + break; + default: + printf("Invalid option. Please try again.\n"); + } + } while (option!= 8); + + return 0; +} + +// ʾ˵ +void displayMenu() { + printf(" 1. Input\n"); + printf(" 2. Output\n"); + printf(" 3. Order\n"); + printf(" 4. Insert\n"); + printf(" 5. Delete\n"); + printf(" 6. Modify\n"); + printf(" 7. Search\n"); + printf(" 8. Quit\n"); + printf("Please enter your option: "); +} + +// ¼ѧϢ +void inputStudentInfo(struct Student students[], int *count) { + for (int i = *count; i < *count + 3; i++) { + students[i].id = (char *)malloc(sizeof(char) * 10); + students[i].clas = (char *)malloc(sizeof(char) * 10); + students[i].name = (char *)malloc(sizeof(char) * 20); + + printf("Please input student %d's id: ", i + 1); + scanf("%s", students[i].id); + + printf("Please input student %d's class: ", i + 1); + scanf("%s", students[i].clas); + + printf("Please input student %d's name: ", i + 1); + scanf("%s", students[i].name); + + printf("Please input student %d's score1: ", i + 1); + scanf("%lf", &students[i].score1); + + printf("Please input student %d's score2: ", i + 1); + scanf("%lf", &students[i].score2); + + printf("Please input student %d's score3: ", i + 1); + scanf("%lf", &students[i].score3); + + students[i].score = students[i].score1 + students[i].score2 + students[i].score3; + } + *count += 3; +} + +// ѧϢ +void outputStudentInfo(struct Student students[], int count) { + for (int i = 0; i < count; i++) { + printf("%s %s %s %.1lf %.1lf %.1lf %.1lf\n", students[i].id, students[i].clas, students[i].name, + students[i].score1, students[i].score2, students[i].score3, students[i].score); + } +} + +// ѧϢ +void orderStudentInfo(struct Student students[], int count) { + for (int i = 0; i < count - 1; i++) { + for (int j = 0; j < count - i - 1; j++) { + if (strcmp(students[j].clas, students[j + 1].clas) > 0 || + (strcmp(students[j].clas, students[j + 1].clas) == 0 && students[j].score < students[j + 1].score)) { + struct Student temp = students[j]; + students[j] = students[j + 1]; + students[j + 1] = temp; + } + } + } + outputStudentInfo(students, count); +} + +// ѧϢ +void insertStudentInfo(struct Student students[], int *count) { + struct Student newStudent; + newStudent.id = (char *)malloc(sizeof(char) * 10); + newStudent.clas = (char *)malloc(sizeof(char) * 10); + newStudent.name = (char *)malloc(sizeof(char) * 20); + + printf("Please input the new student's id: "); + scanf("%s", newStudent.id); + + printf("Please input the new student's class: "); + scanf("%s", newStudent.clas); + + printf("Please input the new student's name: "); + scanf("%s", newStudent.name); + + printf("Please input the new student's score1: "); + scanf("%lf", &newStudent.score1); + + printf("Please input the new student's score2: "); + scanf("%lf", &newStudent.score2); + + printf("Please input the new student's score3: "); + scanf("%lf", &newStudent.score3); + + newStudent.score = newStudent.score1 + newStudent.score2 + newStudent.score3; + + int i; + for (i = 0; i < *count; i++) { + if (strcmp(students[i].clas, newStudent.clas) > 0 || + (strcmp(students[i].clas, newStudent.clas) == 0 && students[i].score < newStudent.score)) { + break; + } + } + + for (int j = *count; j > i; j--) { + students[j] = students[j - 1]; + } + + students[i] = newStudent; + (*count)++; + outputStudentInfo(students, *count); +} + +// ɾѧϢ +void deleteStudentInfo(struct Student students[], int *count) { + char idOrName[20]; + printf("Please input the id or name of the student to delete: "); + scanf("%s", idOrName); + + int i; + for (i = 0; i < *count; i++) { + if (strcmp(students[i].id, idOrName) == 0 || strcmp(students[i].name, idOrName) == 0) { + break; + } + } + + if (i == *count) { + printf("Student not found.\n"); + } else { + for (int j = i; j < *count - 1; j++) { + students[j] = students[j + 1]; + } + (*count)--; + outputStudentInfo(students, *count); + } +} + +// ޸ѧϢ +void modifyStudentInfo(struct Student students[], int count) { + char id[10]; + printf("Please input the id of the student to modify: "); + scanf("%s", id); + + int i; + for (i = 0; i < count; i++) { + if (strcmp(students[i].id, id) == 0) { + break; + } + } + + if (i == count) { + printf("Student not found.\n"); + } else { + free(students[i].id); + free(students[i].clas); + free(students[i].name); + + students[i].id = (char *)malloc(sizeof(char) * 10); + students[i].clas = (char *)malloc(sizeof(char) * 10); + students[i].name = (char *)malloc(sizeof(char) * 20); + + printf("Please input the new class: "); + scanf("%s", students[i].clas); + + printf("Please input the new name: "); + scanf("%s", students[i].name); + + printf("Please input the new score1: "); + scanf("%lf", &students[i].score1); + + printf("Please input the new score2: "); + scanf("%lf", &students[i].score2); + + printf("Please input the new score3: "); + scanf("%lf", &students[i].score3); + + students[i].score = students[i].score1 + students[i].score2 + students[i].score3; + + orderStudentInfo(students, count); + } +} + +// ѯѧϢ +void searchStudentInfo(struct Student students[], int count) { + int option; + printf("Please select the search option:\n"); + printf("1. Search by class range\n"); + printf("2. Search by id range\n"); + printf("3. Search by name prefix\n"); + printf("4. Search by total score\n"); + printf("5. Search by class and id range\n"); + scanf("%d", &option); + + switch (option) { + case 1: { + int startClass, endClass; + printf("Please input the start class and end class (e.g., 11 13): "); + scanf("%d %d", &startClass, &endClass); + + for (int i = 0; i < count; i++) { + int classValue = atoi(students[i].clas); + if (classValue >= startClass && classValue <= endClass) { + printf("%s %s %s %.1lf %.1lf %.1lf %.1lf\n", students[i].id, students[i].clas, students[i].name, + students[i].score1, students[i].score2, students[i].score3, students[i].score); + } + } + break; + } + case 2: { + char startId[10], endId[10]; + printf("Please input the start id and end id (e.g., 10001 10004): "); + scanf("%s %s", startId, endId); + + for (int i = 0; i < count; i++) { + if (strcmp(students[i].id, startId) >= 0 && strcmp(students[i].id, endId) <= 0) { + printf("%s %s %s %.1lf %.1lf %.1lf %.1lf\n", students[i].id, students[i].clas, students[i].name, + students[i].score1, students[i].score2, students[i].score3, students[i].score); + } + } + break; + } + case 3: { + char prefix[10]; + printf("Please input the name prefix (e.g., Zh): "); + scanf("%s", prefix); + + for (int i = 0; i < count; i++) { + if (strncmp(students[i].name, prefix, strlen(prefix)) == 0) { + printf("%s %s %s %.1lf %.1lf %.1lf %.1lf\n", students[i].id, students[i].clas, students[i].name, + students[i].score1, students[i].score2, students[i].score3, students[i].score); + } + } + break; + } + case 4: { + double minScore; + printf("Please input the minimum total score: "); + scanf("%lf", &minScore); + + for (int i = 0; i < count; i++) { + if (students[i].score >= minScore) { + printf("%s %s %s %.1lf %.1lf %.1lf %.1lf\n", students[i].id, students[i].clas, students[i].name, + students[i].score1, students[i].score2, students[i].score3, students[i].score); + } + } + break; + } + case 5: { + int classValue; + char startId[10], endId[10]; + printf("Please input the class and the start id and end id (e.g., 11 10001 10004): "); + scanf("%d %s %s", &classValue, startId, endId); + + for (int i = 0; i < count; i++) { + if (atoi(students[i].clas) == classValue && + strcmp(students[i].id, startId) >= 0 && strcmp(students[i].id, endId) <= 0) { + printf("%s %s %s %.1lf %.1lf %.1lf %.1lf\n", students[i].id, students[i].clas, students[i].name, + students[i].score1, students[i].score2, students[i].score3, students[i].score); + } + } + break; + } + default: + printf("Invalid search option.\n"); + } +}