简单实现图书管理系统

大耗子 2020年02月24日 313次浏览

简单实现一个图书管理系统,可以存储书籍和取出书籍,会员登记制度

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
typedef struct book
{
    char name[10];
    struct book* next;
}BOOK,*pBook;

typedef struct node
{
    int num;
    pBook Book;
    struct node* next;
}NODE,*pNode;
//打印图书
void printBook(pBook head)
{
    pBook temp = head;
    int index = 0;
    while (temp)
    {
        printf("    第%d本图书名:%s\n",++index,temp->name);
        temp = temp->next;
    }
    system("pause");
}
//保存图书
void saveBook(pBook head)
{
    FILE* file = fopen("图书管理.txt", "w");
    pBook temp = head->next;
    int index = 0;
    while (temp != nullptr)
    {
        fprintf(file,"%s\n", temp->name);
        index++;
        temp = temp->next;
    }
    fprintf(file, "%2d", index);
    fclose(file);

    printf("保存完成\n");
    getchar();
    getchar();
}
//读取图书
void readBook(pBook head)
{
    while (head->next)
    {
        head = head->next;
    }
    FILE* file = fopen("图书管理.txt", "r");
    int index = 0;
    fseek(file, -2, SEEK_END);
    fscanf(file, "%2d", &index);
    fseek(file, 0, SEEK_SET);
    for (int i = 0; i < index;i++)
    {
        pBook temp = (pBook)malloc(sizeof(BOOK));
        fscanf(file,"%s\n",temp->name);
        head->next = temp;
        head = head->next;
    }
    head->next = NULL;
    printf("读取完成\n");
    getchar();
    getchar();
}
//添加图书
void addBook(pBook head)
{
    while (head->next)
    {
        head = head->next;
    }
    int index = 0;
    printf("请输入要添加的图书数\n");
    scanf("%d",&index);
    for (int i = 0; i < index; i++)
    {
        printf("请输入第%d本书的名字",i+1);
        pBook temp = (pBook)malloc(sizeof(book));
        scanf("%s", temp->name);
        head->next = temp;
        head = head->next;
    }
    head->next = NULL;
    printf("输入完成\n");
    getchar();
    getchar();

}
//查找图书 返回对应图书的上一本书的地址
pBook foundBook(pBook head)//因为用的是单向链表,所以返回上一个地址比较方便
{
    char name[10] = "";
    printf("请输入要找的图书名字\n");
    scanf("%s",name);
    printf("寻找中...请等待...\n");
    while (head->next && strcmp(head->next->name, name) != 0)
    {
        head = head->next;
    }
    if (head->next == NULL)
        printf("查无此书\n");
    else
        printf("成功查询\n");
    system("pause");
    return head;
}
//删除某本书
void deleteBook(pBook head)
{
    printf("开始查找要删除的图书\n");
    pBook book=foundBook(head);
    if ((book->next))
    {
        pBook temp = book->next;
        book->next = temp->next;
        free(temp);
        printf("删除图书成功\n");
        system("pause");
    }
    else
    {
        printf("没有找到图书,无法删除\n");
        system("pause");
        return;
    }
}



//打印会员借阅图书信息
void printNode(pNode head)
{
    pNode temp = head->next;
    while (temp != nullptr)
    {
        printf("会员编号%d\n", temp->num);
        if (temp->Book->next==NULL)
            printf("    暂无借阅\n");
        else
            printBook(temp->Book->next);
        temp = temp->next;
    }
    system("pause");
}
//添加会员
void addNode(pNode head)
{
    while (head->next)
    {
        head = head->next;
    }
    int index = 0;
    printf("请输入要添加的会员个数\n");
    scanf("%d", &index);
    for (int i = 0; i < index; i++)
    {
        printf("请输入会员编号\n");//暂时用数字代替名字
        pNode temp = (pNode)malloc(sizeof(NODE));
        scanf("%d", &temp->num);
        temp->Book = (pBook)malloc(sizeof(book));
        temp->Book->next = NULL;
        head->next = temp;
        head = head->next;
    }
    head->next = NULL;
    printf("输入完成\n");
    getchar();
    getchar();

}
//查找会员
pNode foundNode(pNode head)
{
    int num = 0;
    printf("请输入要找的会员编号\n");
    scanf("%d", &num);
    printf("寻找中...请等待...\n");
    while (head->next && num != head->next->num)
    {
        head = head->next;
    }
    if (head->next == NULL)
        printf("查无此人\n");
    else
        printf("成功查询\n");
    system("pause");
    return head;
}
//借书,先有会员才可以借书
void borrowBook(pBook book, pNode node)
{
    printf("开始查找要借阅的会员\n");
    pNode nodeTemp = foundNode(node);
    if ((nodeTemp->next)!=NULL)
    {
        nodeTemp = nodeTemp->next;//因为返回的是前一个位置
        pBook nodeBook = nodeTemp->Book;
        if (nodeTemp->Book->next)//找到这个人借的最后一本书的位置的下一个位置(尾插法)
            while (nodeBook->next)
                nodeBook = nodeBook->next;
        pBook bookIndex = foundBook(book);//借走的图书的位置
        if ((bookIndex->next) != NULL)
        {
            pBook temp = bookIndex->next;
            bookIndex->next = temp->next;
            nodeBook->next = temp;
            temp->next = NULL;
            printf("图书借阅成功\n");
        }
        else
            printf("图书借阅失败\n");
        system("pause");
    }
    else
        return;
}
//归还图书
void ReturnBook(pBook book, pNode node)
{
    printf("开始查找要归还图书的会员\n");
    pNode nodeTemp = foundNode(node);
    if ((nodeTemp->next) != NULL)
    {
        nodeTemp = nodeTemp->next;//定位会员
        pBook bookIndex = foundBook(nodeTemp->Book);//读取所要归还图书的位置
        if ((bookIndex->next) != NULL)//用头插法插入图书管理系统
        {
            pBook temp = bookIndex->next;//要归还的图书地址
            bookIndex->next = temp->next;

            temp->next = book->next;
            book->next = temp;
            printf("图书归还成功\n");
        }
        else
            printf("图书归还失败\n");
    }
    else
        return;
}
//删除会员
void deleteNode(pBook book, pNode node)
{
    printf("开始查找要删除的会员\n");
    pNode nodeTemp = foundNode(node);
    pNode temp = nodeTemp->next;
    nodeTemp->next = temp->next;

    while (book->next)
        book = book->next;
    book->next = temp->Book->next;
    free(temp);
}
//删除所有申请空间
void deleteALL(pBook book, pNode node)
{
    while (book->next)
    {
        pBook temp = book->next;
        book->next = temp->next;
        free(temp);
        printf("删除图书成功\n");
    }


    while (node->next)
    {
        while (node->next->Book)
        {
            pBook temp = node->next->Book;
            node->next->Book = temp->next;
            free(temp);
        }
        pNode temp = node->next;
        node->next = temp->next;
        free(temp);
    }
}
void memu()
{
    printf("  ==================图书管理系统====================\n");
    printf(" |图书管理:                                        |\n");
    printf(" |              1.添加图书;                        |\n");
    printf(" |              2.查找图书;                        |\n");
    printf(" |              3.删除图书;                        |\n");
    printf(" |              7.保存图书;                        |\n");
    printf(" |              8.读取书库;                        |\n");
    printf(" |              9.显示所有图书;                    |\n");
    printf(" |会员管理:                                        |\n");
    printf(" |              4.新增会员;                        |\n");
    printf(" |              5.查找会员;                        |\n");
    printf(" |              6.借书;                            |\n");
    printf(" |              66.还书;                           |\n");
    printf(" |              10.显示会员借阅情况;               |\n");
    printf(" |              33.删除会员;                       |\n");
    printf(" |              0.清空所有信息,并退出;            |\n");
    printf("  ===========(请输入相应数字执行其功能)===========\n");
}
void main()
{
    pBook book = (pBook)malloc(sizeof(BOOK));
    book->next = NULL;
    pNode node = (pNode)malloc(sizeof(NODE));
    node->Book = NULL;
    node->next = NULL;
    while (1)
    {
        int index = 0;
        system("cls");
        memu();
        scanf("%d",&index);
        switch (index)
        {
            //图书管理
        case 1:addBook(book);
            break;
        case 2:foundBook(book);
            break;
        case 3:deleteBook(book);
            break;
        case 7:saveBook(book);
            break;
        case 8:readBook(book);
            break;
        case 9:printBook(book->next);
            break;

            //会员管理
        case 4:addNode(node);
            break;
        case 5:foundNode(node);
            break;
        case 6:borrowBook(book, node);
            break;
        case 66:ReturnBook(book, node);
            break;
        case 10:printNode(node);
            break;
        case 33:deleteNode(book, node);
            break;
        
        case 0:deleteALL(book, node); free(book); free(node);exit(0);
            break;
        default:
            break;
        }
    }

}