C++使用鏈表實(shí)現(xiàn)圖書管理系統(tǒng)
本文實(shí)例為大家分享了vue + element ui實(shí)現(xiàn)錨點(diǎn)定位的具體代碼,供大家參考,具體內(nèi)容如下
一、程序?qū)崿F(xiàn)功能
1.錄入書籍:將書籍錄入圖書管理系統(tǒng)
2.瀏覽書籍:查看圖書管理系統(tǒng)里的所有書籍
3.借閱書籍:書籍存在可以借閱,庫存-1,書的庫存不足則無法借閱
4.歸還書籍:庫存+1,如果該書不是圖書館里的書籍,則無法錄入
5.刪除書籍:以書名為基礎(chǔ)從圖書管理系統(tǒng)中刪除該書籍
6.查找書籍:按書名查找書籍,顯示書籍的基本信息
7.排序書籍:按價(jià)格將書籍排序(降序)
二、要求
使用函數(shù)、指針和鏈表編寫。
三、程序功能圖

四、具體函數(shù)

五、程序代碼
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
?
struct bookinfo
{
?? ?char name[20]; ? //書名
?? ?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("書名\t作者\(yùn)t出版日期\t價(jià)格\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)
?? ??? ?{
?? ??? ??? ?/*排序書籍(按價(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ǔ)書籍信息
?? ?struct Node* searchname = NULL; //生成一個(gè)臨時(shí)變量存儲(chǔ)查找的書名
?? ?struct Node* borrowbook = NULL; //生成一個(gè)臨時(shí)變量存儲(chǔ)要借閱的書名
? ? struct Node* returnbook = NULL; //生成一個(gè)臨時(shí)變量存儲(chǔ)要?dú)w還的書名
?? ?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("請(qǐng)輸入要借閱的書名:");
? ? ? ? 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("請(qǐng)輸入要?dú)w還的書名:");
? ? ? ? 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("請(qǐng)輸入要?jiǎng)h除的書名:");
?? ??? ?scanf("%s",tempbook.name);?
? ? ? ? deletebook(list,tempbook.name); ? ?/*按書名刪除書籍*/
?? ??? ?printList(list);
?? ??? ?break;
?? ?case 6:
?? ??? ?printf("[ 查找書籍 ]\n");
?? ??? ?printf("請(qǐng)輸入要查詢的書名:");
?? ??? ?scanf("%s",tempbook.name);
?? ??? ?searchname = searchbook(list,tempbook.name);
?? ??? ?if(searchname == NULL)
?? ??? ?{
?? ??? ??? ?printf("不存在該書,請(qǐng)加購錄入!\n");
?? ??? ?}
?? ??? ?else
?? ??? ?{
?? ??? ??? ?/*輸出該書的信息*/
?? ??? ??? ?printf("書名\t作者\(yùn)t出版日期\t價(jià)格\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"); /*按價(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.錄入書籍


2.瀏覽書籍

3.借閱書籍
存在該書時(shí),借閱成功,庫存-1:

不存在該書時(shí),無法借閱:

4.歸還書籍

當(dāng)圖書管理系統(tǒng)里本不存在該書,則歸還失?。?/p>

5.查找書籍

不存在該書時(shí),則查找失?。?/p>

6.排序書籍
再錄入書籍:

排序(降序):

7.刪除書籍

以上為該程序的所有功能。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C/C++函數(shù)參數(shù)傳遞機(jī)制詳解及實(shí)例
這篇文章主要介紹了C/C++函數(shù)參數(shù)傳遞機(jī)制詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02
C++可變參數(shù)的函數(shù)與模板實(shí)例分析
這篇文章主要介紹了C++可變參數(shù)的函數(shù)與模板,非常重要的概念,需要的朋友可以參考下2014-08-08
C語言中函數(shù)與指針的應(yīng)用總結(jié)
本篇文章是對(duì)C語言中函數(shù)與指針的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

