




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、. . . . #include <iostream>#include <fstream>#include <string>using namespace std;struct BookInfochar loginname10;char bookname20;char author20;char number5;char date10;double price;struct Nodestruct BookInfo book; struct Node *next;void option();void select();Node *head;Node *pt10
2、;FILE *fp;/創(chuàng)建鏈表/*Node *CrtNode(int n) Node *head; /聲明頭指針headNode *p,*s; head=new Node; /創(chuàng)建頭結(jié)點(diǎn)由head指向(空的頭結(jié)點(diǎn))s=head; cout<<"請輸圖書信息:"<<endl;for(int i=0;i<n;i+)p=new Node; /創(chuàng)建一個結(jié)點(diǎn)cout<<"登錄名:"cin.getline(p->book.loginname,10);strcat(p->book.loginname,"n
3、");cout<<"書名:"cin.getline(p->book.bookname,20);strcat(p->book.bookname,"n");cout<<"作者名:"cin.getline(p->book.author,20);strcat(p->book.author,"n");cout<<"分類號:"cin.getline(p->book.number,5);strcat(p->book.number
4、,"n");cout<<"出版日期:"cin>>p->book.date;strcat(p->book.date,"n");cout<<"價格:"cin>>p->book.price;s->next=p; /把創(chuàng)建的結(jié)點(diǎn)由s的next指向s=p; /指針s的指向向后移一個結(jié)點(diǎn)cin.clear();cin.sync();p->next=NULL; /最后一個結(jié)點(diǎn)的next指向空return head; /返回頭指針*/*strcat(p-
5、>book.loginname,"n");strcat(p->book.bookname,"n");strcat(p->book.author,"n");strcat(p->book.number,"n");strcat(p->book.date,"n");*/1.插入void Insert(Node *head)Node *p,*s;s = head;cout<<"請輸入圖書信息:"<<endl;p=new Node;
6、cin.clear();cout<<"登錄名:"cin.getline(p->book.loginname,10);strcat(p->book.loginname,"n");cin.clear();cin.sync();cout<<"書名:"cin.getline(p->book.bookname,20);strcat(p->book.bookname,"n");cin.clear();cin.sync();cout<<"作者名:"c
7、in.getline(p->book.author,20);strcat(p->book.author,"n");cin.clear();cin.sync();cout<<"分類號:"cin.getline(p->book.number,5);strcat(p->book.number,"n");cin.clear();cin.sync();cout<<"出版日期(yyyy-mm-dd):"cin>>p->book.date;strcat(p->
8、;book.date,"n");cout<<"價格:"cin>>p->book.price;while(s->next)s = s->next;s->next = p;p->next = NULL;/初始化Node *Initial()Node *head;head = new Node;head->next = NULL;return head;/2.顯示所有信息void Show(Node *head) int i = 1;Node *p;/顯示除頭結(jié)點(diǎn)以后所有結(jié)點(diǎn)(因為創(chuàng)建時頭結(jié)點(diǎn)為空)p
9、=head->next; if(p = NULL)cout<<"系統(tǒng)沒有儲存任何圖書信息,請輸入圖書信息后再進(jìn)行其他操作!"<<endl;elsecout<<"*下面是所有的圖書信息*"<<endl;while(p!=NULL)cout<<" 圖書"<<i<<": "<<p->book.bookname<<endl;cout<<"登錄名:"<<p->
10、;book.loginname;cout<<"書名:"<<p->book.bookname;cout<<"作者名:"<<p->book.author;cout<<"分類號:"<<p->book.number;cout<<"出版日期:"<<p->book.date;cout<<"價格:"<<p->book.price;cout<<endl
11、;p=p->next;i+;cout<<endl;cout<<"請按回車鍵返回菜單。"<<endl;cin.get();/3.查找int findauthor(const Node *head)Node *ps;char author20;int count = 0;ps = head->next;cout<<"請輸入作者名:"cin.getline(author,20);strcat(author,"n");while(ps)if(strcmp(ps->book.au
12、thor,author) = 0)pt0 = ps;count+;ps = ps->next;if(count = 0)cout<<"查找的圖書不存在!"<<endl;return count;int findbookname(const Node *head)Node *ps;char bookname20;int count = 0;int i = 0;ps = head->next;cout<<"請輸入書名:"cin.getline(bookname,20);strcat(bookname,"
13、;n");while(ps)if(strcmp(ps->book.bookname,bookname) = 0)pti = ps;count+;i+;if(ps->next =NULL)break;ps = ps->next;if(count = 0)cout<<"查找的圖書不存在!"<<endl;return count;void Showarray(int n)cout<<"*下面是所查找的圖書信息*"<<endl;for(int i=0;i<n;i+)cout<
14、<" 圖書"<<i+1<<": "<<pti->book.bookname;cout<<"登錄名:"<<pti->book.loginname;cout<<"書名:"<<pt0->book.bookname;cout<<"作者名:"<<pti->book.author;cout<<"分類號:"<<pti->bo
15、ok.number;cout<<"出版日期:"<<pti->book.date;cout<<"價格:"<<pti->book.price;cout<<endl;cin.get();int Find(Node *head)int n,num;system("cls");if(head->next = NULL)cout<<"系統(tǒng)沒有儲存任何圖書信息,請輸入圖書信息后再進(jìn)行其他操作!"<<endl;cin.get();
16、elsecout<<"請選擇查找方式(1.按作者名查找 2.按書名查找):"cin>>n;cin.clear();cin.sync();switch(n)case 1:num = findauthor(head);if(num != 0)Showarray(num);break;case 2:num = findbookname(head);if(num !=0)Showarray(num);break;default:cout<<"輸入有誤,請重新輸入!"<<endl;cin.get();system(&
17、quot;cls");option();select();return num;/4.修改圖書信息void Modify(Node *head)Node *p,*q,*s;p = head->next;int i,n;if(p = NULL)cout<<"系統(tǒng)沒有儲存任何圖書信息,請輸入圖書信息后再進(jìn)行其他操作!"<<endl;elsecout<<"請輸入需要更正信息的圖書名:"<<endl;n = findbookname(head);Showarray(n);loop:cout<&
18、lt;"請選擇對第幾本書的信息進(jìn)行修改:"cin>>i;cin.clear();cin.sync();if(i > 0 & i <= n)cout<<"*修改第"<<i<<"本書的其他信息*"<<endl;cout<<"登錄名:"cin.getline(pti-1->book.loginname,10);strcat(pti-1->book.loginname,"n");cin.clear()
19、;cin.sync();cout<<"作者名:"cin.getline(pti-1->book.author,20);strcat(pti-1->book.author,"n");cin.clear();cin.sync();cout<<"分類號:"cin.getline(pti-1->book.number,5);strcat(pti-1->book.number,"n");cin.clear();cin.sync();cout<<"出版日期(
20、yyyy-mm-dd):"cin>>pti-1->book.date;strcat(pti-1->book.date,"n");cout<<"價格:"cin>>pti-1->book.price;elsecout<<"選擇的書的序號有誤!"goto loop;cin.get();/5.刪除圖書信息void Deletebook(Node *head)Node *p,*q;char bookname20;int count = 0;int i = 0;p = h
21、ead;q = p->next;if(q = NULL)cout<<"系統(tǒng)沒有任何圖書信息!"<<endl;elsecout<<"請輸入書名:"cin.getline(bookname,20);strcat(bookname,"n");while(q != NULL)q = p->next;if(strcmp(q->book.bookname,bookname) = 0)p->next = q->next;delete q;count+;p = q;q = q->
22、next;if(count = 0)cout<<"沒有找到該圖書信息!"<<endl;elsecout<<"圖書信息已經(jīng)刪除!"<<endl;cin.get();/鏈表長度int Len(const Node *head)Node *ps;int count = 0;ps = head->next;while(ps)count+;ps = ps->next;return count;/6.保存文件void save(Node *head)Node *p;char st20;p = head-&g
23、t;next;if(fp=fopen("book.dat", "w")=NULL)cout<<"打開文件失敗"<<endl;return;char t255; /將 L1.listlen() 賦予字符串中的數(shù)字int lenth = Len(head);fwrite(&lenth,sizeof(int),1,fp);while(p)fwrite(p,sizeof(struct Node),1,fp);p = p->next;fclose(fp);cout<<"文件已經(jīng)保存,請
24、按ENTER鍵退出!"<<endl;cin.get();/8.刪除所有圖書信息void Free_List(Node *head) Node *p;p = head->next;while(p)delete p;p = head->next;/*void readstr(FILE *f,char *string)do /: 先讀入一行文本fgets(string, 255, f); /fgets(): 從文件 f 讀入長度為 255-1 的字符串 / 并存入到 string 中 while (string0 = '/') | (string0
25、= 'n');return;*/讀取文件Node *Load()char c255;int lenth;Node *p,*q;head = new Node;p = head;if(fp=fopen("book.dat", "r")=NULL)cout<<"打開文件失敗"<<endl;return head;elsefread(&lenth,sizeof(int),1,fp);for(int i = 0;i < lenth;i+)q = new Node;fread(q,sizeof(struct Node),1,fp);p->next = q;p = q;p->next = NULL;fclose(fp);return head;/9.菜單選項void select()int m,n;cout<<"請輸入以下數(shù)字來選擇所需要的操作(18):"cin.clear();cin.sync
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 員工簽員工合同范本
- 倆兄弟合伙開店合同范本
- 與他人合伙經(jīng)營合同范本
- 共享汽車租車合同范本
- 冷鏈購銷合同范本
- 合伙出資金合同范本
- 員工協(xié)議簡易合同范本
- 合同范本水印有問題
- 共同建房合同范本
- 廠房質(zhì)押合同范本
- 三年級下冊豎式脫式計算
- 七年級數(shù)學(xué)蘇科版下冊 101 二元一次方程 課件
- 《財務(wù)風(fēng)險的識別與評估管理國內(nèi)外文獻(xiàn)綜述》
- ??谑写媪糠抠I賣合同模板(范本)
- 經(jīng)典文學(xué)作品中的女性形象研究外文文獻(xiàn)翻譯2016年
- 高爐煤氣安全知識的培訓(xùn)
- 2008 年全國高校俄語專業(yè)四級水平測試試卷
- 需求供給與均衡價格PPT課件
- 金融工程鄭振龍課后習(xí)題答案
- 時間單位換算表
- DTSD342-9N說明書(精編版)
評論
0/150
提交評論