成绩管理系统c语言代码

成绩排序:允许用户按照成绩对学生进行排序,可以选择升序或者降序。删除学生:允许用户根据学生姓名删除特定学生的记录。保存到文件:将学生信息保存到文件中,以便于持久化数据。从文件加载:从文件加载之前保存的学生信息,以便程序重新运行时恢复数据。异常处理:对用户的输入进行合理性检查,并提供错误处理机制。界面优化:改进用户界面,使其更加友好和直观。

c
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_STUDENTS 50 #define MAX_NAME_LENGTH 50 struct Student { char name[MAX_NAME_LENGTH]; float score; }; void addStudent(struct Student students[], int *numStudents); float calculateAverage(struct Student students[], int numStudents); void displayStudents(struct Student students[], int numStudents); void sortStudents(struct Student students[], int numStudents); void deleteStudent(struct Student students[], int *numStudents, char *name); int main() { struct Student students[MAX_STUDENTS]; int numStudents = 0; char choice; do { 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("请选择操作: "); scanf(" %c", &choice); switch (choice) { case '1': addStudent(students, &numStudents); break; case '2': printf("暂时不支持输入成绩功能\n"); break; case '3': printf("平均成绩为: %.2f\n", calculateAverage(students, numStudents)); break; case '4': displayStudents(students, numStudents); break; case '5': sortStudents(students, numStudents); break; case '6': printf("请输入要删除学生的姓名: "); char name[MAX_NAME_LENGTH]; scanf("%s", name); deleteStudent(students, &numStudents, name); break; case '7': printf("退出程序\n"); break; default: printf("无效选择\n"); } } while (choice != '7'); return 0; } void addStudent(struct Student students[], int *numStudents) { if (*numStudents < MAX_STUDENTS) { printf("请输入学生姓名: "); scanf("%s", students[*numStudents].name); printf("请输入学生成绩: "); scanf("%f", &students[*numStudents].score); (*numStudents)++; } else { printf("学生人数已达上限\n"); } } float calculateAverage(struct Student students[], int numStudents) { float totalScore = 0.0; for (int i = 0; i < numStudents; i++) { totalScore += students[i].score; } if (numStudents > 0) { return totalScore / numStudents; } else { return 0.0; } } void displayStudents(struct Student students[], int numStudents) { if (numStudents > 0) { printf("所有学生及其成绩:\n"); for (int i = 0; i < numStudents; i++) { printf("姓名: %s, 成绩: %.2f\n", students[i].name, students[i].score); } } else { printf("暂无学生记录\n"); } } void sortStudents(struct Student students[], int numStudents) { // 冒泡排序 for (int i = 0; i < numStudents - 1; i++) { for (int j = 0; j < numStudents - i - 1; j++) { if (students[j].score > students[j + 1].score) { // 交换学生位置 struct Student temp = students[j]; students[j] = students[j + 1]; students[j + 1] = temp; } } } printf("学生按成绩排序完成\n"); } void deleteStudent(struct Student students[], int *numStudents, char *name) { int found = 0; for (int i = 0; i < *numStudents; i++) { if (strcmp(students[i].name, name) == 0) { // 将后面的学生往前移动一个位置 for (int j = i; j < *numStudents - 1; j++) { students[j] = students[j + 1]; } (*numStudents)--; found = 1; printf("学生 %s 已删除\n", name); break; } } if (!found) { printf("未找到学生 %s\n", name); } }

这个版本的程序包含了更多功能,例如排序学生、删除学生等。这些修改可以提高程序的实用性和灵活性,使其更加适合真实的使用场景。