C++使用鏈表實現(xiàn)圖書管理系統(tǒng)
本文實例為大家分享了vue + element ui實現(xiàn)錨點定位的具體代碼,供大家參考,具體內(nèi)容如下
一、程序?qū)崿F(xiàn)功能
1.錄入書籍:將書籍錄入圖書管理系統(tǒng)
2.瀏覽書籍:查看圖書管理系統(tǒng)里的所有書籍
3.借閱書籍:書籍存在可以借閱,庫存-1,書的庫存不足則無法借閱
4.歸還書籍:庫存+1,如果該書不是圖書館里的書籍,則無法錄入
5.刪除書籍:以書名為基礎從圖書管理系統(tǒng)中刪除該書籍
6.查找書籍:按書名查找書籍,顯示書籍的基本信息
7.排序書籍:按價格將書籍排序(降序)
二、要求
使用函數(shù)、指針和鏈表編寫。
三、程序功能圖
四、具體函數(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("當前書籍庫存不足,借閱失??!\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); ? ? ? ? ? ? /*關閉整個程序*/ ?? ??? ?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.歸還書籍
當圖書管理系統(tǒng)里本不存在該書,則歸還失?。?/p>
5.查找書籍
不存在該書時,則查找失敗:
6.排序書籍
再錄入書籍:
排序(降序):
7.刪除書籍
以上為該程序的所有功能。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C語言fprintf()函數(shù)和fscanf()函數(shù)的具體使用
本文主要介紹了C語言fprintf()函數(shù)和fscanf()函數(shù)的具體使用,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11