C語(yǔ)言鏈表實(shí)現(xiàn)簡(jiǎn)單圖書(shū)管理系統(tǒng)
本文實(shí)例為大家分享了C語(yǔ)言鏈表實(shí)現(xiàn)圖書(shū)管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)功能:
用C語(yǔ)言制作圖書(shū)管理系統(tǒng),實(shí)現(xiàn)圖書(shū)進(jìn)行登記書(shū)籍,瀏覽書(shū)籍,借閱書(shū)籍,歸還書(shū)籍,書(shū)籍排序,刪除書(shū)籍信息,查找書(shū)籍等功能。
功能展示
1.登記書(shū)籍

2.瀏覽書(shū)籍

3.借閱書(shū)籍

4.歸還書(shū)籍

5.書(shū)籍排序

6.刪除書(shū)籍

7.查找書(shū)籍

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("書(shū)名\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("*************圖書(shū)管理系統(tǒng)************\n");
?? ?printf("*——————0.退出系統(tǒng)——————*\n");
?? ?printf("*——————1.登記書(shū)籍——————*\n");
?? ?printf("*——————2.瀏覽書(shū)籍——————*\n");
?? ?printf("*——————3.借閱書(shū)籍——————*\n");
?? ?printf("*——————4.歸還書(shū)籍——————*\n");
?? ?printf("*——————5.書(shū)籍排序——————*\n");
?? ?printf("*——————6.刪除書(shū)籍——————*\n");
? ? printf("*——————7.查找書(shū)籍——————*\n");
? ? printf("**************************************\n");
? ? printf("請(qǐng)輸入(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("輸入書(shū)籍的信息(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("請(qǐng)輸入借閱的書(shū)名:");
?? ??? ?scanf("%s", tempBook.name);
?? ??? ?result = searchByName(list, tempBook.name);
?? ??? ?if (result == NULL)
?? ??? ?{
?? ??? ??? ?printf("沒(méi)有相關(guān)書(shū)籍無(wú)法借閱!\n");
?? ??? ?}
?? ??? ?else
?? ??? ?{
?? ??? ??? ?if (result->data.num > 0)
?? ??? ??? ?{
?? ??? ??? ??? ?result->data.num--;
?? ??? ??? ??? ?printf("借閱成功!\n");
?? ??? ??? ?}
?? ??? ??? ?else
?? ??? ??? ?{
?? ??? ??? ??? ?printf("當(dāng)前書(shū)籍無(wú)庫(kù)存,借閱失??!\n");
?? ??? ??? ?}
?? ??? ?}
?? ??? ?break;
?? ?case 4:
?? ??? ?printf("【歸還】\n"); ? ??
?? ??? ?printf("請(qǐng)輸入歸還的書(shū)名:");
?? ??? ?scanf("%s", tempBook.name);
?? ??? ?result = searchByName(list, tempBook.name);
?? ??? ?if (result == NULL)
?? ??? ?{
?? ??? ??? ?printf("該書(shū)無(wú)借出記錄\n");
?? ??? ?}
?? ??? ?else
?? ??? ?{
?? ??? ??? ?result->data.num++;
?? ??? ??? ?printf("書(shū)籍歸還成功!\n");
?? ??? ?}
?? ??? ?break;
?? ?case 5:
?? ??? ?printf("【排序】\n");
?? ??? ?bubbleSortlist(list);
?? ??? ?break;
?? ?case 6:
?? ??? ?printf("【刪除】\n");
?? ??? ?printf("請(qǐng)輸入刪除書(shū)名:");
?? ??? ?scanf("%s", tempBook.name);
?? ??? ?deleteNodeByName(list, tempBook.name);
?? ??? ?saveInfoFile("bookinfo.txt", list);
?? ??? ?break;
?? ?case 7:
?? ??? ?printf("【查找】\n");
?? ??? ?printf("請(qǐng)輸入要查詢的書(shū)名:");
?? ??? ?scanf("%s", tempBook.name);
?? ??? ?result = searchByName(list, tempBook.name);
?? ??? ?if (result == NULL)
?? ??? ?{
?? ??? ??? ?printf("未找到相關(guān)信息!\n");
?? ??? ?}
?? ??? ?else
?? ??? ?{
?? ??? ??? ?printf("書(shū)名\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;
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C語(yǔ)言實(shí)現(xiàn)圖書(shū)管理系統(tǒng)
- C語(yǔ)言圖書(shū)管理系統(tǒng)簡(jiǎn)潔版
- C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單圖書(shū)管理系統(tǒng)
- C語(yǔ)言圖書(shū)管理系統(tǒng)課程設(shè)計(jì)
- C語(yǔ)言鏈表實(shí)現(xiàn)圖書(shū)管理系統(tǒng)
- C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的圖書(shū)管理系統(tǒng)
- C語(yǔ)言實(shí)現(xiàn)圖書(shū)館管理系統(tǒng)
- C語(yǔ)言實(shí)現(xiàn)圖書(shū)管理系統(tǒng)(文件數(shù)據(jù)庫(kù))
- C語(yǔ)言圖書(shū)管理系統(tǒng)實(shí)驗(yàn)
- C語(yǔ)言單鏈表實(shí)現(xiàn)圖書(shū)管理系統(tǒng)
相關(guān)文章
C語(yǔ)言銀行系統(tǒng)課程設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言銀行系統(tǒng)課程設(shè)計(jì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
C語(yǔ)言實(shí)現(xiàn)計(jì)算器的兩種方法
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)計(jì)算器的兩種方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
安裝OpenMPI來(lái)配合C語(yǔ)言程序進(jìn)行并行計(jì)算
這篇文章主要介紹了安裝OpenMPI來(lái)配合C語(yǔ)言程序進(jìn)行并行計(jì)算的例子,MPI的全稱是Message Passing Interface即標(biāo)準(zhǔn)消息傳遞界面,可以用于并行計(jì)算,需要的朋友可以參考下2015-11-11
C語(yǔ)言中結(jié)構(gòu)體(struct)的幾種初始化方法
相信大家都知道struct結(jié)構(gòu)體是C語(yǔ)言中非常重要的復(fù)合類型,初始化的方法很多,那么小編下面對(duì)這些方法進(jìn)行總結(jié),便于自己和大家以后查閱,有需要的可以參考借鑒。2016-08-08

