学生成绩管理系统课程设计c语言
时间: 2023-12-08 11:05:39 浏览: 100
学生成绩管理系统是一个常见的课程设计项目,下面是一个简单的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 add_student() {
if (student_num >= MAX_STUDENT_NUM) {
printf("学生数已达到最大值,无法添加新学生!\n");
return;
}
Student new_student;
printf("请输入学生姓名:");
scanf("%s", new_student.name);
printf("请输入学生学号:");
scanf("%d", &new_student.id);
printf("请输入学生各科目成绩:");
for (int i = 0; i < MAX_SUBJECT_NUM; i++) {
scanf("%f", &new_student.scores[i]);
}
// 计算平均分
float sum = 0;
for (int i = 0; i < MAX_SUBJECT_NUM; i++) {
sum += new_student.scores[i];
}
new_student.avg_score = sum / MAX_SUBJECT_NUM;
students[student_num++] = new_student;
printf("添加学生成功!\n");
}
// 显示学生信息
void show_student(Student student) {
printf("%s\t%d\t", student.name, student.id);
for (int i = 0; i < MAX_SUBJECT_NUM; i++) {
printf("%.1f\t", student.scores[i]);
}
printf("%.1f\n", student.avg_score);
}
// 显示所有学生信息
void show_all_students() {
printf("姓名\t学号\t");
for (int i = 0; i < MAX_SUBJECT_NUM; i++) {
printf("科目%d\t", i + 1);
}
printf("平均分\n");
for (int i = 0; i < student_num; i++) {
show_student(students[i]);
}
}
// 按平均分排序
void sort_by_avg_score() {
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 search_by_id() {
int id;
printf("请输入要查询的学生学号:");
scanf("%d", &id);
for (int i = 0; i < student_num; i++) {
if (students[i].id == id) {
show_student(students[i]);
return;
}
}
printf("未找到该学生!\n");
}
// 删除学生
void delete_student() {
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 calculate_avg_score() {
float sum = 0;
for (int i = 0; i < student_num; i++) {
sum += students[i].avg_score;
}
float avg_score = sum / student_num;
printf("平均分为:%.1f\n", avg_score);
}
int main() {
int choice;
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");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
show_all_students();
break;
case 3:
sort_by_avg_score();
break;
case 4:
search_by_id();
break;
case 5:
delete_student();
break;
case 6:
calculate_avg_score();
break;
case 7:
exit(0);
default:
printf("无效的选择!\n");
}
}
return 0;
}
```
阅读全文