用c语言写一个学生成绩管理系统
时间: 2023-11-17 22:06:06 浏览: 59
好的,下面是一个简单的学生成绩管理系统的 C 语言代码实现,包括添加学生信息、查询学生信息、修改学生信息、删除学生信息、计算总分和平均分等功能。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 100 // 学生数量上限
#define NAME_LENGTH 20 // 姓名长度上限
// 学生信息结构体
struct Student {
char name[NAME_LENGTH]; // 姓名
int math_score; // 数学成绩
int english_score; // 英语成绩
int computer_score; // 计算机成绩
int total_score; // 总分
float avg_score; // 平均分
};
// 学生信息数组
struct Student students[MAX_STUDENT_NUM];
int student_num = 0; // 当前学生数量
// 添加学生信息
void add_student() {
if (student_num >= MAX_STUDENT_NUM) {
printf("学生数量已达到上限,无法添加新学生!\n");
return;
}
char name[NAME_LENGTH];
int math_score, english_score, computer_score;
printf("请输入学生姓名:");
scanf("%s", name);
printf("请输入数学成绩:");
scanf("%d", &math_score);
printf("请输入英语成绩:");
scanf("%d", &english_score);
printf("请输入计算机成绩:");
scanf("%d", &computer_score);
struct Student student;
strcpy(student.name, name);
student.math_score = math_score;
student.english_score = english_score;
student.computer_score = computer_score;
student.total_score = math_score + english_score + computer_score;
student.avg_score = (float)student.total_score / 3;
students[student_num++] = student;
printf("学生信息添加成功!\n");
}
// 查询学生信息
void query_student() {
if (student_num == 0) {
printf("当前没有学生信息!\n");
return;
}
char name[NAME_LENGTH];
printf("请输入要查询的学生姓名:");
scanf("%s", name);
int found = 0;
for (int i = 0; i < student_num; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("姓名:%s\n", students[i].name);
printf("数学成绩:%d\n", students[i].math_score);
printf("英语成绩:%d\n", students[i].english_score);
printf("计算机成绩:%d\n", students[i].computer_score);
printf("总分:%d\n", students[i].total_score);
printf("平均分:%.2f\n", students[i].avg_score);
found = 1;
break;
}
}
if (!found) {
printf("找不到该学生的信息!\n");
}
}
// 修改学生信息
void modify_student() {
if (student_num == 0) {
printf("当前没有学生信息!\n");
return;
}
char name[NAME_LENGTH];
printf("请输入要修改的学生姓名:");
scanf("%s", name);
int found = 0;
for (int i = 0; i < student_num; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("请输入新的数学成绩:");
scanf("%d", &students[i].math_score);
printf("请输入新的英语成绩:");
scanf("%d", &students[i].english_score);
printf("请输入新的计算机成绩:");
scanf("%d", &students[i].computer_score);
students[i].total_score = students[i].math_score + students[i].english_score + students[i].computer_score;
students[i].avg_score = (float)students[i].total_score / 3;
printf("学生信息修改成功!\n");
found = 1;
break;
}
}
if (!found) {
printf("找不到该学生的信息!\n");
}
}
// 删除学生信息
void delete_student() {
if (student_num == 0) {
printf("当前没有学生信息!\n");
return;
}
char name[NAME_LENGTH];
printf("请输入要删除的学生姓名:");
scanf("%s", name);
int found = 0;
for (int i = 0; i < student_num; i++) {
if (strcmp(students[i].name, name) == 0) {
for (int j = i; j < student_num - 1; j++) {
students[j] = students[j + 1];
}
student_num--;
printf("学生信息删除成功!\n");
found = 1;
break;
}
}
if (!found) {
printf("找不到该学生的信息!\n");
}
}
// 计算总分和平均分
void compute_score() {
if (student_num == 0) {
printf("当前没有学生信息!\n");
return;
}
int total_score = 0;
for (int i = 0; i < student_num; i++) {
total_score += students[i].total_score;
}
float avg_score = (float)total_score / student_num;
printf("学生总分:%d\n", total_score);
printf("学生平均分:%.2f\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("请输入要执行的操作编号:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
query_student();
break;
case 3:
modify_student();
break;
case 4:
delete_student();
break;
case 5:
compute_score();
break;
case 6:
printf("程序已退出!\n");
exit(0);
default:
printf("无效操作,请重新输入!\n");
break;
}
}
return 0;
}
```
这个学生成绩管理系统可以通过菜单选择实现不同的操作,包括添加学生信息、查询学生信息、修改学生信息、删除学生信息、计算总分和平均分等功能。你可以根据自己的需要进行修改和完善。
阅读全文