学生信息管理系统
学生信息管理系统,主要为了实现学生信息的增加、删除、修改、查找等功能,以下是实现这一功能的相关代码。
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
struct node //创建一个结构体,学生的相关信息
{
string name; //学生姓名
int number; //学生学号
string sex; //学生性别
node* next;
};
node* creatList()//不带头结点创建链表
{
node* p, * head;
int c; //判断是否继续此操作
head = NULL;
while(1)
{
//创建结点
p = new node;
cout << "输入学生信息:姓名 学号 性别" << endl;
cin >> p->name >> p->number >> p->sex;
p->next = head;
head = p;
cout << "是否继续输入学生信息?[1/0]";
cin >> c;
if (tolower(c) == 0)
break;
}
return head;
}
void print_List(node* head) //打印全体学生的信息
{
node* p;
p = head;
while (p != NULL)
{
cout << p->name << " " << p->number << " " << p->sex << endl;
p = p->next;
}
}
node* Add_stu(node* head) //添加学生
{
node* p;
p = new node;
cout << "请输入增添学生的信息:姓名 学号 性别" << endl;
cin >> p->name >> p->number >> p->sex;
p->next = head;
head = p;
return head;
}
node* Delete_stu(node* head, int dnumber) //删除学生
{
node* p, * q, * t;
t = head;
if (head->number == dnumber)//如果删除的为第一个结点
{
p = head;
head = head->next;
delete p;
cout << "删除成功!" << endl;
return head;
}
p = head;
q = head->next;
while (q != NULL && q->number != dnumber)
{
q = q->next;
p = p->next;
}
if (p != NULL&&q!=NULL)
{
p->next = q->next;
delete q;
cout << "删除成功!" << endl;
return t;
}
cout << "未找到所需删除的学生信息!" << endl;
}
void Find_stu(node* head, int dnumber) //查找学生
{
node* p;
p = head;
while (p != NULL)
{
if (p->number == dnumber)
{
cout << "所要查找学生的相关信息:" << endl;
cout << p->name << " " << p->number << " " << p->sex << endl;
break;
}
else
p = p->next;
}
}
void Modify_stu(node* head, int dnumber) //修改学生的相关信息
{
node* p;
string aname; int anumber; string asex;
p = head;
while (p != NULL)
{
if (p->number == dnumber)
{
cout << "输入修改后的学生信息" << endl;
cin >> aname >> anumber >> asex;
p->name = aname;
p->number = anumber;
p->sex = asex;
cout << "所要修改学生的相关信息:" << endl;
cout << p->name << " " << p->number << " " << p->sex << endl;
break;
}
else
p = p->next;
}
}
void main()
{
int i;int f = 1;
node *head;
head = creatList();
print_List(head);
cout << endl;
cout << "0、增加新的学生!" << endl;
cout << "1、删除学生信息!" << endl;
cout << "2、查找学生信息!" << endl;
cout << "3、修改学生信息!" << endl;
cout << "4、打印学生信息!" << endl;
cout << "5、退出相关操作!" << endl;
while (f)
{
cout << "请输入要进行的操作序号:";
cin >> i;
switch (i)
{
case 0:head=Add_stu(head);
cout << "添加成功!" << endl;
break;
case 1:
{
cout << "请输入要删除学生的相关信息:姓名 学号 性别" << endl;
string dname; int dnumber; string dsex;
cin >> dname >> dnumber >> dsex;
head=Delete_stu(head, dnumber);
break;
}
case 2:
{
cout << "请输入要查找学生的学号:" << endl;
int dnumber;
cin >>dnumber;
Find_stu(head, dnumber);
break;
}
case 3:
{
cout << "请输入要修改学生的学号:" << endl;
int dnumber;
cin >> dnumber;
Find_stu(head, dnumber);
cout << "所要修改学生的相关信息:" << endl;
Modify_stu(head, dnumber);
cout << "修改成功!" << endl;
break;
}
case 4:
cout << "打印学生信息!" << endl;
print_List(head);
break;
case 5:
f = 0;
break;
default:
break;
}
}
cout << "退出!";
exit(0);
}