服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++使用链表实现图书管理系统

C++使用链表实现图书管理系统

2022-10-18 13:05 C/C++

这篇文章主要介绍了C++使用链表实现图书管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文为大家分享了vue + element ui实现锚点定位的具体代码,供大家参考。

一、程序实现功能

1.录入书籍:将书籍录入图书管理系统

2.浏览书籍:查看图书管理系统里的所有书籍

3.借阅书籍:书籍存在可以借阅,库存-1,书的库存不足则无法借阅

4.归还书籍:库存+1,如果该书不是图书馆里的书籍,则无法录入

5.删除书籍:以书名为基础从图书管理系统中删除该书籍

6.查找书籍:按书名查找书籍,显示书籍的基本信息

7.排序书籍:按价格将书籍排序(降序)

二、要求

使用函数、指针和链表编写。

三、程序功能图

C++使用链表实现图书管理系统

 

四、具体函数

C++使用链表实现图书管理系统

 

五、程序代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
 
struct bookinfo
{
    char name[20];   //书名
    char author[10]; //作者
    char date[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 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("[ 退出系统 ]\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.录入书籍

C++使用链表实现图书管理系统

C++使用链表实现图书管理系统

2.浏览书籍

C++使用链表实现图书管理系统

3.借阅书籍

存在该书时,借阅成功,库存-1:

C++使用链表实现图书管理系统

不存在该书时,无法借阅:

C++使用链表实现图书管理系统

4.归还书籍

C++使用链表实现图书管理系统

当图书管理系统里本不存在该书,则归还失败:

C++使用链表实现图书管理系统

5.查找书籍

C++使用链表实现图书管理系统

不存在该书时,则查找失败:

C++使用链表实现图书管理系统

6.排序书籍

再录入书籍:

C++使用链表实现图书管理系统

排序(降序):

C++使用链表实现图书管理系统

7.删除书籍

C++使用链表实现图书管理系统

以上为该程序的所有功能。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_44774441/article/details/121685651

延伸 · 阅读

精彩推荐
  • C/C++C语言邻接表建立图详解

    C语言邻接表建立图详解

    这篇文章主要介绍了C语言邻接表建立图,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...

    落春只在无意间6462021-12-22
  • C/C++深入理解c++模板中的class与typename

    深入理解c++模板中的class与typename

    在c++Template中很多地方都用到了typename与class这两个关键字,而且好像可以替换,是不是这两个关键字完全一样呢?下面这篇文章主要给大家介绍了关于c++模板...

    林嘉伟12432021-05-20
  • C/C++C语言中二级指针的实例详解

    C语言中二级指针的实例详解

    这篇文章主要介绍了C语言中二级指针的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握理解二级指针的知识,需要的朋友可以参考下...

    CharlinGod7592021-06-04
  • C/C++C++随机数生成实例讲解

    C++随机数生成实例讲解

    这篇文章主要为大家详细介绍了C++随机数生成实例,如何利用C++来生成0——N-1之间的随机数,感兴趣的小伙伴们可以参考一下...

    C++教程网6962021-03-30
  • C/C++C++数字三角形问题与dp算法

    C++数字三角形问题与dp算法

    这篇文章主要介绍了C++数字三角形问题与dp算法的相关知识,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下...

    会武术之白猫6542021-07-01
  • C/C++C语言编程之初识数组线性查找和二分查找

    C语言编程之初识数组线性查找和二分查找

    本篇文章是C语言编程篇,主要为大家介绍C语言编程中数组的线性查找及二分查找分析讲解,有需要的朋友可以借鉴参考下,希望可以有所帮助...

    Booksort9552022-01-07
  • C/C++C语言位段(位域)机制结构体的特殊实现及解析

    C语言位段(位域)机制结构体的特殊实现及解析

    这篇文章主要为大家介绍了C语言位段位域机制结构体的特殊实现讲解有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪...

    乔乔家的龙龙4462022-09-21
  • C/C++c++ decltype关键字的用法

    c++ decltype关键字的用法

    这篇文章主要介绍了c++ decltype关键字的用法,帮助大家更好的理解和学习c++,感兴趣的朋友可以了解下...

    半杯茶的小酒杯8382021-09-29