您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“C++如何使用鏈表實現(xiàn)圖書管理系統(tǒng)”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“C++如何使用鏈表實現(xiàn)圖書管理系統(tǒng)”這篇文章吧。
具體內(nèi)容如下
1.錄入書籍:將書籍錄入圖書管理系統(tǒng)
2.瀏覽書籍:查看圖書管理系統(tǒng)里的所有書籍
3.借閱書籍:書籍存在可以借閱,庫存-1,書的庫存不足則無法借閱
4.歸還書籍:庫存+1,如果該書不是圖書館里的書籍,則無法錄入
5.刪除書籍:以書名為基礎(chǔ)從圖書管理系統(tǒng)中刪除該書籍
6.查找書籍:按書名查找書籍,顯示書籍的基本信息
7.排序書籍:按價格將書籍排序(降序)
使用函數(shù)、指針和鏈表編寫。
#include<stdio.h> #include<stdlib.h> #include<string.h> struct bookinfo { char name[20]; //書名 char author[10]; //作者 char date[20]; //出版日期 float price; //價格 int num; //數(shù)量 }; struct Node { struct bookinfo data; struct Node* next; }; /*全局鏈表*/ struct Node* list = NULL; /*創(chuàng)建表頭*/ struct Node* createhead() { /*動態(tài)內(nèi)存申請*/ struct Node* headNode = (struct Node*)malloc(sizeof(struct Node)); headNode->next = NULL; return headNode; } /*創(chuàng)建節(jié)點*/ struct Node* createNode(struct bookinfo data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; return newNode; } void printList(); void display_menu(); void savebookfile(); void insertbook(); void readbookfile(); void deletebook(); struct Node* searchbook(); void sortbook(); void selectkey(); /*打印鏈表*/ void printList(struct Node* headNode) { struct Node* Bmove = headNode->next; printf("書名\t作者\t出版日期\t價格\t庫存\n"); while(Bmove != NULL) { printf("%s\t%s\t%s\t%.1f\t%d\n",Bmove->data.name,Bmove->data.author,Bmove->data.date,Bmove->data.price,Bmove->data.num); Bmove = Bmove->next; } } /*菜單登錄界面*/ void display_menu() { char str[100]; FILE *fp; char *txt; fp = fopen("menu.txt","r"); txt = fgets(str,100,fp); while(txt != NULL) { printf("%s",str); txt = fgets(str,100,fp); } fclose(fp); } /*將信息存到文件中*/ void savebookfile(const char* filename,struct Node* headNode) { FILE* fp = fopen(filename,"w"); struct Node* Bmove = headNode->next; while(Bmove != NULL) { fprintf(fp,"%s\t%s\t%s\t%.1f\t%d\n",Bmove->data.name,Bmove->data.author,Bmove->data.date,Bmove->data.price,Bmove->data.num); Bmove = Bmove->next; } fclose(fp); } /*錄入書籍*/ void insertbook(struct Node* headNode,struct bookinfo data) { struct Node* newNode = createNode(data); newNode->next = headNode->next; headNode->next = newNode; } /*讀取文件*/ void readbookfile(const char* filename, struct Node* headNode) { FILE* fp = fopen(filename,"r"); if(fp == NULL) { fp = fopen(filename,"w+"); } struct bookinfo tempinfo; while(fscanf(fp, "%s\t%s\t%s\t%.1f\t%d\n",tempinfo.name,&tempinfo.author,&tempinfo.date,&tempinfo.price,&tempinfo.num ) != EOF) { insertbook(list,tempinfo); } fclose(fp); } /*刪除書籍*/ void deletebook(struct Node* headNode,char *bookname) { struct Node* leftNode = headNode; struct Node* rightNode = headNode->next; while(rightNode != NULL && strcmp(rightNode->data.name,bookname)) { leftNode = rightNode; rightNode = leftNode->next; } if(leftNode == NULL) { return; } else { printf("刪除書籍成功!\n"); leftNode->next = rightNode->next; free(rightNode); rightNode = NULL; } } /*查找書籍*/ struct Node* searchbook(struct Node* headNode, char* bookname) { struct Node* rightNode = headNode->next; while (rightNode != NULL && strcmp(rightNode->data.name, bookname)) { rightNode = rightNode->next; } return rightNode; } /*排序書籍*/ void sortbook(struct Node* headNode) { for(struct Node* i = headNode->next; i != NULL; i = i->next) { for(struct Node* j = headNode->next;j->next != NULL;j = j->next) { /*排序書籍(按價格降序)*/ if (j->data.price < j->next->data.price) { /*交換值*/ struct bookinfo tempdata = j->data; j->data = j->next->data; j->next->data = tempdata; } } } /*排序后查看效果*/ printList(headNode); } /*交互界面*/ void selectkey() { int userkey = 0; struct bookinfo tempbook; //生成一個臨時的變量存儲書籍信息 struct Node* searchname = NULL; //生成一個臨時變量存儲查找的書名 struct Node* borrowbook = NULL; //生成一個臨時變量存儲要借閱的書名 struct Node* returnbook = NULL; //生成一個臨時變量存儲要歸還的書名 scanf("%d",&userkey); switch(userkey) { case 1: printf("[ 錄入書籍 ]\n"); printf("輸入書籍的信息(name,author,date,price,num):"); scanf("%s%s%s%f%d",tempbook.name,&tempbook.author,&tempbook.date,&tempbook.price,&tempbook.num); insertbook(list,tempbook); /*把書籍信息保存到booksinfo文本文件里*/ savebookfile("bookinfo.txt",list); break; case 2: printf("[ 瀏覽書籍 ]\n"); printList(list); break; case 3: printf("[ 借閱書籍 ]\n"); /*書籍存在可以借閱,庫存-1,書的庫存不足則無法借閱*/ printf("請輸入要借閱的書名:"); scanf("%s",tempbook.name); borrowbook = searchbook(list,tempbook.name); if(borrowbook == NULL) { printf("不存在該書,無法借閱!\n"); } else { if(borrowbook->data.num > 0) { borrowbook->data.num--; printf("借閱成功!\n"); printList(list); } else { printf("當(dāng)前書籍庫存不足,借閱失敗!\n"); } } break; case 4: printf("[ 歸還書籍 ]\n"); //庫存+1 printf("請輸入要歸還的書名:"); scanf("%s",tempbook.name); returnbook = searchbook(list,tempbook.name); if(returnbook == NULL) { printf("該書不是圖書館里的書籍!\n"); } else { returnbook->data.num++; printf("書籍歸還成功!\n"); printList(list); } break; case 5: printf("[ 刪除書籍 ]\n"); printf("請輸入要刪除的書名:"); scanf("%s",tempbook.name); deletebook(list,tempbook.name); /*按書名刪除書籍*/ printList(list); break; case 6: printf("[ 查找書籍 ]\n"); printf("請輸入要查詢的書名:"); scanf("%s",tempbook.name); searchname = searchbook(list,tempbook.name); if(searchname == NULL) { printf("不存在該書,請加購錄入!\n"); } else { /*輸出該書的信息*/ printf("書名\t作者\t出版日期\t價格\t庫存\n"); printf("%s\t%s\t%s\t%.1f\t%d\n",searchname->data.name,searchname->data.author,searchname->data.date,searchname->data.price,searchname->data.num); } break; case 7: printf("[ 排序書籍 ]\n"); /*按價格排序(降序)*/ sortbook(list); break; case 8: printf("[ 退出系統(tǒng) ]\n"); printf("退出成功\n"); system("pause"); exit(0); /*關(guān)閉整個程序*/ break; default: printf("[ 錯誤 ]\n"); break; } } /*主界面*/ int main() { list = createhead(); readbookfile("bookinfo.txt",list); while(1) { display_menu(); selectkey(); system("pause"); system("cls"); } system("pause"); return 0; }
1.錄入書籍
2.瀏覽書籍
3.借閱書籍
存在該書時,借閱成功,庫存-1:
不存在該書時,無法借閱:
4.歸還書籍
當(dāng)圖書管理系統(tǒng)里本不存在該書,則歸還失敗:
5.查找書籍
不存在該書時,則查找失敗:
6.排序書籍
再錄入書籍:
排序(降序):
7.刪除書籍
以上是“C++如何使用鏈表實現(xiàn)圖書管理系統(tǒng)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。