c++数据结构学生管理系统_第1页
c++数据结构学生管理系统_第2页
c++数据结构学生管理系统_第3页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、C+数据结构学生管理系统单链表实现用模板类实现,实现操作符的重载非常详细的代码及其操作1、Stude nt.h2、private:»string name; 姓名 string ID;/ 学号 string sex;/ 性别 string major;/ 专业 string brithday;/ 生日 int ave;/ 均分Stude nt();void SetName(stri ng& strn ame);void SetSex(stri ng& strsex);void SetBrith(stri ng& strbrith);void SetID(str

2、i ng& strID);void SetMajor(stri ng& strmajor);void EditPers on();bool operator !=(Stude nt& stu);bool operator>(Stude nt& stu);friend ostream& operator<<(ostream& ost,Stude nt& stu); friend ifstrea m& operator»(ifstream& ost,Stude nt& stu);friend

3、 ofstream& operator<<(ofstream& ost,Stude nt& stu);string GetName();2、核心代码ostream& operator<<(ostrea m& ost,Stude nt& stu) ost<<"Name "<<stu .n ame<<e ndl; ost<<"Sex"<<stu.sex<<e ndl;ost<<"Brithday

4、 "<<stu.brithday<<e ndl; ost<<"ID"<<stu.ID<<e ndl;ost<<"Major"<<stu.major<<e ndl;return ost;ifstream& operator»(ifstream& ost,Stude nt& stu)ost»stu .n ame;ost»stu.sex; ost»stu.brithday; ost»

5、stuD;ost»stu.major;return ost;Lfstrea m& operator <<(ofstrea m& ost,Stude nt& stu) ost<<stu .n ame<<e ndl; ost<<stu.sex<<e ndl; ost<<stu.brithday<<e ndl; ost<<st u.I D<<e ndl; ost<<stu.major<<e ndl; return ost;bool St

6、ude nt:operator !=(Stude nt& stu)if(stu .n ame=n ame)return false;return true;bool Student:operator >(Student& stu1) if(stu1.ave>ave)return true;return false;3、测试代码序LEIfl信的息息行 生心羣 学槌的富绩 生生生宓 二锂豈于均 -加印曙專平utp F-sr 、力 r>>n alh c d e fsrEoia ;es a csystem("cls");/stu.EditPe

7、rso n();cout<<"请输入学生的姓名"<<e ndl; cin»n ame;stu.SetName( name);cout<<"请输入学生的性别"<<e ndl;NaneSexBrltlida*;lajoi* veragecase'b':cin>>sex;stu.SetSex(sex);cout<<"请输入学生的生日"<<e ndl; cin> >brithday;stu.SetBrith(brithda

8、y);cout<<"请输入学生的学号"<<endl; cin> >ID;stu.SetlD(ID);cout<<"请输入学生的专业"<<endl; cin»m ajor;stu.SetMajor(major);cout<<"请输入平均成绩"<<endl;cin> >ave;stu.SetAve(ave);list.I nsert(1,stu);list.Save("Stude nt.txt");cout<

9、<"请输入选择(帮助选项 -> h ) : "<<endl; break;小杨男1996-10-104物理85 system("cls");list.Pri ntL in kList();cout<<"请输入选择(帮助选项 -> h ) : "<<endl; break;Sex男Brithday 1?6'10'10ID4物理ft verLe 85Name”耳弓长Sex男Brithday 195-10-10ID3Major 英语 A uerage S2NanKSexB

10、rithdaM 1?5'10'1RIDZMaj op语文Auerag-e 78Name 小王Sex女Rpithday 1994-10-10ID1血数学p uepagc ?0case'c':cout<<"当前链表的长度为:"<<e ndl;len gth=list.ListLe ngth();case'd':system("cls");cout<<"现有学生的名字:"<<e ndl;list.DisplayName();cout<<

11、;e ndl;cout<<"请输入学生的姓名"<<e ndl; cin»n ame;stu.SetName( name);delstu=list.Fin d(stu);case'd':cout<<"现有学生的名字:"<<e ndl; list.DisplayName();cout<<e ndl;cout<<"请输入学生的姓名"<<e ndl; cin»n ame;stu.SetName( name);delstu=l

12、ist.Fi nd(stu);3、源代码OS学生的名字:入删除学生的姓名打淅|SexBritMav IBMajor Average NameSexBa* ithdID Major rverASe Nar«S«xBrit LdayTD小张力1795-10=10玉语S2小李女1995-10-18备文78f王1994-10-101数学case'f:system("cls");cout<<"现有学生的名字:"<<e ndl;list.DisplayName();cout<<"请输入修改的学

13、生的姓名:"<<e ndl;cin»n ame;tu.SetName( name);delstu=list.Fi nd(stu);pos=list.Locate(stu);list.Delete(pos);cout<<"请输入学生的新的信息:"<<e ndl;cout<<e ndl;cout<<"请输入学生的姓名"<<e ndl;cin»n ame;stu.SetName( name);cout<<"请输入学生的性别"&l

14、t;<e ndl;cin> >sex;stu.SetSex(sex);cout<<"请输入学生的生日"<<e ndl; cin>>brithday;戶输入学生的新的信息: 暑婁入学生的姓名 賞入学生的性别 嘗输入学生的生日1?6-1010请输入学生的学号 複入学生的专业请输入平均成绩gase'g':list.Sort();list .PrintLin kList();NameSexIDMajop fiuet'age NameSex Bv-ithday ID Cajor u e*aeMa.neEex

15、 Bi*ithd.ay IDajo沪 veF-agfeLin kList.h漲95-语丿 9 4毛 21 3 Sy- 8#ifndef Lin kList_H#define Li nkList_H#in elude <win dows.h>#in clude<fstream>using n amespace std; template<class T> struct NodeT data;Node<T> *n ext;template<class T>class Lin kListNode<T> *head;public:

16、Li nkList();Li nkList(T a,i nt n);void SetLinList(T a,int n);Li nkList();int ListLe ngth();T Get(i nt pos);元素值T Fi nd(T item);int Locate(T item);素的序号void PrintLin kList(); void Insert(int i,T item); T Delete(int i);/元素自身的信息,数据域/后继元素存储地址,地址域/单链表的头指针II求链表的长度/按位查找,取单链表中第POS个节点的II查找函数,以T类型的数据作为参数II按值查找,

17、求单链表中值为 item的元II在i的位置插入元素 itemII在单链表中删除第i个节点,并返回这个值void Invert(); / 逆置函数friend void Merge(LinkList<T> &L1,LinkList<T> &L2);/归并链表void DisplayNode(int i);void Save(char fname);/保存void Open(char fname);void DisplayName();/显示名字void Sort();/排序;#endifLinkList.cpp#include"LinkList.

18、h" template<class T>/ 用于创建一个带有头结点的空链表LinkList<T>:LinkList()head=new Node<T>/ 私有 head->next=NULL;template<class T>/* 用于创建一个带有头结点的空链表*/LinkList<T>:LinkList(T a,int n)/尾插法Node<T> *rear;rear=head;/ 指向当前单链表的最后一个节点for(int i=0;i<n;i+)Node<T> *s;s=new Nod

19、e<T>s->data=ai;rear->next=s;/rear=head rear=s;/rear 一直是指向单链表的最后一项rear->next=NULL;/ 单链表创建结束,最后一个节点的指针置为空template<class T>void LinkList<T>:SetLinList(T a,int n)head=new Node<T>Node<T> *rear=head;for(int i=0;i<n;i+)Node<T> *s; s=new Node<T> s->da

20、ta=ai; rear->next=s;rear=s; rear->next=NULL;/ 单链表创建结束,最后一个节点的指针置为空 template<class T>int LinkList<T>:ListLength()int num=0;Node<T> *p; p=head->next;while(p) p=p->next; num+; return num; template<class T> T LinkList<T>:Get(int pos)Node<T> *p;int j=1;p=he

21、ad->next;/p 指向头结点的下一个节点 while(p&&j<pos) p=p->next; j+; if(!p|j>pos)cout<<" 查找位置非法 " exit(1);else return p->data;template<class T>T LinkList<T>:Find(T item)Node<T> *p; p=head->next; while(p&&p->data!=item) p=p->next;if(!p)cout&

22、lt;<" 查找位置非法 "exit(1);elsereturn p->data;template<class T>int LinkList<T>:Locate(T item)Node<T> *p; p=head->next; int j=1; while(p&&p->data!=item) p=p->next;j+;if(p)return j;elsereturn 0;template<class T>void LinkList<T>:PrintLinkList()N

23、ode<T> *p; p=head->next; while(p) cout<<p->data<<" " p=p->next;template<class T>void LinkList<T>:Insert(int i,T item)Node<T> *p;p=head;int j=0;while(p&&j<i-1)p=p->next;j+;if(!p)cerr<<" 插入位置非法 "exit(1);elseNode<T&

24、gt; *s=new Node<T> s->data=item; s->next=p->next; p->next=s;template<class T>T LinkList<T>:Delete(int i)Node<T> *p,*q;T x;p=head;int j=0;while(p&&j<i-1)p=p->next;j+;if(!p|!p->next)cerr<<" 位置非法 "exit(1);elseq=p->next; x=q->dat

25、a; p->next=q->next; delete q; return x;/* 利用头插法 */ template<class T> void LinkList<T>:Invert()Node<T> *p; p=head->next;head->next=NULL;/ 将逆置后的单链表初始化为空表 while(p!=NULL)Node<T> *q=p; p=p->next; q->next=head->next; head->next=q; template<class T> Lin

26、kList<T>:LinkList()Node<T> *q=new Node<T> while(head!=NULL) q=head; head=head->next; delete q; template<class T> void Merge(LinkList<T> &L1,LinkList<T> &L2)/归并链表Node<T> *p1,*p2,*p3;p1=L1.head->next;/ 指向第一个数据的节点 p2=L2.head->next;p3=L1.head;/

27、指向 L1 的头结点while(p1!=NULL)&&(p2!=NULL) if(p1->data)<(p2->data)p3->next=p1;p1=p1->next;p3=p3->next;elsep3->next=p2;p2=p2->next;p3=p3->next;if(p1!=NULL)/p2 为空的时候p3->next=p1;if(p2!=NULL)p3->next=p2;delete L2.head;L2.head=NULL;template<class T>void LinkList&

28、lt;T>:Save(char fname)ofstream fout(fname); fout<<ListLength()<<endl; Node<T> *p;p=head->next;while(p!=NULL) fout<<p->data<<endl; p=p->next;fout.close();template<class T> void LinkList<T>:Open(char fname) ifstream fin(fname);int n;fin>>n;T

29、item;for(int i=0;i<n;i+)/Node<T> *p=new Node<T>/fin>>p->data;fin>>item; Insert(i+1,item); fin.close(); template<class T> void LinkList<T>:DisplayName()Node<T> *p; p=head->next; while(p) cout<<p->data.GetName()<<endl; p=p->next; tem

30、plate<class T> void LinkList<T>:Sort()Node<T> *p;int len=ListLength(); for(int i=1;i<len;i+) p=head->next; for(int j=0;j<len-i;j+) if(p->data>p->next->data) T temp=p->data; p->data=p->next->data; p->next->data=temp;p=p->next;Student.h #incl

31、ude"LinkList.h" #include<string> #include<fstream> using namespace std;class Student;ostream& operator<<(ostream& os,Student &stu); ifstream& operator>>(ifstream& ost,Student& stu);ofstream& operator <<(ofstream& ost,Student&

32、; stu); class Student private:string name;/string ID;/学号string sex;/性别string major;/专业string brithday;/生日int ave;/ 均分 public:Student();void SetName(string& strname); void SetSex(string& strsex); void SetBrith(string& strbrith); void SetID(string& strID);void SetMajor(string& strm

33、ajor); void SetAve(int strave);void EditPerson();bool operator !=(Student& stu);bool operator>(Student& stu);friend ostream& operator<<(ostream& ost,Student& stu); friend ifstream& operator>>(ifstream& ost,Student& stu); friend ofstream& operator<

34、;<(ofstream& ost,Student& stu); string GetName();Student.cpp#include"Student.h" #include<iostream> using namespace std;ostream& operator<<(ostream& ost,Student& stu) ost<<"Name "<<<<endl; ost<<"Sex "<

35、<stu.sex<<endl; ost<<"Brithday "<<stu.brithday<<endl; ost<<"ID "<<stu.ID<<endl; ost<<"Major "<<stu.major<<endl; ost<<"Average "<<stu.ave<<endl; return ost;ifstream& operator&g

36、t;>(ifstream& ost,Student& stu)ost>>;ost>>stu.sex;ost>>stu.brithday;ost>>stu.ID;ost>>stu.major;ost>>stu.ave;return ost;ofstream& operator <<(ofstream& ost,Student& stu) ost<<<<endl; ost<<stu.sex<<

37、;endl; ost<<stu.brithday<<endl; ost<<stu.ID<<endl; ost<<stu.major<<endl; ost<<stu.ave<<endl;return ost;Student:Student()void Student:SetName(string& strname)name=strname;void Student:SetSex(string& strsex)sex=strsex;void Student:SetBrith(string

38、& strbrith)brithday=strbrith;void Student:SetID(string& strID)ID=strID;void Student:SetMajor(string& strmajor)major=strmajor;void Student:SetAve(int strave)ave=strave;bool Student:operator !=(Student& stu)if(=name)return false;return true;bool Student:operator >(Student&am

39、p; stu1)if(stu1.ave>ave)return true;return false;string Student:GetName()return name;void Student:EditPerson()Test.cpp#include"LinkList.cpp"#include"Student.h"#include<iostream>#include<string>#include<windows.h>using namespace std;void menu()cout<<&quo

40、t;a 、增加一个学生信息 "<<endl; cout<<"b 、打印链表 "<<endl;cout<<"c 、获取当前链表的长度 "<<endl; cout<<"d 、查询学生的信息 "<<endl; cout<<"e 、删除学生信息 "<<endl; cout<<"f 、修改学生的信息 "<<endl;cout<<"g 、按平均成

41、绩进行排序 "<<endl;cout<<"h 、帮助 "<<endl;cout<<"o 、退出 "<<endl;void main()char ch;LinkList<Student> list; list.Open("Student.txt");string name,sex,ID,brithday,major,newname; int length,pos,ave;Student stu,delstu;menu();cout<<"

42、;input"<<endl;doch=getchar();switch(ch)case'h':system("cls");menu();break;case'a':system("cls");/stu.EditPerson();cout<<" 请输入学生的 "<<endl;cin>>name;stu.SetName(name);cout<<" 请输入学生的性别 "<<endl;cin>>se

43、x;stu.SetSex(sex);cout<<" 请输入学生的生日 "<<endl;cin>>brithday; stu.SetBrith(brithday);cout<<" 请输入学生的学号 "<<endl; cin>>ID;stu.SetID(ID);cout<<" 请输入学生的专业 "<<endl;cin>>major;stu.SetMajor(major);cout<<" 请输入平均成绩 &quo

44、t;<<endl;cin>>ave;stu.SetAve(ave); list.Insert(1,stu); list.Save("Student.txt");cout<<" 请输入选择(帮助选项 -> h ) : "<<endl; break;case'b':system("cls");list.PrintLinkList();cout<<" 请输入选择(帮助选项 -> h ) : "<<endl; break;c

45、ase'c':system("cls");cout<<" 当前链表的长度为: "<<endl; length=list.ListLength(); cout<<"length="<<length<<endl;cout<<" 请输入选择(帮助选项 -> h ) : "<<endl; break;case'd':system("cls");cout<<" 现有学生的名字: "<<endl;list.DisplayName();cout<<endl;cout<<" 请输入学生的 "<<endl;cin>>name;stu.SetName(name);delstu=list.Fi

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论