这个简单的学生管理系统可以让您添加学生、查找学生和显示所有学生。这个代码仅包含基本功能,您可以根据需求添加其他功能,如更新学生信息、删除学生信息等
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
typedef struct {
int id;
char name[50];
float score;
} Student;
void add_student(Student *students, int *num_students, int id, char *name, float score);
void display_students(Student *students, int num_students);
int find_student(Student *students, int num_students, int id);
void update_student(Student *students, int num_students, int id);
void delete_student(Student *students, int *num_students, int id);
int main() {
Student students[MAX_STUDENTS];
int num_students = 0;
int choice, id;
char name[50];
float score;
while (1) {
printf("\n学生管理系统菜单:\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:
printf("输入学生ID: ");
scanf("%d", &id);
printf("输入学生姓名: ");
scanf("%s", name);
printf("输入学生成绩: ");
scanf("%f", &score);
add_student(students, &num_students, id, name, score);
break;
case 2:
display_students(students, num_students);
break;
case 3:
printf("输入学生ID: ");
scanf("%d", &id);
int index = find_student(students, num_students, id);
if (index != -1) {
printf("学生ID: %d\n", students[index].id);
printf("学生姓名: %s\n", students[index].name);
printf("学生成绩: %.2f\n", students[index].score);
} else {
printf("未找到ID为 %d 的学生\n", id);
}
break;
case 4:
printf("输入学生ID: ");
scanf("%d", &id);
update_student(students, num_students, id);
break;
case 5:
printf("输入学生ID: ");
scanf("%d", &id);
delete_student(students, &num_students, id);
break;
case 6:
exit(0);
break;
default:
printf("无效选项\n");
break;
}
}
return 0;
}
void add_student(Student *students, int *num_students, int id, char *name, float score) {
students[*num_students].id = id;
strcpy(students[*num_students].name, name);
students[*num_students].score = score;
(*num_students)++;
}
void display_students(Student *students, int num_students) {
for (int i = 0; i < num_students; i++) {
printf("学生ID: %d\n", students[i].id);
printf("学生姓名: %s\n", students[i].name);
printf("学生成绩: %.2f\n", students[i].score);
printf("\n");
}
}
int find_student(Student *students, int num_students, int id) {
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
return i;
}
}
return -1;
}
void update_student(Student *students, int num_students, int id) {
int index = find_student(students, num_students, id);
if (index != -1) {
printf("输入新的学生姓名: ");
scanf("%s", students[index].name);
printf("输入新的学生成绩: ");
scanf("%f", &students[index].score);
} else {
printf("未找到ID为 %d 的学生\n", id);
}
}
void delete_student(Student *students, int *num_students, int id) {
int index = find_student(students, *num_students, id);
if (index != -1) {
for (int i = index; i < *num_students - 1; i++) {
students[i] = students[i + 1];
}
(*num_students)--;
printf("成功删除ID为 %d 的学生\n", id);
} else {
printf("未找到ID为 %d 的学生\n", id);
}
}