帮我用c++ 写一个学生管理系统
时间: 2023-05-16 20:05:26 浏览: 109
好的,我可以为您编写一个学生管理系统。首先,您需要定义学生的结构体,包括学生的姓名、学号、年龄、性别等信息。然后,您可以使用链表或数组来存储学生信息。接下来,您可以编写一些函数来实现添加、删除、修改和查询学生信息的功能。最后,您可以编写一个主函数来调用这些函数,以实现完整的学生管理系统。以下是示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_ID_LEN 10
typedef struct student {
char name[MAX_NAME_LEN];
char id[MAX_ID_LEN];
int age;
char gender;
struct student *next;
} Student;
Student *head = NULL;
void add_student() {
Student *new_student = (Student *)malloc(sizeof(Student));
printf("请输入学生姓名:");
scanf("%s", new_student->name);
printf("请输入学生学号:");
scanf("%s", new_student->id);
printf("请输入学生年龄:");
scanf("%d", &new_student->age);
printf("请输入学生性别(M/F):");
scanf(" %c", &new_student->gender);
new_student->next = head;
head = new_student;
printf("添加成功!\n");
}
void delete_student() {
char id[MAX_ID_LEN];
printf("请输入要删除的学生学号:");
scanf("%s", id);
Student *prev = NULL;
Student *curr = head;
while (curr != NULL) {
if (strcmp(curr->id, id) == 0) {
if (prev == NULL) {
head = curr->next;
} else {
prev->next = curr->next;
}
free(curr);
printf("删除成功!\n");
return;
}
prev = curr;
curr = curr->next;
}
printf("未找到该学生!\n");
}
void modify_student() {
char id[MAX_ID_LEN];
printf("请输入要修改的学生学号:");
scanf("%s", id);
Student *curr = head;
while (curr != NULL) {
if (strcmp(curr->id, id) == 0) {
printf("请输入学生姓名:");
scanf("%s", curr->name);
printf("请输入学生年龄:");
scanf("%d", &curr->age);
printf("请输入学生性别(M/F):");
scanf(" %c", &curr->gender);
printf("修改成功!\n");
return;
}
curr = curr->next;
}
printf("未找到该学生!\n");
}
void query_student() {
char id[MAX_ID_LEN];
printf("请输入要查询的学生学号:");
scanf("%s", id);
Student *curr = head;
while (curr != NULL) {
if (strcmp(curr->id, id) == 0) {
printf("姓名:%s\n", curr->name);
printf("学号:%s\n", curr->id);
printf("年龄:%d\n", curr->age);
printf("性别:%c\n", curr->gender);
return;
}
curr = curr->next;
}
printf("未找到该学生!\n");
}
void print_all_students() {
Student *curr = head;
while (curr != NULL) {
printf("姓名:%s\n", curr->name);
printf("学号:%s\n", curr->id);
printf("年龄:%d\n", curr->age);
printf("性别:%c\n", curr->gender);
printf("\n");
curr = curr->next;
}
}
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");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
delete_student();
break;
case 3:
modify_student();
break;
case 4:
query_student();
break;
case 5:
print_all_students();
break;
case 6:
exit(0);
default:
printf("无效的选择!\n");
}
}
return 0;
}
```
阅读全文