用C语言写一个有增删改查功能的学生管理系统
时间: 2023-06-14 07:07:53 浏览: 135
好的,以下是一个简单的学生管理系统,具有增删改查的基本功能。代码中使用了结构体和指针等基本C语言知识。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT 100 // 最大学生数量
// 学生结构体
typedef struct {
char name[20]; // 姓名
int age; // 年龄
int score; // 成绩
} Student;
// 学生数组
Student students[MAX_STUDENT];
int studentCount = 0; // 学生数量
// 查找学生
int findStudent(char* name) {
for (int i = 0; i < studentCount; i++) {
if (strcmp(students[i].name, name) == 0) {
return i;
}
}
return -1;
}
// 添加学生
void addStudent() {
printf("请输入学生姓名:");
scanf("%s", students[studentCount].name);
printf("请输入学生年龄:");
scanf("%d", &students[studentCount].age);
printf("请输入学生成绩:");
scanf("%d", &students[studentCount].score);
studentCount++;
printf("添加成功!\n");
}
// 删除学生
void deleteStudent() {
char name[20];
printf("请输入要删除的学生姓名:");
scanf("%s", name);
int index = findStudent(name);
if (index == -1) {
printf("该学生不存在!\n");
} else {
for (int i = index; i < studentCount - 1; i++) {
students[i] = students[i + 1];
}
studentCount--;
printf("删除成功!\n");
}
}
// 修改学生
void modifyStudent() {
char name[20];
printf("请输入要修改的学生姓名:");
scanf("%s", name);
int index = findStudent(name);
if (index == -1) {
printf("该学生不存在!\n");
} else {
printf("请输入学生年龄:");
scanf("%d", &students[index].age);
printf("请输入学生成绩:");
scanf("%d", &students[index].score);
printf("修改成功!\n");
}
}
// 查询学生
void queryStudent() {
char name[20];
printf("请输入要查询的学生姓名:");
scanf("%s", name);
int index = findStudent(name);
if (index == -1) {
printf("该学生不存在!\n");
} else {
printf("姓名:%s,年龄:%d,成绩:%d\n",
students[index].name, students[index].age, students[index].score);
}
}
// 显示所有学生
void showStudents() {
printf("学生信息如下:\n");
printf("姓名\t年龄\t成绩\n");
for (int i = 0; i < studentCount; i++) {
printf("%s\t%d\t%d\n",
students[i].name, students[i].age, students[i].score);
}
}
int main() {
int choice;
while (1) {
printf("请选择操作:\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:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
modifyStudent();
break;
case 4:
queryStudent();
break;
case 5:
showStudents();
break;
case 6:
printf("感谢使用!\n");
exit(0);
default:
printf("输入有误,请重新输入!\n");
break;
}
}
return 0;
}
```
这是一个简单的学生管理系统,你可以根据自己的需要进行修改和完善。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.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)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)