用C语言写一个学员管理系统
时间: 2023-06-20 15:03:37 浏览: 63
以下是一个简单的学员管理系统的 C 语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生结构体
struct Student {
int id;
char name[20];
int age;
};
// 声明全局变量
struct Student *students = NULL;
int size = 0;
// 声明函数
void addStudent();
void deleteStudent();
void modifyStudent();
void viewAllStudents();
void viewOneStudent();
int main() {
int choice = 0;
while (1) {
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:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
modifyStudent();
break;
case 4:
viewAllStudents();
break;
case 5:
viewOneStudent();
break;
case 6:
printf("谢谢使用!\n");
exit(0);
default:
printf("无效的选择!\n");
break;
}
}
return 0;
}
void addStudent() {
int id = 0, age = 0;
char name[20];
printf("请输入学生ID: ");
scanf("%d", &id);
// 检查学生ID是否已存在
for (int i = 0; i < size; i++) {
if (students[i].id == id) {
printf("该学生ID已存在!\n");
return;
}
}
printf("请输入学生姓名: ");
scanf("%s", name);
printf("请输入学生年龄: ");
scanf("%d", &age);
// 创建新学生结构体
struct Student newStudent;
newStudent.id = id;
strcpy(newStudent.name, name);
newStudent.age = age;
// 添加新学生到数组中
students = (struct Student *)realloc(students, sizeof(struct Student) * (size + 1));
students[size] = newStudent;
size++;
printf("添加成功!\n");
}
void deleteStudent() {
int id = 0;
printf("请输入要删除的学生ID: ");
scanf("%d", &id);
// 查找学生ID所在的位置
int position = -1;
for (int i = 0; i < size; i++) {
if (students[i].id == id) {
position = i;
break;
}
}
// 如果学生ID不存在
if (position == -1) {
printf("该学生ID不存在!\n");
return;
}
// 将学生ID后面的所有学生向前移动一个位置
for (int i = position; i < size - 1; i++) {
students[i] = students[i + 1];
}
// 重新分配内存
students = (struct Student *)realloc(students, sizeof(struct Student) * (size - 1));
size--;
printf("删除成功!\n");
}
void modifyStudent() {
int id = 0;
printf("请输入要修改的学生ID: ");
scanf("%d", &id);
// 查找学生ID所在的位置
int position = -1;
for (int i = 0; i < size; i++) {
if (students[i].id == id) {
position = i;
break;
}
}
// 如果学生ID不存在
if (position == -1) {
printf("该学生ID不存在!\n");
return;
}
// 修改学生信息
printf("请输入学生姓名: ");
scanf("%s", students[position].name);
printf("请输入学生年龄: ");
scanf("%d", &students[position].age);
printf("修改成功!\n");
}
void viewAllStudents() {
// 如果没有学生
if (size == 0) {
printf("没有学生!\n");
return;
}
printf("ID\t姓名\t年龄\n");
printf("========================================\n");
// 遍历所有学生
for (int i = 0; i < size; i++) {
printf("%d\t%s\t%d\n", students[i].id, students[i].name, students[i].age);
}
}
void viewOneStudent() {
int id = 0;
printf("请输入要查看的学生ID: ");
scanf("%d", &id);
// 查找学生ID所在的位置
int position = -1;
for (int i = 0; i < size; i++) {
if (students[i].id == id) {
position = i;
break;
}
}
// 如果学生ID不存在
if (position == -1) {
printf("该学生ID不存在!\n");
return;
}
printf("ID\t姓名\t年龄\n");
printf("========================================\n");
// 显示该学生信息
printf("%d\t%s\t%d\n", students[position].id, students[position].name, students[position].age);
}
```
该学员管理系统使用了动态内存分配来保存学生信息,可以添加、删除、修改、查看所有和查看单个学生信息。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)