




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、C+數(shù)據(jù)結(jié)構(gòu)學(xué)生管理系統(tǒng)單鏈表實現(xiàn)用模板類實現(xiàn),實現(xiàn)操作符的重載非常詳細(xì)的代碼及其操作1、 Stude nt.h2、private:string name; 姓名 string ID;/ 學(xué)號 string sex;/ 性別 string major;/ 專業(yè) string brithday;/ 生日 int ave;/ 均分Stude nt();void SetName(stri ng& strn ame);void SetSex(stri ng& strsex);void SetBrith(stri ng& strbrith);void SetID(stri ng& strID);void
2、 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 ofstream& operator(ofstream& ost,Stude nt& stu);string GetName();2、核心代碼ost
3、ream& operator(ostrea m& ost,Stude nt& stu) ostName stu .n amee ndl; ostSexstu.sexe ndl;ostBrithday stu.brithdaye ndl; ostIDstu.IDe ndl;ostMajorstu.majore ndl;return ost;ifstream& operator(ifstream& ost,Stude nt& stu)oststu .n ame;oststu.sex; oststu.brithday; oststuD;oststu.major;return ost;Lfstrea
4、m& operator (ofstrea m& ost,Stude nt& stu) oststu .n amee ndl; oststu.sexe ndl; oststu.brithdaye ndl; ostst u.I De ndl; oststu.major(Student& stu1) if(stu1.aveave)return true;return false;3、測試代碼度 序_息4e排 信的息息行 生心皐 學(xué)槌的富績生生生宓 -鋰豈或盂于均 亠加印署專平Hut pF-sr 、力 rn Lah cd g fsrE Q -1a ;es a csystem(cls);/stu.Edi
5、tPerso n();cout請輸入學(xué)生的姓名e ndl; cinn ame;stu.SetName( name);cout請輸入學(xué)生的性別sex;stu.SetSex(sex);cout請輸入學(xué)生的生日 brithday;stu.SetBrith(brithday);cout請輸入學(xué)生的學(xué)號 ID;stu.SetlD(ID);cout請輸入學(xué)生的專業(yè)endl; cinm ajor;stu.SetMajor(major);cout請輸入平均成績 ave;stu.SetAve(ave);list.I nsert(1,stu);list.Save(Stude nt.txt);cout h ) :
6、endl; break;男1996-10-104物理85system(cls);list.Pri ntL in kList();cout h ) : endl; break;SexBrithday ID Major fl veirasfeName Sex Brithday ID Major A uerageName Sex Bpithday ID Maj op Rue 取Name Sex Bj*ithda.y IDajor uepae男4物理&5小張 男 l?95-10-i0英語S2女語文78嚴(yán)1994-10-101數(shù)學(xué)99casec:cout當(dāng)前鏈表的長度為:e ndl;len gth=li
7、st.ListLe ngth();當(dāng)前犍表的長度貴;length=4請礙人選擇(幫助選項一h ):cased:system(cls);cout現(xiàn)有學(xué)生的名字:e ndl;list.DisplayName();coute ndl;cout請輸入學(xué)生的姓名e ndl; cinn ame;stu.SetName( name);delstu=list.Fi nd(stu);I庫入學(xué)生的姓名小楊Ge z男Dr-itID4Major物理請輸入選擇(幫助選項-h )=cased:cout現(xiàn)有學(xué)生的名字:e ndl; list.DisplayName();coute ndl;cout請輸入學(xué)生的姓名e ndl
8、; cinn ame;stu.SetName( name);delstu=list.Fi nd(stu); coutdelstu;3、源代碼入刪除學(xué)生的姓名打關(guān)NameSexBritMav IDMajor Average NameSexBa* ithday ID joi* rveraseNarSexB?it liday ID小張m1795-10-10玉語S2小李女1?9S-10-10備文78f王1994-10-101數(shù)學(xué)casef:system(cls);cout現(xiàn)有學(xué)生的名字:e ndl;list.DisplayName();cout請輸入修改的學(xué)生的姓名:e ndl;cinn ame;tu
9、.SetName( name);delstu=list.Fi nd(stu);pos=list.Locate(stu);list.Delete(pos);cout請輸入學(xué)生的新的信息:e ndl;coute ndl;cout請輸入學(xué)生的姓名e ndl;cinn ame;stu.SetName( name);cout請輸入學(xué)生的性別 sex;stu.SetSex(sex);cout請輸入學(xué)生的生日 brithday;stu.SetBrith(brithday);cout請輸入學(xué)生的學(xué)號 ID;stu.SetID(ID);cout請輸入學(xué)生的專業(yè)endl;cinm ajor;stu.SetMajo
10、r(major); .cout請輸入平均成績 ave;stu SetAve(ave);請輸入學(xué)生的新的信息乗婁入學(xué)生的姓名 賞入學(xué)生的性別 書輸入學(xué)生的生日1?6-10-10請輸入學(xué)生的學(xué)號 祓入學(xué)生的專業(yè)請輸入平均成績Caseg:list.Sort();list .Prin tLin kList();Bitbday IDNaneSex:IDMajorAuerageManeSexBp-ithdavIDlaLjoi* u er-ajgre1996-10-10花學(xué)980王1994- 10-101數(shù)學(xué)小張男1995- 10-10黃語S5LinkList h#ifndef Lin kList_H#de
11、fine Lin kList_H#in elude #in clude using n amespace std; template struct Node T data;Node *n ext;template class Lin kListNode *head;public: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 Print
12、Lin kList(); void Insert(int i,T item); T Delete(int i);個值void In vert();friend void Merge(LinkList &L1, void DisplayNode(i nt i);void Save(char fname);void Open( char fname); void DisplayName();void Sort();/元素自身的信息,數(shù)據(jù)域/后繼元素存儲地址,地址域/單鏈表的頭指針/求鏈表的長度/按位查找,取單鏈表中第POS個節(jié)點的/查找函數(shù),以T類型的數(shù)據(jù)作為參數(shù)/按值查找,求單鏈表中值為 ite
13、m的元/在i的位置插入元素 item/在單鏈表中刪除第i個節(jié)點,并返回這/逆置函數(shù) &L2);/歸并鏈表/保存/顯示名字/排序;#en difLin kList.cpp#i ncludeL in kList.htemplate/用于創(chuàng)建一個帶有頭結(jié)點的空鏈表Lin kList:Li nkList()head=new Node; 私有head-next=NULL;template/*用于創(chuàng)建一個帶有頭結(jié)點的空鏈表*/Lin kList:Li nkList(T a,i nt n)尾插法Node *rear;rear=head; 指向當(dāng)前單鏈表的最后一個節(jié)點for(i nt i=O;i n; i+)
14、Node *s;s=new Node;s-data=ai;rear- n ext=s;/rear=head rear=s;/rear一直是指向單鏈表的最后一項rear-next=NULL;單鏈表創(chuàng)建結(jié)束,最后一個節(jié)點的指針置為空templatevoid LinkList:SetLinList(T a,int n)head=new Node;Node *rear=head;for(i nt i=0;i n; i+)Node *s;s=new Node;s-data=ai;rear-n ext=s;rear=s;rear- next=NULL;單鏈表創(chuàng)建結(jié)束,最后一個節(jié)點的指針置為空templat
15、eint Li nkList:ListLe ngth()int num=0;Node *p; p=head-n ext;while(p)p=p-n ext;nu m+; return num;templateT Lin kList:Get(i nt pos)Node *p;int j=1;p=head- next;/p指向頭結(jié)點的下一個節(jié)點while(p&jn ext;j+;if(!p|jpos)coutdata;templateT LinkList:Find(T item)Node *p;p=head-n ext;while(p&p-data!=item)p=p-n ext;if(!p)co
16、utdata;templateint Lin kList:Locate(T item)Node *p; p=head-n ext;int j=1;while(p&p-data!=item)p=p-n ext;j+;if(p)return j;else return 0;templatevoid Lin kList:Pri ntLi nkList()Node *p; p=head-n ext;while(p)coutdatan ext;templatevoid Lin kList:I nsert(i nt i,T item) Node *p; p=head; int j=0;while(p&jn
17、 ext;j+; if(!p)cerr插入位置非法; exit(1);elseNode *s=new Node;s-data=item;s-n ext=p-n ext;p_n ext=s;templateT Lin kList:Delete(i nt i)Node *p,*q;T x;p=head;int j=0;while(p&jn ext;j+;if(!p|!p- next)cerrn ext;x=q-data;p_n ext=q _n ext;delete q;return x;/*利用頭插法*/templatevoid Lin kList:I nvert()Node *p;p=head
18、-n ext;head- next=NULL;將逆置后的單鏈表初始化為空表while(p!=NULL)Node *q=p;p=p-n ext;q-n ext=head-n ext;head-n ext=q;templateLin kList:Li nkList()Node *q=new Node;while(head!=NULL)q=head;head=head-n ext;delete q;template歸并鏈表void Merge(LinkList &L1 ,LinkList &L2)Node *p1,*p2,*p3;p仁L1.head- next;/指向第一個數(shù)據(jù)的節(jié)點p2=L2.he
19、ad-n ext;p3=L1.head; 指向L1的頭結(jié)點 while(p1!=NULL)&(p2!=NULL)if(p1-data)data)p3-n ext=p1;p1=p1- n ext;p3=p3-n ext;elsep3-n ext=p2;p2=p2-n ext;p3=p3-n ext;if(p1!=NULL)/p2 為空的時候p3-n ext=p1;if(p2!=NULL) p3-n ext=p2;delete L2.head;L2.head=NULL;templatevoid Li nkList:Save(char fname) ofstream fout(fname); fou
20、tListLe ngth()e ndl; Node *p;p=head-n ext; while(p!=NULL)foutdatan ext;fout.close();templatevoid Lin kList:Ope n( char fname) ifstream fin( fname);int n;finn;T item;for(i nt i=0;i n; i+)/Node *p=new Node;/fin p_data;fin item;In sert(i+1,item);fin. close();templatevoid Lin kList:DisplayName()Node *p;
21、p=head-n ext;while(p)coutdata.GetName()n ext;templatevoid Li nkList:Sort()Node *p;int len=ListLe ngth();for(i nt i=1;in ext;for(i nt j=O;jdatap-n ext-data)T temp=p-data; p-data=p-n ext-data; p-n ext-data=temp;p=p-n ext;Stude nt.h#i ncludeL in kList.h#in clude#in cludeusing n amespace std;class Stude
22、 nt;ostrea m& operator(ifstream& ost,Stude nt& stu); ofstream& operator (Stude nt& stu);friend ostrea m& operator(ifstream& ost,Stude nt& stu);friend ofstream& operator(ofstrea m& ost,Stude nt& stu); stri ng GetName();Stude nt.cpp#i ncludeStude nt.h#in cludeusing n amespace std;ostrea m& operator(os
23、tream& ost,Stude nt& stu)ostNamestu .n amee ndl;ostSexstu.sexe ndl;ostBrithday stu.brithdaye ndl; ostIDstu.IDe ndl;ostMajor stu.majore ndl; ostAverage stu.ave(ifstream& ost,Stude nt& stu)oststu .n ame;oststu.sex;oststu.brithday; oststuD;oststu.major; oststu.ave;return ost;ofstream& operator (ofstrea
24、m& ost,Stude nt& stu) oststu .n amee ndl;oststu.sexe ndl; oststu.brithdaye ndl; ostst u.I De ndl; oststu.majore ndl; oststu.ave(Stude nt& stu1)if(stu1.aveave)return true;return false;string Stude nt:GetName()return n ame;void Stude nt:EditPerso n()Test.cpp#i ncludeLi nkList.cpp#i ncludeStude nt.h#in
25、 clude#in clude#in cludeusing n amespace std;void menu()couta、增加一個學(xué)生信息 endl; coutb、打印鏈表endl;coutc、獲取當(dāng)前鏈表的長度 endl; coutd、查詢學(xué)生的信息 endl;coute、刪除學(xué)生信息endl; coutf、修改學(xué)生的信息endl;coutg、按平均成績進(jìn)行排序endl;couth、幫助endl;couto、退出endl;void mai n()char ch;LinkList list;list.Ope n(Stude nt.txt);string name,sex,ID,brithda
26、y,major,newname; int len gth,pos,ave;Stude nt stu,delstu;men u();couti npute ndl;doch=getchar();switch(ch)caseh:system(cls);men u();break;casea:system(cls);/stu.EditPerso n();cout請輸入學(xué)生的endl; cinn ame;stu.SetName( name);cout請輸入學(xué)生的性別 sex;stu.SetSex(sex);cout請輸入學(xué)生的生日 brithday;stu.SetBrith(brithday);cou
27、t請輸入學(xué)生的學(xué)號 ID;stu.SetlD(ID);cout請輸入學(xué)生的專業(yè)endl; cinm ajor;stu.SetMajor(major);cout請輸入平均成績 ave;stu.SetAve(ave);list.I nsert(1,stu);list.Save(Stude nt.txt);cout h ) : endl; break;caseb:system(cls);list.Pri ntL in kList();cout h ) : endl; break;casec:system(cls);cout當(dāng)前鏈表的長度為:endl;len gth=list.ListLe ngth();coutle ngth=le ngthe ndl;cout h ) : endl; break;cased:system(cls);cout現(xiàn)有學(xué)生的名字:endl;list.DisplayName();coute ndl;cout請輸入學(xué)生的en
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 科技職場中的禮儀與職業(yè)素養(yǎng)
- 2025河北保定市望都縣農(nóng)村信用聯(lián)社股份有限公司大堂經(jīng)理服務(wù)外包項目招聘16人筆試參考題庫附帶答案詳解
- 科技助力下的糖尿病營養(yǎng)管理新模式
- 綜采工作面刮板輸送機司機職業(yè)技能理論考試題庫160題(含答案)
- 掘進(jìn)機司機技能理論考試題庫150題(含答案)
- 2025春季中國科學(xué)院金屬研究所職能部門崗位招聘筆試參考題庫附帶答案詳解
- 2025至2030年中國膠砂攪拌鍋數(shù)據(jù)監(jiān)測研究報告
- 電動車智能充電系統(tǒng)在公共交通中的應(yīng)用
- 二零二五年度未婚懷孕分手后男方支付子女教育金及撫養(yǎng)費協(xié)議
- 2025年度競業(yè)限制補償金計算及支付合同(月失效)
- 有機肥配施氮肥對玉米根系生長、氮素利用及產(chǎn)量和品質(zhì)的影響
- 2024年山西省中考語文試卷
- 《大學(xué)美育教程》第二單元-心靈的熏陶:審美活動
- 浙江省溫州市(2024年-2025年小學(xué)五年級語文)人教版期中考試(下學(xué)期)試卷及答案
- 企業(yè)員工背景審查制度
- 數(shù)據(jù)要素白皮書(2024年)-中國通信標(biāo)準(zhǔn)化協(xié)會
- 2023年云南公務(wù)員錄用考試《行測》題
- 電子商務(wù)運營(第二版) 課件 項目1 認(rèn)識電商運營
- 冰淇淋店選址與競爭環(huán)境分析
- 領(lǐng)導(dǎo)干部離任交接表
- 儲運安全檢查分析(SCL)評價記錄
評論
0/150
提交評論