#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;
}
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/419933032626172.png)