学校数据结构的课程实验之一。

用到的数据结构:B-树

基本功能:对虚拟书库的图书进行查看、增加、删除、修改。

主函数:

 1 #include <iostream>
 2 #include "Library.h"
 3 using namespace std;
 4 int main()
 5 {
 6     Library myLib=Library("books.txt");
 7     char choice='y';
 8     while(choice=='y')
 9     {
10         cout << "请选择操作"<<endl;
11         cout << "--------------------------------" << endl;
12         cout << "1----新书入库" << endl;
13         cout << "2----查看库存" << endl;
14         cout << "3----借阅" << endl;
15         cout << "4----归还" << endl;
16         cout << "5----删除旧书" << endl;
17         cout << "6----修改图书信息" << endl;
18         cout << "--------------------------------" << endl;
19         int option;
20         cin >> option;
21         switch (option)
22         {
23         case 1: myLib.add(); break;
24         case 2: myLib.display(); break;
25         case 3: myLib.lend(); break;
26         case 4: myLib.back(); break;
27         case 5: myLib.remove(); break;
28         case 6: myLib.change(); break;
29         }
30         cout << "继续吗?[y/n]";
31         cin >> choice;
32     }
33     cout << "是否保存修改?[y/n]";
34     cin >> choice;
35     if (choice == 'y')
36         myLib.save("books.txt");//需要保存时保存文件
37     return 0;
38 }

图书馆类:

  1 #include <fstream>
  2 #include <string>
  3 #include "B_tree.h"
  4 using namespace std;
  5 
  6 struct Book
  7 {
  8     int number;
  9     string name;
 10     string introduction;
 11     unsigned left;
 12     Book(){}
 13     Book(int num) :number(num), name(""), introduction(""), left(0){}//只有编号的初始化
 14     Book(int num, string nam,string intro, unsigned lef)//完整初始化
 15         :number(num),name(nam),introduction(intro),left(lef){}
 16     void print()//显示信息
 17     {
 18         cout << "-------------------------------" << endl;
 19         cout << "这本书的信息如下:" << endl;
 20         cout << "编号: " << number << endl;
 21         cout << "书名: " << name << endl;
 22         cout << "简介: " << introduction << endl;
 23         cout << "剩余数量: " << left << endl;
 24         cout << "-------------------------------" << endl;
 25     }
 26     bool operator==(const Book &b) const//重载关系运算符
 27     {
 28         if(this->number == b.number) return true;//编号等即命中
 29         else return false;
 30     }
 31     bool operator<(const Book &b) const
 32     {
 33         if (this->number < b.number) return true;
 34         else return false;
 35     }
 36     bool operator>(const Book &b) const
 37     {
 38         if (this->number > b.number) return true;
 39         else return false;
 40     }
 41 };
 42 ofstream outFile;//输出流
 43 
 44 class Library
 45 {
 46 private:
 47     B_tree<Book,3> books;
 48     unsigned total;
 49     static void readBook(Book &aBook)//写一本书的内容(一定要是静态的) 
 50     {
 51         outFile<<aBook.number<<endl;
 52         outFile<<aBook.name<<endl;
 53         outFile<<aBook.introduction<<endl;
 54         outFile << aBook.left << endl;
 55     }
 56     void readFile(const char filename[20])//读文件
 57     {
 58         total = 0;
 59         ifstream inFile;
 60         inFile.open(filename);
 61         char trying;
 62         while(inFile.is_open() && !inFile.eof())
 63         {
 64             //先试探是否为结束符
 65             inFile >> trying;
 66             if (trying == '#') break;
 67             else
 68             {
 69                 inFile.putback(trying);
 70                 int number;
 71                 inFile>>number;
 72                 string name;
 73                 inFile>>name;
 74                 string introduction;
 75                 inFile>>introduction;
 76                 unsigned left;
 77                 inFile>>left;
 78                 Book aBook=Book(number,name,introduction,left);
 79                 aBook.print();//显示这本书的信息
 80                 books.insert(aBook);
 81                 total+=left;
 82             }            
 83         }
 84         cout << "库存共有图书" << total << ""<<endl;
 85         inFile.close();
 86     }
 87     void writeFile(const char filename[20])//写文件
 88     {
 89         outFile.open(filename);
 90         books.traverse(readBook);
 91         outFile << '#';//此处必须有一个结束标识符
 92         outFile.close();
 93     }
 94     Book search(int num)//以编号为依据进行查找
 95     {
 96         Book se_book(num);
 97         books.search_tree(se_book);
 98         return se_book;
 99     }
100     static void print(Book &aBook)//显示信息(必须是静态的)
101     {
102         cout << "-------------------------------" << endl;
103         cout << "这本书的信息如下:" << endl;
104         cout << "编号: " << aBook.number << endl;
105         cout << "书名: " << aBook.name << endl;
106         cout << "简介: " << aBook.introduction << endl;
107         cout << "剩余数量: " << aBook.left << endl;
108         cout << "-------------------------------" << endl;
109     }
110 public:
111     Library(const char filename[20])
112     {
113         cout << "这是现在的库存信息:" << endl;
114         readFile(filename);
115     }
116     void add()//增加图书
117     {
118         cout << "请输入图书信息(编号 书名 简介 数量)" << endl;
119         int num;
120         string name;
121         string introduction;
122         unsigned left;
123         cin >> num >> name >> introduction >> left;
124         Book new_book = Book(num, name, introduction, left);
125         books.insert(new_book);
126         cout << "这本书已入库,信息如下:" << endl;
127         new_book.print();
128         total += left;
129     }
130     void display()//查看库存
131     {
132         cout << "这是现在的库存信息:" << endl;
133         books.traverse(print);
134         cout << "库存共有图书" << total << "" << endl;
135     }
136     void remove()//删除
137     {
138         cout << "请输入要删除的图书编号:";
139         int num;
140         cin >> num;
141         Book &old_book =search(num);//通过编号找到这本书的记录
142         cout << "您即将删除这本书的所有信息:" << endl;
143         old_book.print();
144         cout << "确定要删除吗?[y/n]";
145         char choice;
146         cin >> choice;
147         if (choice == 'y')
148         {
149             books.remove(old_book);//删除这本书的记录
150             cout << "编号为" << num << "的书已成功从库中删除" << endl;
151             total--;
152         }        
153     }
154     void lend()//借出
155     {
156         cout << "请输入要借出的图书编号:";
157         int num;
158         cin >> num;
159         Book &old_book = search(num);//通过编号找到这本书的记录
160         old_book.left--;
161         cout << "编号为" << num << "的图书已借出1本,下面是这本书的现存信息:" << endl;
162         old_book.print();
163         total--;
164     }
165     void change()//修改(先删除再添加)
166     {
167         cout << "请输入要修改的图书编号:";
168         int num;
169         cin >> num;
170         Book &old_book = search(num);
171         cout << "这是这本书的当前信息:" << endl;
172         old_book.print();//显示这本书之前的信息
173         books.remove(old_book);
174         cout << "请输入修改后的图书信息(编号 书名 简介 数量)" << endl;
175         string name;
176         string introduction;
177         unsigned left;
178         cin >> num >> name >> introduction >> left;
179         Book new_book = Book(num, name, introduction, left);
180         books.insert(new_book);
181         cout << "这本书的信息已修改为:" << endl;
182         new_book.print();
183     }
184     void back()//归还
185     {
186         cout << "请输入要归还的图书编号:";
187         int num;
188         cin >> num;
189         Book &old_book = search(num);//通过编号找到这本书的记录
190         old_book.left++;
191         cout << "编号为" << num << "的图书已归还,下面是这本书的现存信息:" << endl;
192         old_book.print();
193         total++;
194     }
195     void save(const char filename[20])
196     {
197         writeFile(filename);
198     }
199 };
Library.h

相关文章: