学生成绩管理系统设计c语言
时间: 2023-12-17 08:06:04 浏览: 97
C语言学生成绩管理系统设计
以下是一个简单的学生成绩管理系统的C语言实现,包括学生信息的输入、输出、查询、排序、删除、统计和退出等功能。具体实现如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 100 // 最大学生数
#define MAX_NAME_LEN 20 // 最大姓名长度
#define MAX_SUBJECT_NUM 5 // 最大科目数
// 学生结构体
typedef struct {
char name[MAX_NAME_LEN]; // 姓名
int id; // 学号
float scores[MAX_SUBJECT_NUM]; // 各科成绩
float avg_score; // 平均成绩
} Student;
// 学生数组
Student students[MAX_STUDENT_NUM];
int student_num = 0; // 学生数
// 显示菜单
void show_menu() {
printf("1. 添加学生信息\n");
printf("2. 显示所有学生信息\n");
printf("3. 查询学生信息\n");
printf("4. 按平均成绩排序\n");
printf("5. 删除学生信息\n");
printf("6. 统计学生信息\n");
printf("7. 退出\n");
}
// 添加学生信息
void add_student() {
if (student_num >= MAX_STUDENT_NUM) {
printf("学生数已达到最大值,无法添加新学生!\n");
return;
}
Student student;
printf("请输入学生姓名:");
scanf("%s", student.name);
printf("请输入学生学号:");
scanf("%d", &student.id);
printf("请输入学生各科成绩:");
for (int i = 0; i < MAX_SUBJECT_NUM; i++) {
scanf("%f", &student.scores[i]);
}
// 计算平均成绩
float sum = 0;
for (int i = 0; i < MAX_SUBJECT_NUM; i++) {
sum += student.scores[i];
}
student.avg_score = sum / MAX_SUBJECT_NUM;
students[student_num++] = student;
printf("添加成功!\n");
}
// 显示所有学生信息
void show_all_students() {
if (student_num == 0) {
printf("没有学生信息!\n");
return;
}
printf("姓名\t学号\t语文\t数学\t英语\t物理\t化学\t平均成绩\n");
for (int i = 0; i < student_num; i++) {
printf("%s\t%d\t", students[i].name, students[i].id);
for (int j = 0; j < MAX_SUBJECT_NUM; j++) {
printf("%.1f\t", students[i].scores[j]);
}
printf("%.1f\n", students[i].avg_score);
}
}
// 查询学生信息
void search_student() {
if (student_num == 0) {
printf("没有学生信息!\n");
return;
}
int id;
printf("请输入要查询的学生学号:");
scanf("%d", &id);
for (int i = 0; i < student_num; i++) {
if (students[i].id == id) {
printf("姓名\t学号\t语文\t数学\t英语\t物理\t化学\t平均成绩\n");
printf("%s\t%d\t", students[i].name, students[i].id);
for (int j = 0; j < MAX_SUBJECT_NUM; j++) {
printf("%.1f\t", students[i].scores[j]);
}
printf("%.1f\n", students[i].avg_score);
return;
}
}
printf("没有找到该学生!\n");
}
// 按平均成绩排序
void sort_by_avg_score() {
if (student_num == 0) {
printf("没有学生信息!\n");
return;
}
// 冒泡排序
for (int i = 0; i < student_num - 1; i++) {
for (int j = 0; j < student_num - i - 1; j++) {
if (students[j].avg_score < students[j + 1].avg_score) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printf("排序成功!\n");
}
// 删除学生信息
void delete_student() {
if (student_num == 0) {
printf("没有学生信息!\n");
return;
}
int id;
printf("请输入要删除的学生学号:");
scanf("%d", &id);
for (int i = 0; i < student_num; i++) {
if (students[i].id == id) {
for (int j = i; j < student_num - 1; j++) {
students[j] = students[j + 1];
}
student_num--;
printf("删除成功!\n");
return;
}
}
printf("没有找到该学生!\n");
}
// 统计学生信息
void count_student() {
if (student_num == 0) {
printf("没有学生信息!\n");
return;
}
float sum[MAX_SUBJECT_NUM] = {0};
float avg[MAX_SUBJECT_NUM] = {0};
float max_score[MAX_SUBJECT_NUM] = {0};
float min_score[MAX_SUBJECT_NUM] = {100};
for (int i = 0; i < student_num; i++) {
for (int j = 0; j < MAX_SUBJECT_NUM; j++) {
sum[j] += students[i].scores[j];
if (students[i].scores[j] > max_score[j]) {
max_score[j] = students[i].scores[j];
}
if (students[i].scores[j] < min_score[j]) {
min_score[j] = students[i].scores[j];
}
}
}
for (int i = 0; i < MAX_SUBJECT_NUM; i++) {
avg[i] = sum[i] / student_num;
}
printf("科目\t平均分\t最高分\t最低分\n");
printf("语文\t%.1f\t%.1f\t%.1f\n", avg[0], max_score[0], min_score[0]);
printf("数学\t%.1f\t%.1f\t%.1f\n", avg[1], max_score[1], min_score[1]);
printf("英语\t%.1f\t%.1f\t%.1f\n", avg[2], max_score[2], min_score[2]);
printf("物理\t%.1f\t%.1f\t%.1f\n", avg[3], max_score[3], min_score[3]);
printf("化学\t%.1f\t%.1f\t%.1f\n", avg[4], max_score[4], min_score[4]);
}
int main() {
int choice;
while (1) {
show_menu();
printf("请输入操作编号:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
show_all_students();
break;
case 3:
search_student();
break;
case 4:
sort_by_avg_score();
break;
case 5:
delete_student();
break;
case 6:
count_student();
break;
case 7:
printf("谢谢使用!\n");
exit(0);
default:
printf("无效的操作编号!\n");
break;
}
}
return 0;
}
```
阅读全文