C++使用鏈表實(shí)現(xiàn)圖書(shū)管理系統(tǒng)
本文實(shí)例為大家分享了vue + element ui實(shí)現(xiàn)錨點(diǎn)定位的具體代碼,供大家參考,具體內(nèi)容如下
一、程序?qū)崿F(xiàn)功能
1.錄入書(shū)籍:將書(shū)籍錄入圖書(shū)管理系統(tǒng)
2.瀏覽書(shū)籍:查看圖書(shū)管理系統(tǒng)里的所有書(shū)籍
3.借閱書(shū)籍:書(shū)籍存在可以借閱,庫(kù)存-1,書(shū)的庫(kù)存不足則無(wú)法借閱
4.歸還書(shū)籍:庫(kù)存+1,如果該書(shū)不是圖書(shū)館里的書(shū)籍,則無(wú)法錄入
5.刪除書(shū)籍:以書(shū)名為基礎(chǔ)從圖書(shū)管理系統(tǒng)中刪除該書(shū)籍
6.查找書(shū)籍:按書(shū)名查找書(shū)籍,顯示書(shū)籍的基本信息
7.排序書(shū)籍:按價(jià)格將書(shū)籍排序(降序)
二、要求
使用函數(shù)、指針和鏈表編寫(xiě)。
三、程序功能圖
四、具體函數(shù)
五、程序代碼
#include<stdio.h> #include<stdlib.h> #include<string.h> ? struct bookinfo { ?? ?char name[20]; ? //書(shū)名 ?? ?char author[10]; //作者 ?? ?char date[20]; ? //出版日期 ?? ?float price; ? ? //價(jià)格 ?? ?int num; ? ? ? ? //數(shù)量 }; ? struct Node { ?? ?struct bookinfo data; ?? ?struct Node* next; }; ? /*全局鏈表*/ struct Node* list = NULL; ? /*創(chuàng)建表頭*/ struct Node* createhead() { ?? ?/*動(dòng)態(tài)內(nèi)存申請(qǐng)*/ ?? ?struct Node* headNode = (struct Node*)malloc(sizeof(struct Node)); ? ? headNode->next = NULL; ?? ?return headNode; } ? /*創(chuàng)建節(jié)點(diǎn)*/ 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("書(shū)名\t作者\(yùn)t出版日期\t價(jià)格\t庫(kù)存\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); } ? /*錄入書(shū)籍*/ 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); } ? /*刪除書(shū)籍*/ 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("刪除書(shū)籍成功!\n"); ?? ??? ?leftNode->next = rightNode->next; ?? ??? ?free(rightNode); ?? ??? ?rightNode = NULL; ?? ?} } ? /*查找書(shū)籍*/ 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; } ? /*排序書(shū)籍*/ 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) ?? ??? ?{ ?? ??? ??? ?/*排序書(shū)籍(按價(jià)格降序)*/ ?? ??? ??? ?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; ?//生成一個(gè)臨時(shí)的變量存儲(chǔ)書(shū)籍信息 ?? ?struct Node* searchname = NULL; //生成一個(gè)臨時(shí)變量存儲(chǔ)查找的書(shū)名 ?? ?struct Node* borrowbook = NULL; //生成一個(gè)臨時(shí)變量存儲(chǔ)要借閱的書(shū)名 ? ? struct Node* returnbook = NULL; //生成一個(gè)臨時(shí)變量存儲(chǔ)要?dú)w還的書(shū)名 ?? ?scanf("%d",&userkey); ?? ?switch(userkey) ?? ?{ ?? ?case 1: ?? ??? ?printf("[ 錄入書(shū)籍 ]\n"); ?? ??? ?printf("輸入書(shū)籍的信息(name,author,date,price,num):"); ? ? ? ? scanf("%s%s%s%f%d",tempbook.name,&tempbook.author,&tempbook.date,&tempbook.price,&tempbook.num); ?? ??? ?insertbook(list,tempbook); ?? ??? ?/*把書(shū)籍信息保存到booksinfo文本文件里*/ ?? ??? ?savebookfile("bookinfo.txt",list); ?? ??? ?break; ?? ?case 2: ?? ??? ?printf("[ 瀏覽書(shū)籍 ]\n"); ?? ??? ?printList(list); ?? ??? ?break; ?? ?case 3: ?? ??? ?printf("[ 借閱書(shū)籍 ]\n"); ? /*書(shū)籍存在可以借閱,庫(kù)存-1,書(shū)的庫(kù)存不足則無(wú)法借閱*/ ?? ??? ?printf("請(qǐng)輸入要借閱的書(shū)名:"); ? ? ? ? scanf("%s",tempbook.name); ?? ??? ?borrowbook = searchbook(list,tempbook.name); ?? ??? ?if(borrowbook == NULL) ?? ??? ?{ ?? ??? ??? ?printf("不存在該書(shū),無(wú)法借閱!\n"); ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?if(borrowbook->data.num > 0) ?? ??? ??? ?{ ?? ??? ??? ??? ?borrowbook->data.num--; ?? ??? ??? ??? ?printf("借閱成功!\n"); ? ? ? ? ? ? ? ? printList(list); ?? ??? ??? ?} ?? ??? ??? ?else ?? ??? ??? ?{ ?? ??? ??? ??? ?printf("當(dāng)前書(shū)籍庫(kù)存不足,借閱失?。n"); ?? ??? ??? ?} ?? ??? ?} ?? ??? ?break; ?? ?case 4: ?? ??? ?printf("[ 歸還書(shū)籍 ]\n"); ?//庫(kù)存+1 ?? ??? ?printf("請(qǐng)輸入要?dú)w還的書(shū)名:"); ? ? ? ? scanf("%s",tempbook.name); ?? ??? ?returnbook = searchbook(list,tempbook.name); ?? ??? ?if(returnbook == NULL) ?? ??? ?{ ?? ??? ??? ?printf("該書(shū)不是圖書(shū)館里的書(shū)籍!\n"); ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?returnbook->data.num++; ?? ??? ??? ?printf("書(shū)籍歸還成功!\n"); ?? ??? ??? ?printList(list); ?? ??? ?} ?? ??? ?break; ?? ?case 5: ?? ??? ?printf("[ 刪除書(shū)籍 ]\n"); ?? ??? ?printf("請(qǐng)輸入要?jiǎng)h除的書(shū)名:"); ?? ??? ?scanf("%s",tempbook.name);? ? ? ? ? deletebook(list,tempbook.name); ? ?/*按書(shū)名刪除書(shū)籍*/ ?? ??? ?printList(list); ?? ??? ?break; ?? ?case 6: ?? ??? ?printf("[ 查找書(shū)籍 ]\n"); ?? ??? ?printf("請(qǐng)輸入要查詢的書(shū)名:"); ?? ??? ?scanf("%s",tempbook.name); ?? ??? ?searchname = searchbook(list,tempbook.name); ?? ??? ?if(searchname == NULL) ?? ??? ?{ ?? ??? ??? ?printf("不存在該書(shū),請(qǐng)加購(gòu)錄入!\n"); ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?/*輸出該書(shū)的信息*/ ?? ??? ??? ?printf("書(shū)名\t作者\(yùn)t出版日期\t價(jià)格\t庫(kù)存\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("[ 排序書(shū)籍 ]\n"); /*按價(jià)格排序(降序)*/ ? ? ? ? sortbook(list); ?? ??? ?break; ? ?? ?case 8: ?? ??? ?printf("[ 退出系統(tǒng) ]\n"); ?? ??? ?printf("退出成功\n"); ?? ??? ?system("pause"); ?? ??? ?exit(0); ? ? ? ? ? ? /*關(guān)閉整個(gè)程序*/ ?? ??? ?break; ?? ?default: ?? ??? ?printf("[ 錯(cuò)誤 ]\n"); ?? ??? ?break; ?? ?} } ? /*主界面*/ int main() { ?? ?list = createhead(); ?? ?readbookfile("bookinfo.txt",list); ?? ?while(1) ?? ?{ ?? ??? ?display_menu(); ?? ??? ?selectkey(); ?? ??? ?system("pause"); ?? ??? ?system("cls"); ?? ?} ? ? system("pause"); ?? ?return 0; }
六、效果
1.錄入書(shū)籍
2.瀏覽書(shū)籍
3.借閱書(shū)籍
存在該書(shū)時(shí),借閱成功,庫(kù)存-1:
不存在該書(shū)時(shí),無(wú)法借閱:
4.歸還書(shū)籍
當(dāng)圖書(shū)管理系統(tǒng)里本不存在該書(shū),則歸還失?。?/p>
5.查找書(shū)籍
不存在該書(shū)時(shí),則查找失敗:
6.排序書(shū)籍
再錄入書(shū)籍:
排序(降序):
7.刪除書(shū)籍
以上為該程序的所有功能。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C++實(shí)現(xiàn)圖書(shū)管理程序
- C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)源碼
- C++編寫(xiě)實(shí)現(xiàn)圖書(shū)管理系統(tǒng)
- C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)簡(jiǎn)易版
- C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)課程設(shè)計(jì)(面向?qū)ο?
- C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)課程設(shè)計(jì)
- C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)(文件操作與類)
- C++圖書(shū)管理系統(tǒng)程序源代碼
- C++詳細(xì)實(shí)現(xiàn)完整圖書(shū)管理功能
相關(guān)文章
C語(yǔ)言fprintf()函數(shù)和fscanf()函數(shù)的具體使用
本文主要介紹了C語(yǔ)言fprintf()函數(shù)和fscanf()函數(shù)的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11