c++数据结构学生管理系统.docx
《c++数据结构学生管理系统.docx》由会员分享,可在线阅读,更多相关《c++数据结构学生管理系统.docx(28页珍藏版)》请在冰豆网上搜索。
![c++数据结构学生管理系统.docx](https://file1.bdocx.com/fileroot1/2022-11/23/749af392-71f2-4ca1-9b00-833918c4f6a3/749af392-71f2-4ca1-9b00-833918c4f6a31.gif)
c++数据结构学生管理系统
C++数据结构学生管理系统
单链表实现
用模板类实现,实现操作符的重载
非常详细的代码及其操作
1、
private:
stringname;//姓名
stringID;//学号
stringsex;//性别
stringmajor;//专业
stringbrithday;//生日
intave;//均分
Student.h
2、
Student();
voidSetName(string&strname);
voidSetSex(string&strsex);
voidSetBrith(string&strbrith);
voidSetID(string&strID);
voidSetMajor(string&strmajor);
voidEditPerson();
booloperator!
=(Student&stu);
booloperator>(Student&stu);
friendostream&operator<<(ostream&ost,Student&stu);
friendifstream&operator>>(ifstream&ost,Student&stu);
friendofstream&operator<<(ofstream&ost,Student&stu);
stringGetName();
2、核心代码
ostream&operator<<(ostream&ost,Student&stu)
{
ost<<"Name"<ost<<"Sex"<ost<<"Brithday"<ost<<"ID"<ost<<"Major"<returnost;
}
3、测试代码
Case’a’:
case'b':
system("cls");
list.PrintLinkList();
cout<<"请输入选择(帮助选项-->h):
"<break;
cout<<"当前链表的长度为:
"<length=list.ListLength();
case'c':
case'd':
system("cls");
cout<<"现有学生的名字:
"<list.DisplayName();
cout<cout<<"请输入学生的姓名"<cin>>name;
stu.SetName(name);
delstu=list.Find(stu);
cout<cout<<"请输入选择(帮助选项-->h):
"<break;
case'd':
cout<<"现有学生的名字:
"<list.DisplayName();
cout<cout<<"请输入学生的姓名"<cin>>name;
stu.SetName(name);
delstu=list.Find(stu);
cout<
3、源代码
case'f':
case'g':
list.Sort();
list.PrintLinkList();
LinkList.h
#ifndefLinkList_H
#defineLinkList_H
#include
#include
usingnamespacestd;
template
structNode
{
Tdata;//元素自身的信息,数据域
Node*next;//后继元素存储地址,地址域
};
template
classLinkList
{
Node*head;//单链表的头指针
public:
LinkList();
LinkList(Ta[],intn);
voidSetLinList(Ta[],intn);
~LinkList();
intListLength();//求链表的长度
TGet(intpos);//按位查找,取单链表中第POS个节点的元素值
TFind(Titem);//查找函数,以T类型的数据作为参数
intLocate(Titem);//按值查找,求单链表中值为item的元素的序号
voidPrintLinkList();
voidInsert(inti,Titem);//在i的位置插入元素item
TDelete(inti);//在单链表中删除第i个节点,并返回这个值
voidInvert();//逆置函数
friendvoidMerge(LinkList&L1,LinkList&L2);//归并链表
voidDisplayNode(inti);
voidSave(charfname[]);//保存
voidOpen(charfname[]);
voidDisplayName();//显示名字
voidSort();//排序
};
#endif
LinkList.cpp
#include"LinkList.h"
template
//用于创建一个带有头结点的空链表
LinkList:
:
LinkList()
{
head=newNode;//私有
head->next=NULL;
}
template
/*
用于创建一个带有头结点的空链表
*/
LinkList:
:
LinkList(Ta[],intn)//尾插法
{
Node*rear;
rear=head;//指向当前单链表的最后一个节点
for(inti=0;i{
Node*s;
s=newNode;
s->data=a[i];
rear->next=s;//rear=head
rear=s;//rear一直是指向单链表的最后一项
}
rear->next=NULL;//单链表创建结束,最后一个节点的指针置为空
}
template
voidLinkList:
:
SetLinList(Ta[],intn)
{
head=newNode;
Node*rear=head;
for(inti=0;i{
Node*s;
s=newNode;
s->data=a[i];
rear->next=s;
rear=s;
}
rear->next=NULL;//单链表创建结束,最后一个节点的指针置为空
}
template
intLinkList:
:
ListLength()
{
intnum=0;
Node*p;
p=head->next;
while(p)
{
p=p->next;
num++;
}
returnnum;
}
template
TLinkList:
:
Get(intpos)
{
Node*p;
intj=1;
p=head->next;//p指向头结点的下一个节点
while(p&&j{
p=p->next;
j++;
}
if(!
p||j>pos)
{
cout<<"查找位置非法";
exit
(1);
}
else
returnp->data;
}
template
TLinkList:
:
Find(Titem)
{
Node*p;
p=head->next;
while(p&&p->data!
=item)
{
p=p->next;
}
if(!
p)
{
cout<<"查找位置非法";
exit
(1);
}
else
returnp->data;
}
template
intLinkList:
:
Locate(Titem)
{
Node*p;
p=head->next;
intj=1;
while(p&&p->data!
=item)
{
p=p->next;
j++;
}
if(p)
returnj;
else
return0;
}
template
voidLinkList:
:
PrintLinkList()
{
Node*p;
p=head->next;
while(p)
{
cout<data<<"";
p=p->next;
}
}
template
voidLinkList:
:
Insert(inti,Titem)
{
Node*p;
p=head;
intj=0;
while(p&&j{
p=p->next;
j++;
}
if(!
p)
{
cerr<<"插入位置非法";
exit
(1);
}
else
{
Node*s=newNode;
s->data=item;
s->next=p->next;
p->next=s;
}
}
template
TLinkList:
:
Delete(inti)
{
Node*p,*q;
Tx;
p=head;
intj=0;
while(p&&j{
p=p->next;
j++;
}
if(!
p||!
p->next)
{
cerr<<"位置非法";
exit
(1);
}
else
{
q=p->next;
x=q->data;
p->next=q->next;
deleteq;
returnx;
}
}
/*利用头插法*/
template
voidLinkList:
:
Invert()
{
Node*p;
p=head->next;
head->next=NULL;//将逆置后的单链表初始化为空表
while(p!
=NULL)
{
Node*q=p;
p=p->next;
q->next=head->next;
head->next=q;
}
}
template
LinkList:
:
~LinkList()
{
Node*q=newNode;
while(head!
=NULL)
{
q=head;
head=head->next;
deleteq;
}
}
template
voidMerge(LinkList&L1,LinkList&L2)//归并链表
{
Node*p1,*p2,*p3;
p1=L1.head->next;//指向第一个数据的节点
p2=L2.head->next;
p3=L1.head;//指向L1的头结点
while((p1!
=NULL)&&(p2!
=NULL))
{
if((p1->data)<(p2->data))
{
p3->next=p1;
p1=p1->next;
p3=p3->next;
}
else
{
p3->next=p2;
p2=p2->next;
p3=p3->next;
}
}
if(p1!
=NULL)//p2为空的时候
p3->next=p1;
if(p2!
=NULL)
p3->next=p2;
deleteL2.head;
L2.head=NULL;
}
template
voidLinkList:
:
Save(charfname[])
{
ofstreamfout(fname);
fout<Node*p;
p=head->next;
while(p!
=NULL)
{
fout<data<p=p->next;
}
fout.close();
}
template
voidLinkList:
:
Open(charfname[])
{
ifstreamfin(fname);
intn;
fin>>n;
Titem;
for(inti=0;i{
//Node*p=newNode;
//fin>>p->data;
fin>>item;
Insert(i+1,item);
}
fin.close();
}
template
voidLinkList:
:
DisplayName()
{
Node*p;
p=head->next;
while(p)
{
cout<data.GetName()<p=p->next;
}
}
template
voidLinkList:
:
Sort()
{
Node*p;
intlen=ListLength();
for(inti=1;i{
p=head->next;
for(intj=0;j{
if(p->data>p->next->data)
{
Ttemp=p->data;
p->data=p->next->data;
p->next->data=temp;
}
p=p->next;
}
}
}
Student.h
#include"LinkList.h"
#include
#include
usingnamespacestd;
classStudent;
ostream&operator<<(ostream&os,Student&stu);
ifstream&operator>>(ifstream&ost,Student&stu);
ofstream&operator<<(ofstream&ost,Student&stu);
classStudent
{
private:
stringname;//姓名
stringID;//学号
stringsex;//性别
stringmajor;//专业
stringbrithday;//生日
intave;//均分
public:
Student();
voidSetName(string&strname);
voidSetSex(string&strsex);
voidSetBrith(string&strbrith);
voidSetID(string&strID);
voidSetMajor(string&strmajor);
voidSetAve(intstrave);
voidEditPerson();
booloperator!
=(Student&stu);
booloperator>(Student&stu);
friendostream&operator<<(ostream&ost,Student&stu);
friendifstream&operator>>(ifstream&ost,Student&stu);
friendofstream&operator<<(ofstream&ost,Student&stu);
stringGetName();
};
Student.cpp
#include"Student.h"
#include
usingnamespacestd;
ostream&operator<<(ostream&ost,Student&stu)
{
ost<<"Name"<ost<<"Sex"<ost<<"Brithday"<ost<<"ID"<ost<<"Major"<ost<<"Average"<returnost;
}
ifstream&operator>>(ifstream&ost,Student&stu)
{
ost>>stu.name;
ost>>stu.sex;
ost>>stu.brithday;
ost>>stu.ID;
ost>>stu.major;
ost>>stu.ave;
returnost;
}
ofstream&operator<<(ofstream&ost,Student&stu)
{
ost<ost<ost<ost<ost<ost<returnost;
}
Student:
:
Student()
{
}
voidStudent:
:
SetName(string&strname)
{
name=strname;
}
voidStudent:
:
SetSex(string&strsex)
{
sex=strsex;
}
voidStudent:
:
SetBrith(string&strbrith)
{
brithday=strbrith;
}
voidStudent:
:
SetID(string&strID)
{
ID=strID;
}
voidStudent:
:
SetMajor(string&strmajor)
{
major=strmajor;
}
voidStudent:
:
SetAve(intstrave)
{
ave=strave;
}
boolStudent:
:
operator!
=(Student&stu)
{
if(stu.name==name)
returnfalse;
returntrue;
}
boolStudent:
:
operator>(Student&stu1)
{
if(stu1.ave>ave)
returntrue;
returnfalse;
}
stringStudent:
:
GetName()
{
returnname;
}
voidStudent:
:
EditPerson()
{
}
Test.cpp
#include"LinkList.cpp"
#include"Student.h"
#include
#include
#include
usingnamespacestd;
voidmenu()
{
cout<<"a、增加一个学生信息"<cout<<"b、打印链表"<cout<<"c、获取当前链表的长度"<cout<<"d、查询学生的信息"<cout<<"e、删除学生信息"<cout<<"f、修改学生的信息"<cout<<"g、按平均成绩进行排序"<cout<<"h、帮助"<cout<<"o、退出"<}
voidmain()
{
charch;
LinkListlist;
list.Open("Student.txt");
stringname,sex,ID,brithday,major,newname;
intlength,pos,ave;
Studentstu,delstu;
menu();
cout<<"input"<do{
ch=getchar();
switch(ch)
{
case'h':
system("cls");
menu();
break;
case'a':
system("cls");
//stu.EditPerson();
cout<<"请输入学生的姓名"<cin>>name;
stu.SetName(name);
cout<<"请输入学生的性别"<cin>>sex;
stu.SetSex(sex);
cout<<"请输入学生的生日"<cin>>brithday;
stu.SetBrith(brithday);
cout<<"请输入学生的学号"<cin>>ID;
stu.SetID(ID);
cout<<"请输入学生的专业"<cin>>major;
stu.SetMajor(major);