C语言学生成绩管理系统设计与实现
#include <stdio.h> #include <string.h>
#define MAX_STUDENT 100 // 最大学生数量 #define MAX_NAME 20 // 姓名最大长度
// 学生结构体 struct Student { char name[MAX_NAME]; // 姓名 int id; // 学号 int score1; // 第一门课成绩 int score2; // 第二门课成绩 int score3; // 第三门课成绩 };
// 学生成绩管理系统 struct GradeManagementSystem { struct Student students[MAX_STUDENT]; // 学生列表 int count; // 学生数量 };
// 添加学生信息 void addStudent(struct GradeManagementSystem* system) { struct Student student; printf("请输入学生信息:\n"); printf("姓名:"); scanf("%s", student.name); printf("学号:"); scanf("%d", &student.id); printf("第一门课成绩:"); scanf("%d", &student.score1); printf("第二门课成绩:"); scanf("%d", &student.score2); printf("第三门课成绩:"); scanf("%d", &student.score3); system->students[system->count++] = student; printf("添加成功!\n"); }
// 显示学生信息 void showStudents(struct GradeManagementSystem* system) { printf("学生信息如下:\n"); printf("姓名\t学号\t第一门课成绩\t第二门课成绩\t第三门课成绩\n"); for (int i = 0; i < system->count; i++) { struct Student student = system->students[i]; printf("%s\t%d\t%d\t\t%d\t\t%d\n", student.name, student.id, student.score1, student.score2, student.score3); } }
// 显示某门课成绩的最高分 void showMaxScore(struct GradeManagementSystem* system, int course) { int max = -1; for (int i = 0; i < system->count; i++) { struct Student student = system->students[i]; int score = 0; if (course == 1) { score = student.score1; } else if (course == 2) { score = student.score2; } else if (course == 3) { score = student.score3; } if (score > max) { max = score; } } printf("第%d门课成绩的最高分为:%d\n", course, max); }
// 显示某门课成绩的最低分 void showMinScore(struct GradeManagementSystem* system, int course) { int min = 101; for (int i = 0; i < system->count; i++) { struct Student student = system->students[i]; int score = 0; if (course == 1) { score = student.score1; } else if (course == 2) { score = student.score2; } else if (course == 3) { score = student.score3; } if (score < min) { min = score; } } printf("第%d门课成绩的最低分为:%d\n", course, min); }
// 显示某门课成绩的平均分 void showAvgScore(struct GradeManagementSystem* system, int course) { int sum = 0; for (int i = 0; i < system->count; i++) { struct Student student = system->students[i]; int score = 0; if (course == 1) { score = student.score1; } else if (course == 2) { score = student.score2; } else if (course == 3) { score = student.score3; } sum += score; } double avg = (double)sum / system->count; printf("第%d门课成绩的平均分为:%.2lf\n", course, avg); }
// 从高到低显示某门课程的成绩 void showScoresInDesc(struct GradeManagementSystem* system, int course) { int scores[MAX_STUDENT]; for (int i = 0; i < system->count; i++) { struct Student student = system->students[i]; int score = 0; if (course == 1) { score = student.score1; } else if (course == 2) { score = student.score2; } else if (course == 3) { score = student.score3; } scores[i] = score; } for (int i = 0; i < system->count - 1; i++) { for (int j = i + 1; j < system->count; j++) { if (scores[i] < scores[j]) { int temp = scores[i]; scores[i] = scores[j]; scores[j] = temp; } } } printf("第%d门课成绩从高到低排列如下:\n", course); printf("排名\t学号\t成绩\n"); for (int i = 0; i < system->count; i++) { for (int j = 0; j < system->count; j++) { struct Student student = system->students[j]; int score = 0; if (course == 1) { score = student.score1; } else if (course == 2) { score = student.score2; } else if (course == 3) { score = student.score3; } if (scores[i] == score) { printf("%d\t%d\t%d\n", i + 1, student.id, score); break; } } } }
// 按学号查找某个学生的三门课成绩 void findStudentById(struct GradeManagementSystem* system, int id) { int found = 0; for (int i = 0; i < system->count; i++) { struct Student student = system->students[i]; if (student.id == id) { printf("学生%s的三门课成绩为:%d\t%d\t%d\n", student.name, student.score1, student.score2, student.score3); found = 1; break; } } if (!found) { printf("找不到该学生!\n"); } }
// 按姓名查找某个学生的三门课成绩 void findStudentByName(struct GradeManagementSystem* system, char* name) { int found = 0; for (int i = 0; i < system->count; i++) { struct Student student = system->students[i]; if (strcmp(student.name, name) == 0) { printf("学生%d的三门课成绩为:%d\t%d\t%d\n", student.id, student.score1, student.score2, student.score3); found = 1; break; } } if (!found) { printf("找不到该学生!\n"); } }
// 主函数 int main() { struct GradeManagementSystem system; system.count = 0; while (1) { printf("请输入操作编号:\n"); printf("1. 添加学生信息\n"); printf("2. 显示学生信息\n"); printf("3. 显示某门课成绩的最高分\n"); printf("4. 显示某门课成绩的最低分\n"); printf("5. 显示某门课成绩的平均分\n"); printf("6. 从高到低显示某门课程的成绩\n"); printf("7. 按学号查找某个学生的三门课成绩\n"); printf("8. 按姓名查找某个学生的三门课成绩\n"); printf("9. 退出\n"); int option; scanf("%d", &option); if (option == 1) { addStudent(&system); } else if (option == 2) { showStudents(&system); } else if (option == 3) { printf("请输入要查询的课程编号(1-3):\n"); int course; scanf("%d", &course); showMaxScore(&system, course); } else if (option == 4) { printf("请输入要查询的课程编号(1-3):\n"); int course; scanf("%d", &course); showMinScore(&system, course); } else if (option == 5) { printf("请输入要查询的课程编号(1-3):\n"); int course; scanf("%d", &course); showAvgScore(&system, course); } else if (option == 6) { printf("请输入要查询的课程编号(1-3):\n"); int course; scanf("%d", &course); showScoresInDesc(&system, course); } else if (option == 7) { printf("请输入要查询的学生学号:\n"); int id; scanf("%d", &id); findStudentById(&system, id); } else if (option == 8) { printf("请输入要查询的学生姓名:\n"); char name[MAX_NAME]; scanf("%s", name); findStudentByName(&system, name); } else if (option == 9) { printf("谢谢使用!\n"); break; } else { printf("无效的操作编号!\n"); } } return 0; }
原文地址: https://www.cveoy.top/t/topic/ohOC 著作权归作者所有。请勿转载和采集!