写一个c语言学生管理系统,支持增删改查和排序,支持将数据写入文件
时间: 2023-03-22 22:01:02 浏览: 96
以下是一个简单的C语言学生管理系统,支持增加、删除、修改、查询和排序学生信息,并可以将学生信息写入文件。
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
struct Student {
char name[50];
int age;
float score;
};
struct Student students[MAX_STUDENTS];
int num_students = 0;
void add_student() {
if (num_students >= MAX_STUDENTS) {
printf("学生人数已满,无法添加新学生。\n");
return;
}
struct Student student;
printf("请输入学生姓名:");
scanf("%s", student.name);
printf("请输入学生年龄:");
scanf("%d", &student.age);
printf("请输入学生成绩:");
scanf("%f", &student.score);
students[num_students++] = student;
printf("添加学生成功!\n");
}
void remove_student() {
if (num_students == 0) {
printf("当前没有学生信息。\n");
return;
}
char name[50];
printf("请输入要删除的学生姓名:");
scanf("%s", name);
int i;
for (i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
break;
}
}
if (i == num_students) {
printf("未找到名为%s的学生。\n", name);
} else {
for (int j = i; j < num_students - 1; j++) {
students[j] = students[j + 1];
}
num_students--;
printf("删除成功!\n");
}
}
void modify_student() {
if (num_students == 0) {
printf("当前没有学生信息。\n");
return;
}
char name[50];
printf("请输入要修改的学生姓名:");
scanf("%s", name);
int i;
for (i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
break;
}
}
if (i == num_students) {
printf("未找到名为%s的学生。\n", name);
} else {
struct Student student;
printf("请输入学生姓名:");
scanf("%s", student.name);
printf("请输入学生年龄:");
scanf("%d", &student.age);
printf("请输入学生成绩:");
scanf("%f", &student.score);
students[i] = student;
printf("修改成功!\n");
}
}
void query_student() {
if (num_students == 0) {
printf("当前没有学生信息。\n");
return;
}
char name[50];
printf("请输入要查询的学生姓名:");
scanf("%s", name);
int i;
for (i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("姓名:%s,年龄:%d,成绩:%.2f\n", students[i].name, students[i].age, students[i].score);
break;
}
}
if (i == num_students) {
printf("未找到名为%s的学生。\n
阅读全文