C++写一个学生信息管理系统
时间: 2024-04-29 16:26:30 浏览: 137
好的,我可以为您提供一份参考代码来实现学生信息管理系统,代码如下:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student {
char name[20];
int id;
float gpa;
};
struct node {
struct student data;
struct node* next;
};
struct node* head = NULL;
void printMenu() {
printf("==========学生信息管理系统==========\n");
printf("1. 添加学生信息\n");
printf("2. 删除学生信息\n");
printf("3. 修改学生信息\n");
printf("4. 查询学生信息\n");
printf("5. 显示所有学生信息\n");
printf("6. 退出系统\n");
printf("====================================\n");
}
void addStudent() {
struct student s;
printf("请依次输入学生姓名、学号和GPA:\n");
scanf("%s%d%f", s.name, &s.id, &s.gpa);
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = s;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
}
else {
struct node* p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = newNode;
}
printf("添加成功!\n");
}
void deleteStudent() {
int id;
printf("请输入要删除的学生学号:\n");
scanf("%d", &id);
struct node* p = head;
struct node* prev;
while (p != NULL && p->data.id != id) {
prev = p;
p = p->next;
}
if (p == NULL) {
printf("未找到学号为%d的学生,删除失败!\n", id);
}
else {
prev->next = p->next;
free(p);
printf("删除成功!\n");
}
}
void modifyStudent() {
int id;
printf("请输入要修改的学生学号:\n");
scanf("%d", &id);
struct node* p = head;
while (p != NULL && p->data.id != id) {
p = p->next;
}
if (p == NULL) {
printf("未找到学号为%d的学生,修改失败!\n", id);
}
else {
printf("该学生原信息为:%s %d %.2f\n", p->data.name, p->data.id, p->data.gpa);
printf("请输入修改后的学生姓名、学号和GPA:\n");
scanf("%s%d%f", p->data.name, &p->data.id, &p->data.gpa);
printf("修改成功!\n");
}
}
void queryStudent() {
int id;
printf("请输入要查询的学生学号:\n");
scanf("%d", &id);
struct node* p = head;
while (p != NULL && p->data.id != id) {
p = p->next;
}
if (p == NULL) {
printf("未找到学号为%d的学生!\n", id);
}
else {
printf("查询结果为:%s %d %.2f\n", p->data.name, p->data.id, p->data.gpa);
}
}
void displayStudent() {
struct node* p = head;
while (p != NULL) {
printf("%s %d %.2f\n", p->data.name, p->data.id, p->data.gpa);
p = p->next;
}
}
int main() {
int choice;
do {
printMenu();
printf("请输入操作编号:\n");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
modifyStudent();
break;
case 4:
queryStudent();
break;
case 5:
displayStudent();
break;
case 6:
printf("谢谢使用!\n");
exit(0);
default:
printf("输入有误,请重新输入!\n");
break;
}
} while (1);
return 0;
}
```
您可以将此代码保存为一个 C 文件,例如 `student_management_system.c`,然后编译并运行它。这个程序会提供一个菜单,让您可以添加、删除、修改、查询和显示所有学生的信息。
阅读全文