欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++使用鏈表實現(xiàn)圖書管理系統(tǒng)

 更新時間:2022年03月12日 12:47:08   作者:唄  
這篇文章主要介紹了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語言中花式退出程序的方式總結(jié)

    C語言中花式退出程序的方式總結(jié)

    在本篇文章當中主要給大家介紹C語言當中一些不常用的特性,比如在main函數(shù)之前和之后設置我們想要執(zhí)行的函數(shù),以及各種花式退出程序的方式,需要的可以參考一下
    2022-10-10
  • CLion安裝、漢化、配置圖文詳解

    CLion安裝、漢化、配置圖文詳解

    這篇文章主要介紹了CLion安裝、漢化、激活、配置圖文詳解,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • 深入理解C/C++中的寫時拷貝

    深入理解C/C++中的寫時拷貝

    這篇文章主要給大家介紹了C/C++中寫時拷貝的相關資料,所謂寫時拷貝也就是拖延版的深拷貝,下面文章中介紹的非常清楚,需要的朋友可以參考學習,下面來一起看看吧。
    2017-03-03
  • C++實例輸入多行數(shù)字到數(shù)組

    C++實例輸入多行數(shù)字到數(shù)組

    這篇文章主要介紹了C++實例輸入多行數(shù)字到數(shù)組的相關資料,這里提供實例代碼幫助大家學習理解,需要的朋友可以參考下
    2016-12-12
  • C語言fprintf()函數(shù)和fscanf()函數(shù)的具體使用

    C語言fprintf()函數(shù)和fscanf()函數(shù)的具體使用

    本文主要介紹了C語言fprintf()函數(shù)和fscanf()函數(shù)的具體使用,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • C++文件依存關系介紹

    C++文件依存關系介紹

    如果現(xiàn)在你做的C++項目(課題)包含的文件沒有超過1000個,你可以選擇略過此文,不過建議繼續(xù)瀏覽
    2013-01-01
  • opencv3/C++圖像邊緣提取方式

    opencv3/C++圖像邊緣提取方式

    今天小編就為大家分享一篇opencv3/C++圖像邊緣提取方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • 詳解C語言中rand函數(shù)的使用

    詳解C語言中rand函數(shù)的使用

    在編程時我們有時總希望自己產(chǎn)生一個隨機數(shù)字,以供使用,那么下面介紹rand函數(shù)的使用,有需要的可以參考學習。
    2016-08-08
  • C/C++ 編譯器優(yōu)化介紹

    C/C++ 編譯器優(yōu)化介紹

    這篇文章主要涉及了C/C++ 編譯器優(yōu)化的簡單介紹,具有一定參考價值。如有不對之處,歡迎指出。
    2017-09-09
  • 變量定義與聲明的區(qū)別詳細解析

    變量定義與聲明的區(qū)別詳細解析

    外部變量(全局變量)的"定義"與外部變量的"聲明"是不相同的,外部變量的定義只能有一次,它的位置是在所有函數(shù)之外,而同一個文件中的外部變量聲明可以是多次的,它可以在函數(shù)之內(nèi)(哪個函數(shù)要用就在那個函數(shù)中聲明)也可以在函數(shù)之外(在外部變量的定義點之前)
    2013-09-09

最新評論