c语言 学生学籍管理系统
时间: 2023-12-21 11:30:45 浏览: 83
以下是一个简单的C语言学生学籍管理系统的实现,包括添加学生、删除学生、修改学生、查找学生、按学号排序、按年级分类显示等功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 100 // 最大学生数
#define MAX_NAME_LEN 20 // 姓名最大长度
#define MAX_ID_LEN 10 // 学号最大长度
#define MAX_GRADE_LEN 10 // 年级最大长度
// 学生结构体
typedef struct {
char name[MAX_NAME_LEN]; // 姓名
char id[MAX_ID_LEN]; // 学号
char grade[MAX_GRADE_LEN]; // 年级
} Student;
// 学生管理系统结构体
typedef struct {
Student *students[MAX_STUDENT_NUM]; // 学生指针数组
int count; // 学生人数
} StudentManager;
// 初始化学生管理系统
void init(StudentManager *manager) {
manager->count = 0;
}
// 添加学生
void addStudent(StudentManager *manager, Student *student) {
if (manager->count >= MAX_STUDENT_NUM) {
printf("学生人数已达到上限,无法添加新学生!\n");
return;
}
manager->students[manager->count] = student;
manager->count++;
}
// 删除学生
void deleteStudent(StudentManager *manager, char *id) {
int i, j;
for (i = 0; i < manager->count; i++) {
if (strcmp(manager->students[i]->id, id) == 0) {
free(manager->students[i]);
for (j = i; j < manager->count - 1; j++) {
manager->students[j] = manager->students[j + 1];
}
manager->count--;
printf("删除成功!\n");
return;
}
}
printf("学号为%s的学生不存在!\n", id);
}
// 修改学生
void modifyStudent(StudentManager *manager, char *id, Student *newStudent) {
int i;
for (i = 0; i < manager->count; i++) {
if (strcmp(manager->students[i]->id, id) == 0) {
strcpy(manager->students[i]->name, newStudent->name);
strcpy(manager->students[i]->grade, newStudent->grade);
printf("修改成功!\n");
return;
}
}
printf("学号为%s的学生不存在!\n", id);
}
// 查找学生
void findStudent(StudentManager *manager, char *id) {
int i;
for (i = 0; i < manager->count; i++) {
if (strcmp(manager->students[i]->id, id) == 0) {
printf("姓名:%s,学号:%s,年级:%s\n", manager->students[i]->name, manager->students[i]->id, manager->students[i]->grade);
return;
}
}
printf("学号为%s的学生不存在!\n", id);
}
// 按学号排序
void sortById(StudentManager *manager) {
int i, j;
Student *temp;
for (i = 0; i < manager->count - 1; i++) {
for (j = 0; j < manager->count - 1 - i; j++) {
if (strcmp(manager->students[j]->id, manager->students[j + 1]->id) > 0) {
temp = manager->students[j];
manager->students[j] = manager->students[j + 1];
manager->students[j + 1] = temp;
}
}
}
printf("按学号排序成功!\n");
}
// 按年级分类显示
void showByGrade(StudentManager *manager) {
int i, j;
char grades[MAX_STUDENT_NUM][MAX_GRADE_LEN];
int gradeCount[MAX_STUDENT_NUM] = {0};
int gradeNum = 0;
for (i = 0; i < manager->count; i++) {
for (j = 0; j < gradeNum; j++) {
if (strcmp(manager->students[i]->grade, grades[j]) == 0) {
gradeCount[j]++;
break;
}
}
if (j == gradeNum) {
strcpy(grades[j], manager->students[i]->grade);
gradeCount[j]++;
gradeNum++;
}
}
for (i = 0; i < gradeNum; i++) {
printf("%s年级有%d名学生:\n", grades[i], gradeCount[i]);
for (j = 0; j < manager->count; j++) {
if (strcmp(manager->students[j]->grade, grades[i]) == 0) {
printf("姓名:%s,学号:%s\n", manager->students[j]->name, manager->students[j]->id);
}
}
}
}
// 保存到文件
void saveToFile(StudentManager *manager, char *filename) {
FILE *fp = fopen(filename, "w");
if (fp == NULL) {
printf("文件打开失败!\n");
return;
}
int i;
for (i = 0; i < manager->count; i++) {
fprintf(fp, "%s %s %s\n", manager->students[i]->name, manager->students[i]->id, manager->students[i]->grade);
}
fclose(fp);
printf("保存成功!\n");
}
// 从文件读取
void readFromFile(StudentManager *manager, char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("文件打开失败!\n");
return;
}
char name[MAX_NAME_LEN];
char id[MAX_ID_LEN];
char grade[MAX_GRADE_LEN];
while (fscanf(fp, "%s %s %s", name, id, grade) != EOF) {
Student *student = (Student *)malloc(sizeof(Student));
strcpy(student->name, name);
strcpy(student->id, id);
strcpy(student->grade, grade);
addStudent(manager, student);
}
fclose(fp);
printf("读取成功!\n");
}
// 清空数据
void clear(StudentManager *manager) {
int i;
for (i = 0; i < manager->count; i++) {
free(manager->students[i]);
}
manager->count = 0;
printf("清空成功!\n");
}
// 显示菜单
void showMenu() {
printf("1.添加学生\n");
printf("2.删除学生\n");
printf("3.修改学生\n");
printf("4.查找学生\n");
printf("5.按学号排序\n");
printf("6.按年级分类显示\n");
printf("7.保存到文件\n");
printf("8.从文件读取\n");
printf("9.清空数据\n");
printf("0.退出程序\n");
}
int main() {
StudentManager manager;
init(&manager);
int choice;
do {
showMenu();
printf("请输入操作编号:");
scanf("%d", &choice);
switch (choice) {
case 1: {
Student *student = (Student *)malloc(sizeof(Student));
printf("请输入学生姓名:");
scanf("%s", student->name);
printf("请输入学生学号:");
scanf("%s", student->id);
printf("请输入学生年级:");
scanf("%s", student->grade);
addStudent(&manager, student);
printf("添加成功!\n");
break;
}
case 2: {
char id[MAX_ID_LEN];
printf("请输入要删除的学生学号:");
scanf("%s", id);
deleteStudent(&manager, id);
break;
}
case 3: {
char id[MAX_ID_LEN];
printf("请输入要修改的学生学号:");
scanf("%s", id);
Student *newStudent = (Student *)malloc(sizeof(Student));
printf("请输入新的学生姓名:");
scanf("%s", newStudent->name);
printf("请输入新的学生年级:");
scanf("%s", newStudent->grade);
modifyStudent(&manager, id, newStudent);
break;
}
case 4: {
char id[MAX_ID_LEN];
printf("请输入要查找的学生学号:");
scanf("%s", id);
findStudent(&manager, id);
break;
}
case 5: {
sortById(&manager);
break;
}
case 6: {
showByGrade(&manager);
break;
}
case 7: {
char filename[100];
printf("请输入文件名:");
scanf("%s", filename);
saveToFile(&manager, filename);
break;
}
case 8: {
char filename[100];
printf("请输入文件名:");
scanf("%s", filename);
readFromFile(&manager, filename);
break;
}
case 9: {
clear(&manager);
break;
}
case 0: {
printf("退出程序!\n");
break;
}
default: {
printf("输入有误,请重新输入!\n");
break;
}
}
} while (choice != 0);
return 0;
}
```
阅读全文