單鏈表實(shí)現(xiàn)圖書(shū)管理系統(tǒng)_第1頁(yè)
單鏈表實(shí)現(xiàn)圖書(shū)管理系統(tǒng)_第2頁(yè)
單鏈表實(shí)現(xiàn)圖書(shū)管理系統(tǒng)_第3頁(yè)
單鏈表實(shí)現(xiàn)圖書(shū)管理系統(tǒng)_第4頁(yè)
單鏈表實(shí)現(xiàn)圖書(shū)管理系統(tǒng)_第5頁(yè)
已閱讀5頁(yè),還剩4頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上單鏈表:typedef struct char num20; char name50; float pri; Book;typedef struct LNode /線性表的單鏈表存儲(chǔ) Book book; /數(shù)據(jù)域struct LNode *next; /指針域LNode,*LinkList;void Input(LinkList &L) /前插法創(chuàng)建圖書(shū)鏈表 LinkList p; L=new LNode; L->next=NULL;/初始化單鏈表 ifstream inFile("book.txt"); if(!inFile)ce

2、rr<<"Cannot open this file!"<<endl;exit(1); char book_head110,book_head210,book_head310; /定義文件中標(biāo)題 inFile>>book_head1>>book_head2>>book_head3;/讀取文件中的標(biāo)題 while(!inFile.eof()/到達(dá)文件尾部前逐行依次讀取所有圖書(shū)數(shù)據(jù) p=new LNode; /生成新結(jié)點(diǎn) inFile>>p->book.num>>p->book.na

3、me>>p->book.pri; p->next=L->next; /插入到表頭 L->next=p;h inFile.close(); cout<<"讀取完畢!"<<endl;void Output(LinkList &L)/輸出 LinkList p; p=L->next; while(p) cout<<left<<setw(15)<<p->book.num<<"t"<<left<<setw(50)&

4、lt;<p-><<"t"<<left<<setw(5)<<p->book.pri<<endl; p=p->next; cout<<"n信息顯示完畢n"<<endl;void Searchname(LinkList &L) LinkList p; p=L->next; char name120; cout<<"請(qǐng)輸入要查找的書(shū)名:" cin>>name1; while(p)

5、if(strcmp(name1,p->)=0) cout<<left<<setw(15)<<p->book.num<<"t"<<left<<setw(50)<<p-><<"t"<<left<<setw(5)<<p->book.pri<<endl; p=p->next; void Searchnum(LinkList &L) int i; i

6、nt j=0; LinkList p; p=L->next; /p指向第一個(gè)結(jié)點(diǎn) cout<<"請(qǐng)輸入要查找的書(shū)籍的位置" cin>>i; while(p&&j<i-1) /順著鏈表向后掃描,直到p指向第i個(gè)元素或p為空 j+;p=p->next; if(!p|j>i) /第i個(gè)元素不存在 cout<<"錯(cuò)誤!"<<endl; cout<<"第"<<i<<"本書(shū)信息如下:"<<en

7、dl; cout<<left<<setw(15)<<p->book.num<<"t"<<left<<setw(50)<<p-><<"t"<<left<<setw(5)<<p->book.pri<<endl; void Max(LinkList &L) LinkList pmax,p; pmax=L->next; p=L->next->next; whi

8、le(p) if(p->book.pri>pmax->book.pri)pmax=p; p=p->next; cout<<left<<setw(15)<<pmax->book.num<<"t"<<left<<setw(50)<<pmax-><<"t"<<left<<setw(5)<<pmax->book.pri<<endl;void Sort(LinkL

9、ist L)cout<<"此功能還未實(shí)現(xiàn),研究中"<<endl;void Inverse(LinkList &L)LinkList p; /待插入到鏈表的節(jié)點(diǎn)LinkList q; p = L->next; L->next = NULL; /初始化鏈表 ofstream outFile("bookinverse.txt");/每一次將p插入到L(頭結(jié)點(diǎn))與L->next(鏈表的第一個(gè)結(jié)點(diǎn)之間) while (p) q = p->next; p->next = L->next; L->

10、;next = p; p = q; p=L->next;/循環(huán)完后,p指向的是鏈表末尾,需要重置回首元結(jié)點(diǎn),否則無(wú)法寫(xiě)入文件 while(p) cout<<setw(15)<<p->book.num<<"t"<<setw(50)<<p-><<"t"<<setw(5)<<p->book.pri<<endl; outFile<<left<<setw(15)<<p->bo

11、ok.num<<"t"<<left<<setw(50)<<p-><<"t"<<left<<setw(5)<<p->book.pri<<endl; p=p->next; cout<<"信息已寫(xiě)入bookinverse.txt中!"<<endl;void Count(LinkList L) int i=0; LinkList p; p=L->next; while(p

12、) i+; p=p->next; cout<<"當(dāng)前的圖書(shū)總數(shù)為:"<<i<<endl;void Insert(LinkList L) Book bk; LinkList p=L; /初始化 LinkList s=new LNode; /新結(jié)點(diǎn)s int i; int j=0; cout<<"請(qǐng)輸入待插入書(shū)籍的位置"<<endl; cin>>i; cout<<"請(qǐng)輸入待插入的書(shū)籍的信息"<<endl; cout<<&quo

13、t;書(shū)號(hào): "<<endl; cin>>bk.num; cout<<"書(shū)名: "<<endl; cin>>; cout<<"價(jià)格: "<<endl; cin>>bk.pri; while(p&&j<i-1) /尋找第i-1個(gè)結(jié)點(diǎn) j+; p=p->next; if(!p|j>i-1) cout<<"錯(cuò)誤!"<<endl; s->book=bk; /將結(jié)點(diǎn)

14、s的數(shù)據(jù)域置為bk s->next=p->next; /將結(jié)點(diǎn)s插入L中 p->next=s; /重新寫(xiě)入book.txt文件 ofstream outFile("book.txt"); p=L->next; while(p) outFile<<left<<setw(15)<<p->book.num<<"t"<<left<<setw(50)<<p-><<"t"<<left&l

15、t;<setw(5)<<p->book.pri<<endl; p=p->next; cout<<"插入新的書(shū)籍后的書(shū)籍信息已重新寫(xiě)入book.txt文件"<<endl;void Delete(LinkList L) Book bk1; LinkList p=L; /初始化 LinkList q; /臨時(shí)保存被刪除結(jié)點(diǎn)的地址以備釋放 int i; int j=0; cout<<"請(qǐng)輸入待刪除書(shū)籍的位置"<<endl; cin>>i; while(p->

16、;next&&j<i-1) j+; p=p->next; if(!(p->next)|j>i-1) cout<<"錯(cuò)誤!"<<endl; q=p->next; p->next=q->next; bk1=q->book; delete q; /重新寫(xiě)入book.txt文件 ofstream outFile("book.txt"); p=L->next; while(p) outFile<<left<<setw(15)<<p-&g

17、t;book.num<<"t"<<left<<setw(50)<<p-><<"t"<<left<<setw(5)<<p->book.pri<<endl; p=p->next; cout<<"刪除所選書(shū)籍后的書(shū)籍信息已重新寫(xiě)入book.txt文件"<<endl;順序表typedef struct Book *book; int length;/圖書(shū)表的圖書(shū)記錄個(gè)數(shù)SqLi

18、st;void Input(SqList &L) int i=0;char book_head110,book_head210,book_head310; /定義文件中標(biāo)題 L.book=new BookMAXSIZE; ifstream inFile("book.txt"); /打開(kāi)文件if(!inFile)cerr<<"Cannot open this file!"<<endl;exit(1);inFile>>book_head1>>book_head2>>book_head3;/讀

19、取文件中的標(biāo)題L.length=0;while(!inFile.eof() /逐行依次讀取所有圖書(shū)數(shù)據(jù)inFile>>L.booki.num >>L.>>L.booki.pri;i+; /記錄圖書(shū)個(gè)數(shù) L.length=i;inFile.close();cout<<"n讀取 book.txt 信息完畢!"<<endl;void Output(SqList L) for(int i=0;i<L.length;i+)cout<<left<<setw(15)<<

20、;L.booki.num<<"t"<<left<<setw(50)<<L.<<"t"<<left<<setw(5)<<L.booki.pri<<endl;cout<<"n信息顯示完畢n"<<endl;void Searchname(SqList &L) int y; char name120; cout<<"請(qǐng)輸入要查找的書(shū)名:" cin>

21、>name1; for(y=0;y<L.length;y+)/比較輸入的name1與結(jié)構(gòu)體數(shù)組中name是否相同; if(strcmp(name1,L.)=0) cout<<left<<setw(15)<<L.booky.num<<"t"<<left<<setw(50)<<L.<<"t"<<left<<setw(5)<<L.booky.pri<<endl; br

22、eak; void Searchnum(SqList &L) int i; cout<<"請(qǐng)輸入要查找的書(shū)的位置"<<endl; cin>>i; cout<<left<<setw(15)<<L.booki-1.num<<left<<setw(50)<<L.<<left<<setw(5)<<L.booki-1.pri<<endl;void Max(SqList L)int max=0;cou

23、t<<"價(jià)格最高的書(shū)是:"<<endl;cout<<"書(shū)號(hào)"<<"t"<<"書(shū)名"<<"t"<<"價(jià)格"<<endl;for (int i=0;i<L.length;i+)if(L.booki.pri>L.bookmax.pri)max=i;cout<<left<<setw(15)<<L.bookmax.num<<&quo

24、t;t"<<left<<setw(50)<<L.<<"t"<<left<<setw(5)<<L.bookmax.pri<<endl;for (int j=0;j<L.length-1;j+)if(j!=max)if(L.bookj.pri=L.bookmax.pri)cout<<left<<setw(15)<<L.bookj.num<<"t"<<left<

25、<setw(50)<<L.<<"t"<<left<<setw(5)<<L.bookj.pri<<endl;cout<<endl;void Sort(SqList L) /冒泡排序 Book book1; /重新定義一個(gè)結(jié)構(gòu)體變量 for(int m=0;m<L.length-1;m+) for(int n=0;n<L.length-m-1;n+)if(L.bookn.pri<L.bookn+1.pri) /交換結(jié)構(gòu)體數(shù)組的值book1=L.book

26、n;L.bookn=L.bookn+1;L.bookn+1=book1; /寫(xiě)入booksort.txt文件 ofstream outFile("booksort.txt",ios:out); for(m=0;m<L.length-1;m+) outFile<<left<<setw(15)<<L.bookm.num<<"t"<<left<<setw(50)<<L.<<"t"<<left<<s

27、etw(5)<<L.bookm.pri<<endl; outFile.close(); cout<<"排序后書(shū)籍信息已寫(xiě)入booksort.txt文件"<<endl;void Inverse(SqList L) Input(L); Book book2; int i,j,k; k=L.length/2; for(i=0;i<k;i+)j=L.length-1-i;book2=L.booki; L.booki=L.bookj;L.bookj=book2;/寫(xiě)入bookinverse.txt文件ofstream outFil

28、e("bookinverse.txt");for(i=0;i<L.length;i+) outFile<<left<<setw(15)<<L.booki.num<<"t"<<left<<setw(50)<<L.<<"t"<<left<<setw(5)<<L.booki.pri<<endl; outFile.close(); cout<<"排序后書(shū)

29、籍信息已寫(xiě)入bookinverse.txt文件"<<endl;void Count(SqList L) cout<<"當(dāng)前的圖書(shū)總數(shù)為:"<<L.length<<endl<<endl;void Insert(SqList L) Book book3; int i; int j; cout<<"請(qǐng)輸入待插入書(shū)籍的位置"<<endl; cin>>i; cout<<"請(qǐng)輸入待插入的書(shū)籍的信息"<<endl; co

30、ut<<"書(shū)號(hào): "<<endl; cin>>book3.num; cout<<"書(shū)名: "<<endl; cin>>; cout<<"價(jià)格: "<<endl; cin>>book3.pri; if(i<1|i>L.length+1) /i值不合法 cout<<"輸入的位置錯(cuò)誤,請(qǐng)重新輸入:"<<endl; cout<<"請(qǐng)輸入待插入書(shū)籍的位置"<<endl; cin>>i; for(j=L.length-1;j>=i-1;j-) L.bookj+1=L.bookj; /輸入位置及之后的元素位置后移 L.booki-1=book3; /將新元素book3放入第i個(gè)位置 L.length+; /表長(zhǎng)增加1 /重新寫(xiě)入book.txt文件 ofstream outFile("book.txt"); for(i=0;i<L.length;i

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論