1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
| #include <stdio.h> #include <string.h>
#define MAX_STUDENTS 100 #define NAME_LENGTH 50
typedef struct { char name[NAME_LENGTH]; int id; float score; char grade; } Student;
void input_student(Student *student); void calculate_grade(Student *student); void print_student(const Student *student); float calculate_class_average(const Student students[], int count); void find_top_student(const Student students[], int count, Student *top); int count_passing_students(const Student students[], int count); void print_statistics(const Student students[], int count);
int main() { Student students[MAX_STUDENTS]; int student_count = 0; int choice; printf("学生成绩管理系统\n"); do { printf("\n=== 主菜单 ===\n"); printf("1. 添加学生\n"); printf("2. 显示所有学生\n"); printf("3. 显示统计信息\n"); printf("0. 退出\n"); printf("请选择:"); scanf("%d", &choice); switch (choice) { case 1: if (student_count < MAX_STUDENTS) { input_student(&students[student_count]); calculate_grade(&students[student_count]); student_count++; printf("学生添加成功!\n"); } else { printf("学生数量已达上限!\n"); } break; case 2: if (student_count == 0) { printf("暂无学生记录\n"); } else { printf("\n所有学生信息:\n"); for (int i = 0; i < student_count; i++) { print_student(&students[i]); } } break; case 3: if (student_count == 0) { printf("暂无学生记录\n"); } else { print_statistics(students, student_count); } break; case 0: printf("谢谢使用!\n"); break; default: printf("无效选择!\n"); break; } } while (choice != 0); return 0; }
void input_student(Student *student) { printf("请输入学生姓名:"); scanf("%s", student->name); printf("请输入学号:"); scanf("%d", &student->id); printf("请输入成绩:"); scanf("%f", &student->score); }
void calculate_grade(Student *student) { if (student->score >= 90) { student->grade = 'A'; } else if (student->score >= 80) { student->grade = 'B'; } else if (student->score >= 70) { student->grade = 'C'; } else if (student->score >= 60) { student->grade = 'D'; } else { student->grade = 'F'; } }
void print_student(const Student *student) { printf("姓名:%-10s 学号:%-8d 成绩:%6.1f 等级:%c\n", student->name, student->id, student->score, student->grade); }
float calculate_class_average(const Student students[], int count) { if (count == 0) return 0.0; float sum = 0.0; for (int i = 0; i < count; i++) { sum += students[i].score; } return sum / count; }
void find_top_student(const Student students[], int count, Student *top) { if (count == 0) return; *top = students[0]; for (int i = 1; i < count; i++) { if (students[i].score > top->score) { *top = students[i]; } } }
int count_passing_students(const Student students[], int count) { int passing = 0; for (int i = 0; i < count; i++) { if (students[i].score >= 60) { passing++; } } return passing; }
void print_statistics(const Student students[], int count) { printf("\n=== 班级统计信息 ===\n"); printf("总学生数:%d\n", count); float average = calculate_class_average(students, count); printf("平均成绩:%.2f\n", average); Student top_student; find_top_student(students, count, &top_student); printf("最高分学生:%s (%.1f分)\n", top_student.name, top_student.score); int passing = count_passing_students(students, count); printf("及格人数:%d\n", passing); printf("及格率:%.1f%%\n", (float)passing / count * 100); }
|