C語言鏈表實(shí)現(xiàn)簡單圖書管理系統(tǒng)
本文實(shí)例為大家分享了C語言鏈表實(shí)現(xiàn)圖書管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)功能:
用C語言制作圖書管理系統(tǒng),實(shí)現(xiàn)圖書進(jìn)行登記書籍,瀏覽書籍,借閱書籍,歸還書籍,書籍排序,刪除書籍信息,查找書籍等功能。
功能展示
1.登記書籍
2.瀏覽書籍
3.借閱書籍
4.歸還書籍
5.書籍排序
6.刪除書籍
7.查找書籍
8.退出程序
代碼如下
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h> ? struct bookInfo { ?? ?char name[20]; ?? ?float price; ?? ?int num; }; ? struct Node ? { ?? ?struct bookInfo data; ?? ?struct Node* next; }; struct Node* list = NULL; ? struct Node* createHead() { ?? ?struct Node* headNode = (struct Node*)malloc(sizeof(struct Node)); ?? ?headNode->next = NULL; ?? ?return headNode; } ? ? struct Node* createNode(struct bookInfo data) { ?? ?struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); ?? ?newNode->data = data; ?? ?newNode->next = NULL; ?? ?return newNode; } ? ? void insertNodeByhead(struct Node* headNode, struct bookInfo data) { ?? ?struct Node* newNode = createNode(data); ?? ?newNode->next = headNode->next; ?? ?headNode->next = newNode; } ? void deleteNodeByName(struct Node* headNode, char *bookName) { ?? ?struct Node* posLeftNode = headNode; ?? ?struct Node* posNode = headNode->next; ?? ?while (posNode != NULL && strcmp(posNode->data.name, bookName)) ?? ?{ ?? ??? ?posLeftNode = posNode; ?? ??? ?posNode = posLeftNode->next; ?? ?} ?? ?if (posNode == NULL) ?? ??? ?return; ?? ?else ?? ?{ ?? ??? ?printf("刪除成功!\n"); ?? ??? ?posLeftNode->next = posNode->next; ?? ??? ?free(posNode); ?? ??? ?posNode = NULL; ?? ?} } struct Node* searchByName(struct Node* headNode, char* bookName) { ?? ?struct Node* posNode = headNode->next; ?? ?while (posNode != NULL && strcmp(posNode->data.name, bookName)) ?? ?{ ?? ??? ?posNode = posNode->next; ?? ?} ?? ?return posNode; } ? void printlist(struct Node* headNode) { ?? ?struct Node* pMove = headNode->next; ?? ?printf("書名\t價(jià)格\t數(shù)量\t作者\(yùn)t出版社\n"); ?? ?while (pMove!=NULL) ?? ?{ ?? ??? ?printf("%s\t%.1f\t%d\n", pMove->data.name, pMove->data.price, pMove->data.num); ?? ??? ?pMove = pMove->next; ?? ?} } ? void makeMenu() { ?? ?printf("*************************************\n"); ?? ?printf("*************圖書管理系統(tǒng)************\n"); ?? ?printf("*——————0.退出系統(tǒng)——————*\n"); ?? ?printf("*——————1.登記書籍——————*\n"); ?? ?printf("*——————2.瀏覽書籍——————*\n"); ?? ?printf("*——————3.借閱書籍——————*\n"); ?? ?printf("*——————4.歸還書籍——————*\n"); ?? ?printf("*——————5.書籍排序——————*\n"); ?? ?printf("*——————6.刪除書籍——————*\n"); ? ? printf("*——————7.查找書籍——————*\n"); ? ? printf("**************************************\n"); ? ? printf("請輸入(0 ~ 7):"); } ? void saveInfoFile(const char* fileName, struct Node* headNode) { ?? ?FILE* fp = fopen(fileName, "w");?? ? ?? ?struct Node* pMove = headNode->next; ?? ?while (pMove != NULL) ?? ?{ ?? ??? ?fprintf(fp, "%s\t%.1f\t%d\n", pMove->data.name, pMove->data.price, pMove->data.num); ?? ??? ?pMove = pMove->next; ?? ?} ?? ?fclose(fp); } ? void readInfoFromFile(const char* fileName, struct Node* headNode) { ?? ?FILE* fp = fopen(fileName, "r"); ?? ?if (fp == NULL) ?? ?{ ?? ??? ?fp = fopen(fileName, "w+"); ?? ?} ?? ?struct bookInfo tempData; ?? ?while (fscanf(fp, "%s\t%f\t%d\n", tempData.name, &tempData.price, &tempData.num) != EOF) ?? ?{ ?? ??? ?insertNodeByhead(list, tempData); ?? ?} ?? ?fclose(fp); } ? void bubbleSortlist(struct Node* headNode) { ?? ?for (struct Node* p = headNode->next; p != NULL; p = p->next) ?? ?{ ?? ??? ?for (struct Node* q = headNode->next; q->next != NULL; q = q->next) ?? ??? ?{ ?? ??? ??? ?if (q->data.price > q->next->data.price) ?? ??? ??? ?{ ?? ??? ??? ??? ?struct bookInfo tempData = q->data; ?? ??? ??? ??? ?q->data = q->next->data; ?? ??? ??? ??? ?q->next->data = tempData; ?? ??? ??? ?} ?? ??? ?} ?? ?} ?? ?printlist(headNode); } ? void keyDown() { ?? ?int userKey = 0; ?? ?struct bookInfo tempBook; ?? ?struct Node* result = NULL; ?? ?scanf("%d",&userKey); ?? ?switch (userKey) ?? ?{ ?? ?case 0: ?? ??? ?printf("【退出】\n"); ?? ??? ?printf("退出成功\n"); ?? ??? ?system("pause"); ?? ??? ?exit(0); ? ? ? ? ? ? ? ? ? ?? ??? ?break; ?? ?case 1: ?? ??? ?printf("【登記】\n"); ?? ??? ?printf("輸入書籍的信息(name,price,num):"); ?? ??? ?scanf("%s%f%d", tempBook.name, &tempBook.price, &tempBook.num); ?? ??? ?insertNodeByhead(list, tempBook); ?? ??? ?saveInfoFile("bookinfo.txt", list ); ?? ??? ?break; ?? ?case 2: ?? ??? ?printf("【瀏覽】\n"); ?? ??? ?printlist(list); ?? ??? ?break; ?? ?case 3: ?? ??? ?printf("【借閱】\n"); ? ? ?? ??? ?printf("請輸入借閱的書名:"); ?? ??? ?scanf("%s", tempBook.name); ?? ??? ?result = searchByName(list, tempBook.name); ?? ??? ?if (result == NULL) ?? ??? ?{ ?? ??? ??? ?printf("沒有相關(guān)書籍無法借閱!\n"); ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?if (result->data.num > 0) ?? ??? ??? ?{ ?? ??? ??? ??? ?result->data.num--; ?? ??? ??? ??? ?printf("借閱成功!\n"); ?? ??? ??? ?} ?? ??? ??? ?else ?? ??? ??? ?{ ?? ??? ??? ??? ?printf("當(dāng)前書籍無庫存,借閱失??!\n"); ?? ??? ??? ?} ?? ??? ?} ?? ??? ?break; ?? ?case 4: ?? ??? ?printf("【歸還】\n"); ? ?? ?? ??? ?printf("請輸入歸還的書名:"); ?? ??? ?scanf("%s", tempBook.name); ?? ??? ?result = searchByName(list, tempBook.name); ?? ??? ?if (result == NULL) ?? ??? ?{ ?? ??? ??? ?printf("該書無借出記錄\n"); ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?result->data.num++; ?? ??? ??? ?printf("書籍歸還成功!\n"); ?? ??? ?} ?? ??? ?break; ?? ?case 5: ?? ??? ?printf("【排序】\n"); ?? ??? ?bubbleSortlist(list); ?? ??? ?break; ?? ?case 6: ?? ??? ?printf("【刪除】\n"); ?? ??? ?printf("請輸入刪除書名:"); ?? ??? ?scanf("%s", tempBook.name); ?? ??? ?deleteNodeByName(list, tempBook.name); ?? ??? ?saveInfoFile("bookinfo.txt", list); ?? ??? ?break; ?? ?case 7: ?? ??? ?printf("【查找】\n"); ?? ??? ?printf("請輸入要查詢的書名:"); ?? ??? ?scanf("%s", tempBook.name); ?? ??? ?result = searchByName(list, tempBook.name); ?? ??? ?if (result == NULL) ?? ??? ?{ ?? ??? ??? ?printf("未找到相關(guān)信息!\n"); ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?printf("書名\t價(jià)格\t數(shù)量\t作者\(yùn)t出版社\n"); ?? ??? ??? ?printf("%s\t%.1f\t%d\n", result->data.name, result->data.price, result->data.num); ?? ??? ?} ?? ??? ?break; ?? ?default: ?? ??? ?printf("【error】\n"); ?? ??? ?break; ?? ?} } ? int main() { ?? ?list= createHead(); ?? ?readInfoFromFile("bookinfo.txt", list); ?? ?while (1) ?? ?{ ?? ??? ?makeMenu(); ?? ??? ?keyDown(); ?? ??? ?system("pause"); ?? ??? ?system("cls"); ?? ?} ?? ?system("pause"); ?? ?return 0; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言數(shù)據(jù)結(jié)構(gòu)系列篇二叉樹的概念及滿二叉樹與完全二叉樹
在上一章中我們正式開啟了對數(shù)據(jù)結(jié)構(gòu)中樹的講解,介紹了樹的基礎(chǔ)。本章我們將學(xué)習(xí)二叉樹的概念,介紹滿二叉樹和完全二叉樹的定義,并對二叉樹的基本性質(zhì)進(jìn)行一個(gè)簡單的介紹。本章附帶課后練習(xí)2022-02-02C++ inline內(nèi)聯(lián)函數(shù)詳解
這篇文章主要介紹了C++ inline內(nèi)聯(lián)函數(shù)詳解,有感興趣的同學(xué)可以借鑒參考下2021-02-02C++11中bind綁定器和function函數(shù)對象介紹
這篇文章主要介紹了C++11中bind綁定器和function函數(shù)對象介紹,綁定器,函數(shù)對象和lambda表達(dá)式只能使用在一條語句中,更多相關(guān)內(nèi)容需要的小伙伴可以參考一下2022-07-07notepad介紹及插件cmake編譯過程(替代notepad++)
這篇文章主要介紹了notepad介紹及插件cmake編譯過程(替代notepad++),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03