用c++编写一个简单的学生信息管理程序及菜单系统,学生包括学号、姓名、性别、年龄、专业、班级、成绩等成员,要求可以对学生信息实现显示、查找、删除、修改
时间: 2023-07-04 19:23:59 浏览: 86
学生信息管理系统, 实现学生信息包括:学号、姓名、性别、年龄、班级等信息
5星 · 资源好评率100%
好的,我可以为你提供一个简单的学生信息管理程序及菜单系统的代码,以下是代码:
```c++
#include<iostream>
#include<cstring>
using namespace std;
//学生结构体
struct Student{
string id; //学号
string name; //姓名
string gender; //性别
int age; //年龄
string major; //专业
string cls; //班级
float score; //成绩
};
//链表节点结构体
struct Node{
Student data; //学生信息
Node* next; //指向下一个节点的指针
};
//链表类
class StudentList{
private:
Node* head; //头节点指针
public:
//构造函数
StudentList(){
head = new Node;
head->next = NULL;
}
//析构函数
~StudentList(){
Node* p = head;
Node* q = p;
while(p != NULL){
q = p->next;
delete p;
p = q;
}
}
//添加学生信息
void addStudent(Student data){
Node* p = head;
while(p->next != NULL){
p = p->next;
}
Node* node = new Node;
node->data = data;
node->next = NULL;
p->next = node;
}
//删除学生信息
void deleteStudent(string id){
Node* p = head->next;
Node* q = head;
while(p != NULL){
if(p->data.id == id){
q->next = p->next;
delete p;
return;
}
q = p;
p = p->next;
}
}
//修改学生信息
void modifyStudent(string id, Student data){
Node* p = head->next;
while(p != NULL){
if(p->data.id == id){
p->data = data;
return;
}
p = p->next;
}
}
//查找学生信息
Student* findStudent(string id){
Node* p = head->next;
while(p != NULL){
if(p->data.id == id){
return &(p->data);
}
p = p->next;
}
return NULL;
}
//显示学生信息
void display(){
Node* p = head->next;
while(p != NULL){
cout << "学号:" << p->data.id << endl;
cout << "姓名:" << p->data.name << endl;
cout << "性别:" << p->data.gender << endl;
cout << "年龄:" << p->data.age << endl;
cout << "专业:" << p->data.major << endl;
cout << "班级:" << p->data.cls << endl;
cout << "成绩:" << p->data.score << endl << endl;
p = p->next;
}
}
};
//菜单类
class Menu{
public:
//显示菜单
static void show(){
cout << "**********学生信息管理系统**********" << endl;
cout << "1.添加学生信息" << endl;
cout << "2.删除学生信息" << endl;
cout << "3.修改学生信息" << endl;
cout << "4.查找学生信息" << endl;
cout << "5.显示学生信息" << endl;
cout << "0.退出系统" << endl;
cout << "************************************" << endl;
}
//获取菜单选项
static int getOption(){
int option;
cout << "请选择操作:";
cin >> option;
return option;
}
//获取学生信息
static Student getStudent(){
Student student;
cout << "请输入学生信息:" << endl;
cout << "学号:";
cin >> student.id;
cout << "姓名:";
cin >> student.name;
cout << "性别:";
cin >> student.gender;
cout << "年龄:";
cin >> student.age;
cout << "专业:";
cin >> student.major;
cout << "班级:";
cin >> student.cls;
cout << "成绩:";
cin >> student.score;
return student;
}
//获取学号
static string getId(){
string id;
cout << "请输入学号:";
cin >> id;
return id;
}
//显示学生信息
static void displayStudent(Student* student){
if(student == NULL){
cout << "未找到该学生!" << endl;
return;
}
cout << "学号:" << student->id << endl;
cout << "姓名:" << student->name << endl;
cout << "性别:" << student->gender << endl;
cout << "年龄:" << student->age << endl;
cout << "专业:" << student->major << endl;
cout << "班级:" << student->cls << endl;
cout << "成绩:" << student->score << endl << endl;
}
};
int main(){
StudentList list;
while(true){
Menu::show();
int option = Menu::getOption();
switch(option){
//退出系统
case 0:
return 0;
//添加学生信息
case 1:
list.addStudent(Menu::getStudent());
cout << "添加成功!" << endl;
break;
//删除学生信息
case 2:
list.deleteStudent(Menu::getId());
cout << "删除成功!" << endl;
break;
//修改学生信息
case 3:
{
string id = Menu::getId();
Student* student = list.findStudent(id);
if(student == NULL){
cout << "未找到该学生!" << endl;
break;
}
Student data = Menu::getStudent();
list.modifyStudent(id, data);
cout << "修改成功!" << endl;
}
break;
//查找学生信息
case 4:
{
Student* student = list.findStudent(Menu::getId());
Menu::displayStudent(student);
}
break;
//显示学生信息
case 5:
list.display();
break;
//非法操作
default:
cout << "非法操作!" << endl;
break;
}
}
return 0;
}
```
这个程序使用了链表来存储学生信息,并提供了菜单系统来操作学生信息。你可以按照菜单提示进行相应的操作。
阅读全文