某班有M人(具体人数由键盘输入)参加期末考试,有N门课程(具体课程数由键盘输入)用C语言编程实现包括以下功能的菜单驱动的学生成绩管理系统。 功能介绍: (1) 录入功能:录入每个学生的学号、姓名和各科成绩,并计算每门课程及每个学生的总分和平均分 (2) 修改功能:根据学号修改学生的学号、姓名以及各门课成绩 (3) 删除功能:根据学号删除相应学生的相关信息 (4) 输出功能:显示所有学生的学号、姓名、各科成绩、总分和分均分 (5) 插入功能:插入新学生的相关信息 (6) 查询功能:根据学号查询该学生的相关信息 (7) 排序功能:根据总分和平均分分别对学生进行排序 (8) 系统以菜单方式工作
时间: 2024-03-18 08:39:16 浏览: 49
好的,以下是一个简单的菜单驱动的学生成绩管理系统的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
#define MAX_NAME_LEN 20
#define MAX_ID_LEN 10
typedef struct {
char id[MAX_ID_LEN];
char name[MAX_NAME_LEN];
int scores[MAX_SUBJECTS];
int total_score;
float avg_score;
} Student;
int num_students = 0;
int num_subjects = 0;
Student students[MAX_STUDENTS];
void print_menu() {
printf("\n");
printf("1. 录入学生成绩\n");
printf("2. 修改学生成绩\n");
printf("3. 删除学生成绩\n");
printf("4. 输出学生成绩\n");
printf("5. 插入学生成绩\n");
printf("6. 查询学生成绩\n");
printf("7. 排序学生成绩\n");
printf("0. 退出程序\n");
}
void input_students() {
printf("请输入学生人数:");
scanf("%d", &num_students);
printf("请输入科目数:");
scanf("%d", &num_subjects);
printf("请依次输入每个学生的学号、姓名和各科成绩:\n");
for (int i = 0; i < num_students; i++) {
printf("学生%d:", i + 1);
scanf("%s%s", students[i].id, students[i].name);
students[i].total_score = 0;
for (int j = 0; j < num_subjects; j++) {
scanf("%d", &students[i].scores[j]);
students[i].total_score += students[i].scores[j];
}
students[i].avg_score = (float)students[i].total_score / num_subjects;
}
printf("学生信息录入成功!\n");
}
void modify_student() {
char id[MAX_ID_LEN];
printf("请输入要修改的学生学号:");
scanf("%s", id);
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("请输入修改后的学号、姓名和各科成绩:");
scanf("%s%s", students[i].id, students[i].name);
students[i].total_score = 0;
for (int j = 0; j < num_subjects; j++) {
scanf("%d", &students[i].scores[j]);
students[i].total_score += students[i].scores[j];
}
students[i].avg_score = (float)students[i].total_score / num_subjects;
printf("学生信息修改成功!\n");
return;
}
}
printf("未找到该学生!\n");
}
void delete_student() {
char id[MAX_ID_LEN];
printf("请输入要删除的学生学号:");
scanf("%s", id);
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].id, id) == 0) {
for (int j = i; j < num_students - 1; j++) {
students[j] = students[j + 1];
}
num_students--;
printf("学生信息删除成功!\n");
return;
}
}
printf("未找到该学生!\n");
}
void output_students() {
printf("学号\t姓名");
for (int i = 0; i < num_subjects; i++) {
printf("\t科目%d", i + 1);
}
printf("\t总分\t平均分\n");
for (int i = 0; i < num_students; i++) {
printf("%s\t%s", students[i].id, students[i].name);
for (int j = 0; j < num_subjects; j++) {
printf("\t%d", students[i].scores[j]);
}
printf("\t%d\t%.2f\n", students[i].total_score, students[i].avg_score);
}
}
void insert_student() {
if (num_students >= MAX_STUDENTS) {
printf("学生信息已满,无法插入!\n");
return;
}
Student new_student;
printf("请输入要插入的学生学号、姓名和各科成绩:");
scanf("%s%s", new_student.id, new_student.name);
new_student.total_score = 0;
for (int i = 0; i < num_subjects; i++) {
scanf("%d", &new_student.scores[i]);
new_student.total_score += new_student.scores[i];
}
new_student.avg_score = (float)new_student.total_score / num_subjects;
students[num_students] = new_student;
num_students++;
printf("学生信息插入成功!\n");
}
void search_student() {
char id[MAX_ID_LEN];
printf("请输入要查询的学生学号:");
scanf("%s", id);
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("学号\t姓名");
for (int j = 0; j < num_subjects; j++) {
printf("\t科目%d", j + 1);
}
printf("\t总分\t平均分\n");
printf("%s\t%s", students[i].id, students[i].name);
for (int j = 0; j < num_subjects; j++) {
printf("\t%d", students[i].scores[j]);
}
printf("\t%d\t%.2f\n", students[i].total_score, students[i].avg_score);
return;
}
}
printf("未找到该学生!\n");
}
void sort_students_by_total() {
for (int i = 0; i < num_students - 1; i++) {
for (int j = i + 1; j < num_students; j++) {
if (students[i].total_score < students[j].total_score) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("按总分排序成功!\n");
}
void sort_students_by_avg() {
for (int i = 0; i < num_students - 1; i++) {
for (int j = i + 1; j < num_students; j++) {
if (students[i].avg_score < students[j].avg_score) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("按平均分排序成功!\n");
}
int main() {
int choice;
while (1) {
print_menu();
printf("请输入选项:");
scanf("%d", &choice);
switch (choice) {
case 0:
printf("程序已退出!\n");
exit(0);
case 1:
input_students();
break;
case 2:
modify_student();
break;
case 3:
delete_student();
break;
case 4:
output_students();
break;
case 5:
insert_student();
break;
case 6:
search_student();
break;
case 7:
printf("请选择排序方式(1. 按总分排序 2. 按平均分排序):");
scanf("%d", &choice);
if (choice == 1) {
sort_students_by_total();
} else if (choice == 2) {
sort_students_by_avg();
} else {
printf("无效的选项!\n");
}
break;
default:
printf("无效的选项!\n");
break;
}
}
return 0;
}
```
这个示例代码实现了您所需的所有功能,可以根据您的实际需求进行修改和调整。
阅读全文