您的位置:首页 > 编程语言 > C语言/C++

程序开发===》图书馆的借阅系统------开发源代码---雏形设计——经典C++的完全应用

2019-06-24 23:20 1321 查看
版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons


初步学习了C++这门面向对象的语言后,我们学以致用,利用最简朴的VC2010编译环境在源文件出进行代码开发,收获颇丰。

图书馆的借阅系统

建立两个类:

  • 书籍类
  • 图书馆类

书籍类:

class Book//书籍类
{
public:
Book()//无参构造函数,对对象的成员进行初始化。
{
borrow_flag = false;
}

Book(string name, string num, string auther):name(name), num(num), auther(auther)//有参构造函数
{
borrow_flag = false;
}

void setReader(string reader, int lcn, string data); //设置读者
void setInfo(string name, string num, string auther); //设置书籍信息

//书籍信息:
string getName()
{
return name;
}
string getNum()
{
return num;
}
string getAuther()
{
return auther;
}
//读者信息:
bool getBorrow_flag()
{
return borrow_flag;
}
string getReader()
{
return reader;
}
int getLcn()
{
return lcn;
}
string getData()
{
return data;
}

bool isBorrow(){ return borrow_flag; }        //判断书籍是否借出
void setBorrow_flag(bool b){ borrow_flag = b; }//返还书籍
void showInfo();        //显示数据信息

private:
string name;  //书名
string num;   //编号(唯一标示)
string auther; //作者

bool borrow_flag;
string reader; //读者
int lcn;       //借书证号
string data;   //借书日期
};

图书馆类:

class Library//图书馆类
{
public:
Library(){ currentNum = 0; brrowNum = 0; }
void addBook();                //向图书馆里加书籍
void addBook(string name, string num, string auther);//有参构造函数
void deleteBook();   //删除无用书籍
void brrowBook();   //借书,之前先判断书籍是否存在
void returnBook();   //还书
void getReader();  //查询某编号的书是谁借了
int indexOfNum(string num); //根据编号得到书在数组中的下标
vector<Book> getBooks()//等于声明一个Book类的类getBook();
{
return books;
}
friend void read();//友元函数访问,类外定义read()函数
void showInfo();
int getTotalBooks()//书籍总和
{
return currentNum + brrowNum;
}

private:
vector<Book> books;//所有书籍
map<string, int> readers;  //存储读者及其所借的书籍数目
int currentNum;   //库存书籍数目(不包括借出的)
int brrowNum;     //借出书籍数目
};

读者及书籍信息的设置:

void Book::setReader(string reader, int lcn, string data)//设置读者
{
borrow_flag = true;
this->reader.assign(reader);
this->lcn = lcn;
this->data.assign(data);
}
void Book::setInfo(string name, string num, string auther)//设置书籍信息
{
this->name.assign(name);
this->num.assign(num);
this->auther.assign(auther);
}

对图书的添、删、借、还、查:

void Library::addBook()//向图书馆里增添书籍
{
Book b;
int temp;
string name, num, auther;
cout<<"  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n";
cout<<"  ┃*****************************增加界面*****************************┃\n";
cout<<"  ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n\n";
do{
cout << "输入书籍名称,编号,作者:";
cin >> name >> num >> auther;
b.setInfo(name, num, auther);//设置图书信息
if (indexOfNum(num) == -1){
books.push_back(b);//栈压入数据
currentNum++;
cout << "\n添加成功。" << endl;
cout << "输入1继续增加:";
cin >> temp;
}
else{
cout << "已存在该编号的书籍,添加失败。" << endl;
cout << "输入1继续重新增加:";
cin >> temp;
}
} while (temp==1);
system("pause");
system("cls");
}

void Library::addBook(string name, string num, string auther)//添上书籍的信息
{
Book b;
b.setInfo(name, num, auther);//设置图书信息
books.push_back(b);//以栈压入
}

void Library::deleteBook()//删除书籍
{
int index,temp;
string num;
cout << "  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n";
cout << "  ┃*****************************删除界面*****************************┃\n";
cout << "  ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n\n";
do{
cout << "输入要删除的书籍的编号:";
cin >> num;
index = indexOfNum(num);//赋值于index编号
if (index != -1){
if (!books[index].getBorrow_flag()){
cout << "删除的书籍的信息:\n";
books[index].showInfo();//显示图书编号
books.erase(books.begin() + index);//清除数组空间
currentNum--;
cout << "删除成功。" << endl;
cout << "输入1继续继续删除:";
cin >> temp;
}
else{
cout << "删除失败!书籍已经被借出。" << endl;
cout << "输入1继续继续删除:";
cin >> temp;
}
}
else
{
cout << "删除失败。未找到编号为" << num << "的书籍。\n";
cout << "输入1继续继续删除:";
cin >> temp;
}
} while (temp==1);
system("pause");
system("cls");
}

void Library::brrowBook()//图书的借阅
{
string num;
int index;
cout << "  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n";
cout << "  ┃*****************************借阅界面*****************************┃\n";
cout << "  ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n\n";
cout << "输入要借阅的书籍的编号:";
cin >> num;
index = indexOfNum(num);
if (index != -1){
if (books[index].isBorrow()){
cout << "借阅失败,书籍以及被借出。\n";
system("pause");
system("cls");
}
else
{
cout << "要借的书籍的信息:\n";
books[index].showInfo();
string reader, data;
int lcn;
cout << "输入读者姓名,借书证号,借书日期:";
cin >> reader >> lcn >> data;
if (readers[reader] != 2){
books[index].setReader(reader, lcn, data);
cout << "借书完成。\n";
currentNum--;
brrowNum++;
readers[reader]++;
system("pause");
system("cls");
}
else
{
cout << "借书失败,该读者以借超过两本书籍。\n";
system("pause");
system("cls");
}
}
}
else
{
cout << "借书失败。未找到编号为" << num << "的书籍.\n";
system("pause");
system("cls");
}

}
void Library::returnBook()//返还图书
{
string num;
cout << "  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n";
cout << "  ┃*****************************还书界面*****************************┃\n";
cout << "  ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n\n";
cout << "输入要还的书籍的编号:";
cin >> num;
int index;
index = indexOfNum(num);
if (index != -1)
{
cout << "要还的书籍的信息为:\n";
books[index].showInfo();
books[index].setBorrow_flag(false);
readers[books[index].getReader()]--;//减1标记
cout << "还书成功。\n";
system("pause");
system("cls");
}
else
{
cout << "还书失败,请检查书籍编号是否输入错误!\n";
system("pause");
system("cls");
}
}
void Library::getReader()//查找书籍
{
string num;
cout << "  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n";
cout << "  ┃*****************************查询界面*****************************┃\n";
cout << "  ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n\n";
cout << "输入要查找的书籍编号:";
cin >> num;
int index;
index = indexOfNum(num);
if (index != -1)
{
if (books[index].getBorrow_flag())
cout << "读者为:" << books[index].getReader() << endl;
else{
cout << "无读者。" << endl;
}
system("pause");
system("cls");
}
else
{
cout << "查询失败,请检查书籍编号是否输入错误!\n";
system("pause");
system("cls");
}
}Library l;

文件操作:

void read()//文件的读入
{
int i, temp;
ifstream my1("num.txt");//对文件的操作
my1 >> temp;
ifstream my("book.txt");
for (i = 0; i<temp; i++)
{
string name, num, auther, reader, data;
int lcn;
bool borrow_flag;
my >> name >> num >> auther >> borrow_flag;
l.addBook(name, num, auther);
if (borrow_flag)
{
my >> reader >> lcn >> data;
l.books[i].setReader(reader, lcn, data);
}
}
}

void save()//文件的输出
{
ofstream my("book.txt");
int i;
for (i = 0; i<l.getBooks().size(); i++){
Book b;
b = l.getBooks()[i];
if (b.getBorrow_flag())
my << b.getName() << "\t" << b.getNum() << "\t" << b.getAuther() << "\t" << b.getBorrow_flag()
<< "\t" << b.getReader() << "\t" << b.getLcn() << "\t" << b.getData() << "\n";
else
my << b.getName() << "\t" << b.getNum() << "\t" << b.getAuther() << "\t" << b.getBorrow_flag() << "\n";
}
ofstream my1("num.txt");
my1 << l.getBooks().size();
}

显示信息:

void Book::showInfo()//显示数据信息
{
cout<< "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓" << endl;
cout<< "┃书籍名称:" << setiosflags(ios_base::left)<<setw(56)<<name <<"┃"<< endl//默认左对齐,全局设置,sew()控制输出长度。
<< "┃书籍编号:" << setw(56) << num << "┃" << endl
<< "┃书籍作者:" << setw(56) << auther << "┃" << endl;
if (borrow_flag)
{
cout <<"┃书籍被借出。                                                      ┃\n"
<< "┃读者姓名:" << setw(56) << reader << "┃" << endl
<< "┃借书证号:" << setw(56) << lcn << "┃" << endl
<< "┃借书日期:" << setw(56) << data << "┃" << endl
<< "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n";
}
else{
cout << "┃书籍未被借出。                                                    ┃\n";
cout << "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n";
}
void Library::showInfo()//显示每本书的信息
{
cout << "  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n";
cout << "  ┃***************************所有图书信息***************************┃\n";
cout << "  ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n\n";
for (int i = 0; i<books.size(); i++)
{
cout << "第"<<i+1<<"本书籍的信息。" << endl;
books[i].showInfo();//显示第i+1本书。
}
system("pause");
system("cls");
}
void menu()//显示菜单
{
int temp;
cout << "■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\n";
cout << "■                                                                          ■\n";
cout << "■                                                                          ■\n";
cout << "■             ┏━━━━━━━━━━━━━━━━━━━━━━━┓           ■\n";
cout << "■             ┃                                              ┃           ■\n";
cout << "■             ┃       制作者:yyx                             ┃           ■\n";
cout << "■             ┃                                              ┃           ■\n";
cout << "■             ┃       制作日期:2019.06                       ┃           ■\n";
cout << "■             ┃                                              ┃           ■\n";
cout << "■             ┃       学校:sq university                     ┃           ■\n";
cout << "■             ┃                                              ┃           ■\n";
cout << "■             ┃       班级:软嵌1803                          ┃           ■\n";
cout << "■             ┃                                              ┃           ■\n";
cout << "■             ┃       版本:图书馆管理系统                     ┃           ■\n";
cout << "■             ┃                                              ┃           ■\n";
cout << "■             ┃       Weclome to use it!                     ┃           ■\n";
cout << "■             ┃                                              ┃           ■\n";
cout << "■             ┃           All Ringhts Reserved!              ┃           ■\n";
cout << "■             ┃                                              ┃           ■\n";
cout << "■             ┗━━━━━━━━━━━━━━━━━━━━━━━┛           ■\n";
cout << "■                                                                          ■\n";
cout << "■                                                                          ■\n";
cout << "■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\n";
Sleep(2000);//执行挂起一段时间,也就是等待一段时间再继续执行。
system("cls");
while (1)
{
cout << "■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\n";
cout << "■___________________________ 图书馆管理系统______________________________■\n";
cout << "■                  ┏━━━━━━━━━━━━━━┓                      ■\n";
cout << "■                  ┃ ●[0]退出系统。            ┃                      ■\n";
cout << "■                  ┣━━━━━━━━━━━━━━┫                      ■\n";
cout << "■                  ┃ ●[1]增加图书。            ┃                      ■\n";
cout << "■                  ┣━━━━━━━━━━━━━━┫                      ■\n";
cout << "■                  ┃ ●[2]删除图书。            ┃                      ■\n";
cout << "■                  ┣━━━━━━━━━━━━━━┫                      ■\n";
cout << "■                  ┃ ●[3]借阅图书。            ┃                      ■\n";
cout << "■                  ┣━━━━━━━━━━━━━━┫                      ■\n";
cout << "■                  ┃ ●[4]归还图书。            ┃                      ■\n";
cout << "■                  ┣━━━━━━━━━━━━━━┫                      ■\n";
cout << "■                  ┃ ●[5]显示图书信息。        ┃                      ■\n";
cout << "■                  ┣━━━━━━━━━━━━━━┫                      ■\n";
cout << "■                  ┃ ●[6]查询图书。            ┃                      ■\n";
cout << "■                  ┗━━━━━━━━━━━━━━┛                      ■\n";
cout << "■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\n";
cout << "输入要进行的操作:" ;
cin >> temp;
switch (temp){
case 1:
system("cls");
l.addBook(); save();//将数据写入一个临时文件。
break;
case 2:system("cls");
l.deleteBook(); save();
break;
case 3:system("cls");
l.brrowBook(); save();
break;
case 4:system("cls");
l.returnBook(); save();
break;
case 5:system("cls");
l.showInfo();
break;
case 6:system("cls");
l.getReader();
break;
case 0:
save();
exit(1);
break;
default:
cout << "输入错误!" << endl;
system("cls");
}
}
}

------》源代码:

#include<iostream>
#include<string>
#include<vector>
/*vector是一种顺序容器,事实上和数组差不多,但它比数组更优越。
一般来说数组不能动态拓展,因此在程序运行的时候不是浪费内存,就是造成越界。
而vector正好弥补了这个缺陷,它的特征是相当于可分配拓展的数组,它的随机访问快,在中间插入和删除慢,但在末端插入和删除快。
用.at()访问的话,也可以做越界检查。*/
#include<map>//自动建立key - value的对应。key 和 value可以是任意你需要的类型。
#include<fstream>
/*在C++中,对文件的操作是通过stream的子类fstream(filestream)来实现的
所以,要用这种方式操作文件,就必须加入头文件fstream.h*/
#include<windows.h>//sleep()函数的头文件
#include<iomanip>//参数的格式化输入/输出,sew()函数的头文件。
using namespace std;

class Book//书籍类
{
public:
Book()//无参构造函数,对对象的成员进行初始化。
{
borrow_flag = false;
}

Book(string name, string num, string auther):name(name), num(num), auther(auther)//有参构造函数
{
borrow_flag = false;
}

void setReader(string reader, int lcn, string data); //设置读者
void setInfo(string name, string num, string auther); //设置书籍信息

//书籍信息:
string getName()
{
return name;
}
string getNum()
{
return num;
}
string getAuther()
{
return auther;
}
//读者信息:
bool getBorrow_flag()
{
return borrow_flag;
}
string getReader()
{
return reader;
}
int getLcn()
{
return lcn;
}
string getData()
{
return data;
}

bool isBorrow(){ return borrow_flag; }        //判断书籍是否借出
void setBorrow_flag(bool b){ borrow_flag = b; }//返还书籍
void showInfo();        //显示数据信息

private:
string name;  //书名
string num;   //编号(唯一标示)
string auther; //作者

bool borrow_flag;
string reader; //读者
int lcn;       //借书证号
string data;   //借书日期
};
void Book::setReader(string reader, int lcn, string data)//设置读者
{
borrow_flag = true;
this->reader.assign(reader);
this->lcn = lcn;
this->data.assign(data);
}
void Book::setInfo(string name, string num, string auther)//设置书籍信息
{
this->name.assign(name);
this->num.assign(num);
this->auther.assign(auther);
}void Book::showInfo()//显示数据信息
{
cout<< "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓" << endl;
cout<< "┃书籍名称:" << setiosflags(ios_base::left)<<setw(56)<<name <<"┃"<< endl//默认左对齐,全局设置,sew()控制输出长度。
<< "┃书籍编号:" << setw(56) << num << "┃" << endl
<< "┃书籍作者:" << setw(56) << auther << "┃" << endl;
if (borrow_flag)
{
cout <<"┃书籍被借出。                                                      ┃\n"
<< "┃读者姓名:" << setw(56) << reader << "┃" << endl
<< "┃借书证号:" << setw(56) << lcn << "┃" << endl
<< "┃借书日期:" << setw(56) << data << "┃" << endl
<< "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n";
}
else{
cout << "┃书籍未被借出。                                                    ┃\n";
cout << "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n";
}
}

class Library//图书馆类
{
public:
Library(){ currentNum = 0; brrowNum = 0; }
void addBook();                //向图书馆里加书籍
void addBook(string name, string num, string auther);//有参构造函数
void deleteBook();   //删除无用书籍
void brrowBook();   //借书,之前先判断书籍是否存在
void returnBook();   //还书
void getReader();  //查询某编号的书是谁借了
int indexOfNum(string num); //根据编号得到书在数组中的下标
vector<Book> getBooks()//等于声明一个Book类的类getBook();
{
return books;
}
friend void read();//友元函数访问,类外定义read()函数
void showInfo();
int getTotalBooks()//书籍总和
{
return currentNum + brrowNum;
}

private:
vector<Book> books;//所有书籍
map<string, int> readers;  //存储读者及其所借的书籍数目
int currentNum;   //库存书籍数目(不包括借出的)
int brrowNum;     //借出书籍数目
};
void Library::showInfo()//显示每本书的信息
{
cout << "  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n";
cout << "  ┃***************************所有图书信息***************************┃\n";
cout << "  ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n\n";
for (int i = 0; i<books.size(); i++)
{
cout << "第"<<i+1<<"本书籍的信息。" << endl;
books[i].showInfo();//显示第i+1本书。
}
system("pause");
system("cls");
}

int Library::indexOfNum(string num)//图书编号
{
int i;
for (i = 0; i<books.size(); i++)
{
if (books[i].getNum() == num)
return i;
}
return -1;
}

void Library::addBook()//向图书馆里增添书籍
{
Book b;
int temp;
string name, num, auther;
cout<<"  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n";
cout<<"  ┃*****************************增加界面*****************************┃\n";
cout<<"  ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n\n";
do{
cout << "输入书籍名称,编号,作者:";
cin >> name >> num >> auther;
b.setInfo(name, num, auther);//设置图书信息
if (indexOfNum(num) == -1){
books.push_back(b);//栈压入数据
currentNum++;
cout << "\n添加成功。" << endl;
cout << "输入1继续增加:";
cin >> temp;
}
else{
cout << "已存在该编号的书籍,添加失败。" << endl;
cout << "输入1继续重新增加:";
cin >> temp;
}
} while (temp==1);
system("pause");
system("cls");
}

void Library::addBook(string name, string num, string auther)//添上书籍的信息
{
Book b;
b.setInfo(name, num, auther);//设置图书信息
books.push_back(b);//以栈压入
}

void Library::deleteBook()//删除书籍
{
int index,temp;
string num;
cout << "  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n";
cout << "  ┃*****************************删除界面*****************************┃\n";
cout << "  ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n\n";
do{
cout << "输入要删除的书籍的编号:";
cin >> num;
index = indexOfNum(num);//赋值于index编号
if (index != -1){
if (!books[index].getBorrow_flag()){
cout << "删除的书籍的信息:\n";
books[index].showInfo();//显示图书编号
books.erase(books.begin() + index);//清除数组空间
currentNum--;
cout << "删除成功。" << endl;
cout << "输入1继续继续删除:";
cin >> temp;
}
else{
cout << "删除失败!书籍已经被借出。" << endl;
cout << "输入1继续继续删除:";
cin >> temp;
}
}
else
{
cout << "删除失败。未找到编号为" << num << "的书籍。\n";
cout << "输入1继续继续删除:";
cin >> temp;
}
} while (temp==1);
system("pause");
system("cls");
}

void Library::brrowBook()//图书的借阅
{
string num;
int index;
cout << "  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n";
cout << "  ┃*****************************借阅界面*****************************┃\n";
cout << "  ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n\n";
cout << "输入要借阅的书籍的编号:";
cin >> num;
index = indexOfNum(num);
if (index != -1){
if (books[index].isBorrow()){
cout << "借阅失败,书籍以及被借出。\n";
system("pause");
system("cls");
}
else
{
cout << "要借的书籍的信息:\n";
books[index].showInfo();
string reader, data;
int lcn;
cout << "输入读者姓名,借书证号,借书日期:";
cin >> reader >> lcn >> data;
if (readers[reader] != 2){
books[index].setReader(reader, lcn, data);
cout << "借书完成。\n";
currentNum--;
brrowNum++;
readers[reader]++;
system("pause");
system("cls");
}
else
{
cout << "借书失败,该读者以借超过两本书籍。\n";
system("pause");
system("cls");
}
}
}
else
{
cout << "借书失败。未找到编号为" << num << "的书籍.\n";
system("pause");
system("cls");
}

}
void Library::returnBook()
{
string num;
cout << "  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n";
cout << "  ┃*****************************还书界面*****************************┃\n";
cout << "  ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n\n";
cout << "输入要还的书籍的编号:";
cin >> num;
int index;
index = indexOfNum(num);
if (index != -1)
{
cout << "要还的书籍的信息为:\n";
books[index].showInfo();
books[index].setBorrow_flag(false);
readers[books[index].getReader()]--;//减1标记
cout << "还书成功。\n";
system("pause");
system("cls");
}
else
{
cout << "还书失败,请检查书籍编号是否输入错误!\n";
system("pause");
system("cls");
}
}
void Library::getReader()//查找书籍
{
string num;
cout << "  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n";
cout << "  ┃*****************************查询界面*****************************┃\n";
cout << "  ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n\n";
cout << "输入要查找的书籍编号:";
cin >> num;
int index;
index = indexOfNum(num);
if (index != -1)
{
if (books[index].getBorrow_flag())
cout << "读者为:" << books[index].getReader() << endl;
else{
cout << "无读者。" << endl;
}
system("pause");
system("cls");
}
else
{
cout << "查询失败,请检查书籍编号是否输入错误!\n";
system("pause");
system("cls");
}
}Library l;

void read()//文件的读入
{
int i, temp;
ifstream my1("num.txt");//对文件的操作
my1 >> temp;
ifstream my("book.txt");
for (i = 0; i<temp; i++)
{
string name, num, auther, reader, data;
int lcn;
bool borrow_flag;
my >> name >> num >> auther >> borrow_flag;
l.addBook(name, num, auther);
if (borrow_flag)
{
my >> reader >> lcn >> data;
l.books[i].setReader(reader, lcn, data);
}
}
}

void save()//文件的输出
{
ofstream my("book.txt");
int i;
for (i = 0; i<l.getBooks().size(); i++){
Book b;
b = l.getBooks()[i];
if (b.getBorrow_flag())
my << b.getName() << "\t" << b.getNum() << "\t" << b.getAuther() << "\t" << b.getBorrow_flag()
<< "\t" << b.getReader() << "\t" << b.getLcn() << "\t" << b.getData() << "\n";
else
my << b.getName() << "\t" << b.getNum() << "\t" << b.getAuther() << "\t" << b.getBorrow_flag() << "\n";
}
ofstream my1("num.txt");
my1 << l.getBooks().size();
}
void menu()//显示菜单
{
int temp;
cout << "■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\n";
cout << "■                                                                          ■\n";
cout << "■                                                                          ■\n";
cout << "■             ┏━━━━━━━━━━━━━━━━━━━━━━━┓           ■\n";
cout << "■             ┃                                              ┃           ■\n";
cout << "■             ┃       制作者:yyx                         ┃           ■\n";
cout << "■             ┃                                              ┃           ■\n";
cout << "■             ┃       制作日期:2019.06                      ┃           ■\n";
cout << "■             ┃                                              ┃           ■\n";
cout << "■             ┃       学校:sq university                          ┃           ■\n";
cout << "■             ┃                                              ┃           ■\n";
cout << "■             ┃       班级:软嵌1803                         ┃           ■\n";
cout << "■             ┃                                              ┃           ■\n";
cout << "■             ┃       版本:图书馆管理系统                ┃           ■\n";
cout << "■             ┃                                              ┃           ■\n";
cout << "■             ┃       Weclome to use it!                     ┃           ■\n";
cout << "■             ┃                                              ┃           ■\n";
cout << "■             ┃           All Ringhts Reserved!              ┃           ■\n";
cout << "■             ┃                                              ┃           ■\n";
cout << "■             ┗━━━━━━━━━━━━━━━━━━━━━━━┛           ■\n";
cout << "■                                                                          ■\n";
cout << "■                                                                          ■\n";
cout << "■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\n";
Sleep(2000);//执行挂起一段时间,也就是等待一段时间再继续执行。
system("cls");
while (1)
{
cout << "■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\n";
cout << "■___________________________ 图书馆管理系统______________________________■\n";
cout << "■                  ┏━━━━━━━━━━━━━━┓                      ■\n";
cout << "■                  ┃ ●[0]退出系统。            ┃                      ■\n";
cout << "■                  ┣━━━━━━━━━━━━━━┫                      ■\n";
cout << "■                  ┃ ●[1]增加图书。            ┃                      ■\n";
cout << "■                  ┣━━━━━━━━━━━━━━┫                      ■\n";
cout << "■                  ┃ ●[2]删除图书。            ┃                      ■\n";
cout << "■                  ┣━━━━━━━━━━━━━━┫                      ■\n";
cout << "■                  ┃ ●[3]借阅图书。            ┃                      ■\n";
cout << "■                  ┣━━━━━━━━━━━━━━┫                      ■\n";
cout << "■                  ┃ ●[4]归还图书。            ┃                      ■\n";
cout << "■                  ┣━━━━━━━━━━━━━━┫                      ■\n";
cout << "■                  ┃ ●[5]显示图书信息。        ┃                      ■\n";
cout << "■                  ┣━━━━━━━━━━━━━━┫                      ■\n";
cout << "■                  ┃ ●[6]查询图书。            ┃                      ■\n";
cout << "■                  ┗━━━━━━━━━━━━━━┛                      ■\n";
cout << "■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\n";
cout << "输入要进行的操作:" ;
cin >> temp;
switch (temp){
case 1:
system("cls");
l.addBook(); save();//将数据写入一个临时文件。
break;
case 2:system("cls");
l.deleteBook(); save();
break;
case 3:system("cls");
l.brrowBook(); save();
break;
case 4:system("cls");
l.returnBook(); save();
break;
case 5:system("cls");
l.showInfo();
break;
case 6:system("cls");
l.getReader();
break;
case 0:
save();
exit(1);
break;
default:
cout << "输入错误!" << endl;
system("cls");
}
}
}

int main()
{
read();//文件读入
menu();//菜单输出
return 0;
}

实验效果图:







内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐