diff --git a/8th.c b/8th.c new file mode 100644 index 0000000..a5267f1 --- /dev/null +++ b/8th.c @@ -0,0 +1,156 @@ +#include +#include +#include +#define MAX_STUDENTS 100 +struct Student { + char *id; + char *clas; + char *name; + double score1; + double score2; + double score3; + double totalScore; +}; +struct Student students[MAX_STUDENTS]; +int studentCount = 0; +// 函数声明 +void menu(); +void inputStudent(); +void deleteStudent(); +void selectStudent(); +void orderStudents(); +void outputStudents(); +void freeMemory(); +// 主函数 +int main() { + int option; + do { + menu(); + printf("please input your option: \n"); + scanf("%d", &option); + switch(option) { + case 1: inputStudent(); break; + case 2: deleteStudent(); break; + case 3: selectStudent(); break; + case 4: orderStudents(); break; + case 5: outputStudents(); break; + case 6: freeMemory(); return 0; // 清理内存并退出 + } + } while (option != 6); + return 0; +} +// 菜单函数 +void menu() { + printf("1.input\n2.delete\n3.select\n4.order\n5.output\n6.quit\n"); +} +// 输入学生信息 +void inputStudent() { + char continueInput[4]; + do { + students[studentCount].id = (char *)malloc(20 * sizeof(char)); + students[studentCount].clas = (char *)malloc(20 * sizeof(char)); + students[studentCount].name = (char *)malloc(20 * sizeof(char)); + + printf("Id "); + scanf("%s", students[studentCount].id); + printf("class "); + scanf("%s", students[studentCount].clas); + printf("name "); + scanf("%s", students[studentCount].name); + printf("score1 "); + scanf("%lf", &students[studentCount].score1); + printf("score2 "); + scanf("%lf", &students[studentCount].score2); + printf("score3 "); + scanf("%lf", &students[studentCount].score3); + + students[studentCount].totalScore = students[studentCount].score1 + students[studentCount].score2 + students[studentCount].score3; + studentCount++; + printf("continue? \n"); + scanf("%s", continueInput); + } while (strcmp(continueInput, "yes") == 0); +} +// 删除学生信息 +void deleteStudent() { + char target[20]; + char continueDelete[4]; + int found = 0; + do { + found = 0; + scanf("%s", target); + int i1; + for (i1 = 0; i1 < studentCount; i1++) { + if (strcmp(students[i1].id, target) == 0 || strcmp(students[i1].name, target) == 0) { + free(students[i1].id); + free(students[i1].clas); + free(students[i1].name); + memmove(&students[i1], &students[i1+1], (studentCount - i1 - 1) * sizeof(struct Student)); + studentCount--; + found = 1; + int l; + for(l =0;l 0 || + (strcmp(students[j].clas, students[j + 1].clas) == 0 && students[j].totalScore < students[j + 1].totalScore)) { + struct Student temp = students[j]; + students[j] = students[j + 1]; + students[j + 1] = temp; + } + } + } + outputStudents(); +} +void outputStudents() { + + int i4; + for (i4 = 0; i4 < studentCount; i4++) { + printf("%s,%s,%s,%.1f,%.1f,%.1f,%.1f\n", students[i4].id, students[i4].clas, students[i4].name, students[i4].score1, students[i4].score2, students[i4].score3, students[i4].totalScore); + } +} +void freeMemory(){ + int i; + for(i = 0;i