数据结构--简易图书管理模拟系统

数据结构–简易图书管理模拟系统

【实验目的】

插入、查找和删除等数据操作在实际应用中非常普遍,通过设计和实现一个简易的图书管理系统,进一步提高学生对插入、查找和删除等操作的理解和应用能力。帮助学生理解和掌握线性表和平衡二叉树等数据结构的基本操作和实现方法,加强学生综合应用数据结构知识解决实际问题的水平和能力。

【实验内容】

1、问题描述:一个简易图书管理的基本业务活动包括:对新购入一种书的采编入库、图书的借阅和归还等。
2、基本要求:
(1)每种书的登记内容至少包括书号、书名、著者、现存量和总库存量等五项。
(2)作为演示系统,不必使用文件存储书籍数据和借阅登记信息。我们要求各种书的数据用二叉排序树来存储,借阅登记信息采用顺序表—链表来存储。顺序表存储借阅者信息,链表存储借阅者所借的各种书籍信息。借阅登记信息的存储结构如下示意:
在这里插入图片描述
需要实现的三种主要功能定义如下:
①采编入库:新购入一种书,经分类和确定书号之后登记到图书帐目中去。如果这种书在帐中已有,则只将该书的总库存量增加。
②借阅:如果一种书的现存量大于零,则借出一本,登记借阅者的图书证号和归还期限。
③归还:注销对借阅者的登记,改变该书的现存量(如果借阅者归还所有的书,则注销该借阅者的信息)。
3、测试数据:
入库书号:ISBN 7-302-02368-9,ISBN 978-7-115-16985-3/TP,ISBN 978-7-302-03314-1,ISBN7-115-10563-4/TP·3043,ISBN 978-7-121-07479-0,ISBN 978-7-115-18809-0/TP,ISBN 978-7-04-024246-1,ISBN 7-111-12886-9,ISBN 978-7-115-19601-9/TP,ISBN 7-900183-01-9。
借书证号为081716的借阅者,先借阅10种图书各一本,后归还图书ISBN 7-302-02368-9和ISBN 978-7-121-07479-0。
借书证号为081710的借阅者,先借阅图书ISBN 978-7-121-07479-0和ISBN 978-7-302-03314-110各一本,后归还图书ISBN 978-7-121-07479-0。
其余数据可自行设计。
4、实现提示:
(1)各种图书按登记的先后顺序入库(利用平衡二叉树实现动态查找表的插入),书号为图书的关键字。初始时,平衡二叉树为空树。
(2)借阅者借阅图书时,先检查借阅者有无超期未归还图书,如有,则不能借阅,如无,利用平衡二叉树实现动态查找表的查找,登记借阅信息。注意,按规定,同一种书不能重复借阅。
(3)清除库存(选作)时,将该图书从平衡二叉树中删除。
5、选作内容:
(1)实现图书管理的清除库存操作:某种书已无保留价值,将它从图书帐目中注销。即,要实现平衡二叉树的删除操作。
(2)实现图书管理的显示操作:每次插入或删除平衡二叉树的一个结点之后,更新平衡二叉树的显示。平衡二叉树的显示可采用《数据结构题集》6.69题要求的凹入表形式,也可以采用图形界面画出树形。
(3)图书管理模拟系统的其它功能实现。

【实验数据】

#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<string>
#include<cmath>
#include<vector>
#include<fstream>

using namespace std;

ifstream fin("图书管理系统测试数据2.txt");

typedef struct Book
{
    string name;
    string bid;
    string writer;
    string date;
    Book *lc, *rc, *parent, *next;
    int now;                               //struct stu;
    int summary;                          //stu *visit;
    int key;
} B_list, *book;

typedef struct stu
{
    string name;
    book bl[26];            //存自己借的书
    string sid;
    book first;          //用于访问书结构体的指针
    int booknum;

} S_list, *student;

typedef struct graph_s         //学生查书的图
{
    S_list sl[26];             //已经创建了26个学生的数组
    int s_num;
    int b_num;
    //book bl[26];                //书本采用两种方式存储,一种树状结构,一种链式结构,链式结构用于查找.
} g_s_list;

book original;
void find_index(book h,book hh, g_s_list w);            //先声明一下

void digui_create(book &bo, g_s_list &w)
{
    string ch;
    cout << "请输入书名(%代表没有这种书)" << endl;
    cin >> ch;
    if (ch == "%")          //输入的时候最好一次输入一个换行,别加空格
    {
        bo = NULL;
    }
    if (bo)
    {
        bo = new Book;

        bo->name = ch;
        digui_create(bo->lc, w);
        digui_create(bo->rc, w);
    }
}

//book hh=bo;              //要把这个指向二叉树的根节点

void insert(book hh,g_s_list w)
{
    book h;
    int n;
    cout << "请输入要添加的书籍种类数:" << endl;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        h = new Book;
        cout << "请输入第" << i + 1 << "本书的书名,书号以及库存和key:" << endl;
        cin >> h->name >> h->bid >> h->summary>>h->key;
        h->lc = NULL;
        h->rc = NULL;            //要把左右孩子置空
        find_index(h,hh, w);
    }
}

book temp;
int x;
void find_index(book h,book bo,g_s_list w)                 //用于找插入位置
{
    if (bo)
    {
        if (h->key < bo->key)
        {
            x = 0;
        }
        if (h->key > bo->key)
        {
            x = 1;
        }
        if (h->key == bo->key)
        {
            x = 2;
        }
        if (x==0/*h->key < bo->key*/)
        {
            temp = bo;
            find_index(h, bo->lc, w);
        }
        //h = temp->lc;
        if (x==1/*h->key >= bo->key*/)
        {
            temp = bo;
            find_index(h, bo->rc, w);
        }
        if (x == 2)
        {
            temp = bo;
            temp->summary += h->summary;
            x = 3;               //要把状态码x置为一个不存在的状态
            return;
        }
        //h = temp->rc;
    }
    else
    {
        if (x == 0)
        {
            temp->lc = h;    //如上
            x = 3;
        }
        else if (x == 1)
        {
            temp->rc = h;    //如上
            x = 3;
        }
        //else
        //{
        //	temp->summary += h->summary;
        //}
    }
}

void preorder(book bo)
{
    if (bo)
    {
        cout << "请输入  《" << bo->name << "》  书的书号,作者,key,库存:" << endl;   //先序遍历创建的书的树
        cin >> bo->bid >> bo->writer >> bo->key >> bo->summary;
        preorder(bo->lc);
        preorder(bo->rc);
    }
}
void preorder_x(book bo)              //遍历访问插入新书后的书树
{
    if (bo)
    {
        cout << "书名为:  《" << bo->name << "》 key为:  " << bo->key << "  库存为:  " << bo->summary << endl;
        preorder_x(bo->lc);
        preorder_x(bo->rc);
    }
}

book find_book;
book t;
void find(book bo, string book_id)             //用于找书
{
    if (bo)
    {
        if (bo&&bo->bid == book_id)
        {
            find_book = bo;
            // tem = bo;
            cout << "书名为:\n" <<"《"<< find_book->name<<"》";
            cout << endl;
            cout << "库存为:\n" << find_book->summary << endl;
            cout << "请输入借入时间:" << endl;
            cin >> find_book->date;
        }
        find(bo->lc, book_id);
        find(bo->rc, book_id);
    }
}

void create(book bo, g_s_list &w, S_list &s)            //建图
{
    book t;                              //用于保存下找到的上一本书
    cout << "请输入有几个学生:" << endl;
    cin >> w.s_num;
    w.b_num = 0;
    book q;
    int x = 0;
    string book_id;
    for (int i = 0; i < w.s_num; i++)
    {
        //s=new stu;
        cout << "请输入第" << i + 1 << "个学生的姓名,学号和借书量:" << endl;
        cin >> w.sl[i].name >> w.sl[i].sid >> w.sl[i].booknum;
        q = bo;                         //把q指向创建书本的树的根节点
        cout << "请输入第" << i + 1 << "个学生的第 1 本书的书号:" << endl;
        cin >> book_id;
        //cout << "库存量为:" << find(bo, book_id);

        find(bo, book_id);
        w.sl[i].first = find_book;
        t = find_book;                            //保存下来
        w.sl[i].bl[x] = find_book;                   //把第一本书保存下来,便于后面的学生查找自己借的书
        x++;
        cout << endl;
        for (int j = 0; j < w.sl[i].booknum - 1; j++)
        {
            //find_book->next = new Book;
            //find_book = find_book->next;
            cout << "请输入第" << i + 1 << "个学生的第 " << j + 2 << " 本书的书号:" << endl;
            cin >> book_id;
            find(bo, book_id);
            //t->next = find_book;
            w.sl[i].bl[x] = find_book;
            x++;
            //t = find_book;
            cout << endl;
            //cout << "库存量为:" << find(bo, book_id);
        }
        x = 0;
    }

}

void lend(book bo, g_s_list w, S_list s)
{
    //t = original;                    //t用于遍历自己借的书
    string sq;
    cout << "请输入学号:" << endl;
    cin >> sq;
    for (int i = 0; i < w.s_num; i++)
    {
        t = w.sl[i].bl[i];
        if (sq == w.sl[i].sid)
        {
            cout << "欢迎" << " " << w.sl[i].name << "\n" << " \n" << "你借的书有:\n";
            //w.sl[i].bl[i]->name << " 库存为:" << w.sl[i].bl[i]->summary <<" 借入时间为:"<<w.sl[i].bl[i]->date<< endl;
            //t = t->next;
            for (int j = 0; j < w.sl[i].booknum; j++)
            {
                //t=t->next;
                //cout << t->name << " " << "库存为:"<<t->summary<<" 借入时间为:"<<t->date << endl;
                cout << "《"<<w.sl[i].bl[j]->name << "》 " << "库存为:" << w.sl[i].bl[j]->summary << " 借入时间为:" << w.sl[i].bl[j]->date << endl;

            }

            break;
        }
        if (i == w.s_num - 1)
            cout << "查无此人" << endl;
    }
}

int main()
{

    book hwq;
    g_s_list wwq;
    S_list s;
    digui_create(hwq, wwq);
    preorder(hwq);
    insert( hwq, wwq);
    preorder_x(hwq);
    create(hwq, wwq, s);
    lend(hwq, wwq, s);
    system("pause");
    return 0;
}



【实验结果】

在这里插入图片描述

发布了27 篇原创文章 · 获赞 4 · 访问量 1251

猜你喜欢

转载自blog.csdn.net/WX_timi/article/details/104207736