用dev C写一个学生管理系统
时间: 2023-12-14 08:04:42 浏览: 33
好的,学生管理系统可以包括以下功能:
1. 添加学生信息
2. 修改学生信息
3. 删除学生信息
4. 查询学生信息
5. 显示全部学生信息
下面是一个简单的学生管理系统的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 50 // 定义字符串最大长度
struct student {
char name[MAX_LEN]; // 姓名
int age; // 年龄
char gender[MAX_LEN]; // 性别
char id[MAX_LEN]; // 学号
};
int main() {
int n; // 学生数量
struct student *students; // 学生数组
printf("请输入学生数量:");
scanf("%d", &n);
students = (struct student *)malloc(n * sizeof(struct student)); // 动态分配数组内存
// 添加学生信息
for (int i = 0; i < n; i++) {
printf("请输入第%d个学生的姓名、年龄、性别、学号:", i+1);
scanf("%s %d %s %s", students[i].name, &students[i].age, students[i].gender, students[i].id);
}
// 显示全部学生信息
printf("\n全部学生信息如下:\n");
printf("姓名\t年龄\t性别\t学号\n");
for (int i = 0; i < n; i++) {
printf("%s\t%d\t%s\t%s\n", students[i].name, students[i].age, students[i].gender, students[i].id);
}
// 查询学生信息
char search_id[MAX_LEN];
printf("\n请输入要查询的学生学号:");
scanf("%s", search_id);
for (int i = 0; i < n; i++) {
if (strcmp(students[i].id, search_id) == 0) {
printf("查询结果如下:\n");
printf("姓名\t年龄\t性别\t学号\n");
printf("%s\t%d\t%s\t%s\n", students[i].name, students[i].age, students[i].gender, students[i].id);
break;
}
}
// 修改学生信息
char modify_id[MAX_LEN];
printf("\n请输入要修改的学生学号:");
scanf("%s", modify_id);
for (int i = 0; i < n; i++) {
if (strcmp(students[i].id, modify_id) == 0) {
printf("请输入新的姓名、年龄、性别、学号:");
scanf("%s %d %s %s", students[i].name, &students[i].age, students[i].gender, students[i].id);
break;
}
}
// 删除学生信息
char delete_id[MAX_LEN];
printf("\n请输入要删除的学生学号:");
scanf("%s", delete_id);
for (int i = 0; i < n; i++) {
if (strcmp(students[i].id, delete_id) == 0) {
for (int j = i; j < n - 1; j++) {
students[j] = students[j + 1];
}
n--;
students = (struct student *)realloc(students, n * sizeof(struct student)); // 释放内存
printf("删除成功!\n");
break;
}
}
// 显示全部学生信息
printf("\n全部学生信息如下:\n");
printf("姓名\t年龄\t性别\t学号\n");
for (int i = 0; i < n; i++) {
printf("%s\t%d\t%s\t%s\n", students[i].name, students[i].age, students[i].gender, students[i].id);
}
free(students); // 释放内存
return 0;
}
```
以上代码可以在Dev-C++中编译运行。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)