用c语言写一个学生管理系统
时间: 2023-11-25 09:07:14 浏览: 95
好的,下面是一个简单的学生管理系统的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 100 // 最大学生数
#define MAX_NAME_LENGTH 20 // 姓名最大长度
#define MAX_COURSE_NUM 5 // 最大课程数
typedef struct Student {
char name[MAX_NAME_LENGTH];
int age;
float scores[MAX_COURSE_NUM];
float average_score;
} Student;
int student_num = 0;
Student student_list[MAX_STUDENT_NUM];
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.age);
for (int i = 0; i < MAX_COURSE_NUM; i++) {
printf("请输入第%d门课程成绩:", i + 1);
scanf("%f", &new_student.scores[i]);
}
new_student.average_score = 0;
for (int i = 0; i < MAX_COURSE_NUM; i++) {
new_student.average_score += new_student.scores[i];
}
new_student.average_score /= MAX_COURSE_NUM;
student_list[student_num++] = new_student;
printf("添加学生成功!\n");
}
void show_all_students() {
if (student_num == 0) {
printf("没有学生记录!\n");
return;
}
printf("| %-10s | %-5s |", "姓名", "年龄");
for (int i = 0; i < MAX_COURSE_NUM; i++) {
printf(" 第%d门课 ", i + 1);
}
printf("| 平均分 |\n");
printf("+------------+-------+");
for (int i = 0; i < MAX_COURSE_NUM; i++) {
printf("+-------");
}
printf("+---------+\n");
for (int i = 0; i < student_num; i++) {
printf("| %-10s | %-5d |", student_list[i].name, student_list[i].age);
for (int j = 0; j < MAX_COURSE_NUM; j++) {
printf(" %-6.2f ", student_list[i].scores[j]);
}
printf("| %-7.2f |\n", student_list[i].average_score);
}
}
void search_student() {
char name[MAX_NAME_LENGTH];
printf("请输入要查找的学生姓名:");
scanf("%s", name);
for (int i = 0; i < student_num; i++) {
if (strcmp(student_list[i].name, name) == 0) {
printf("| %-10s | %-5s |", "姓名", "年龄");
for (int j = 0; j < MAX_COURSE_NUM; j++) {
printf(" 第%d门课 ", j + 1);
}
printf("| 平均分 |\n");
printf("+------------+-------+");
for (int j = 0; j < MAX_COURSE_NUM; j++) {
printf("+-------");
}
printf("+---------+\n");
printf("| %-10s | %-5d |", student_list[i].name, student_list[i].age);
for (int j = 0; j < MAX_COURSE_NUM; j++) {
printf(" %-6.2f ", student_list[i].scores[j]);
}
printf("| %-7.2f |\n", student_list[i].average_score);
return;
}
}
printf("没有找到该学生记录!\n");
}
void menu() {
printf("学生管理系统菜单:\n");
printf("1. 添加学生\n");
printf("2. 查看所有学生\n");
printf("3. 查找学生\n");
printf("4. 退出程序\n");
printf("请选择操作:");
}
int main() {
int choice;
while (1) {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
show_all_students();
break;
case 3:
search_student();
break;
case 4:
printf("程序已退出!\n");
exit(0);
default:
printf("无效的选择!\n");
}
}
return 0;
}
```
这个学生管理系统可以添加学生、查看所有学生、查找单个学生的记录。每个学生包含姓名、年龄、多门课程的成绩和平均分等信息。
阅读全文