图书管理系统(最终版)
作者:互联网
图书的采编入库功能——输入图书基本信息并保存到文件中;
图书的显示库存功能——显示出库存图书的基本信息;
图书的查询功能——根据书名查询图书信息,并显示查询结果;
图书的借阅功能——输入借阅基本信息,改变图书现存量;
图书的归还功能——注销对借阅者的登记,改变该书的现存量;
图书的删除功能——根据书号删除图书信息;
图书的借阅记录功能——显示所有借阅记录的基本信息;
#include<iostream> using namespace std; #include<fstream> #define filename "book2.txt" #define file "stu2.txt" #include<list> #include<cstring> #include<iomanip> class Book //创建Book类,存放图书信息 { public: Book(string na=" " ,int p=0, int n=0, int bid =0 ) { bookid =bid; name = na; num = n; price = p; } void Show() { cout << "\t书号:" << std::left << setw(20) << bookid << "书名:" << std::left << setw(20) << name << std::right << setw(6) << "\t价格:" << price << "\t数量:" << num << endl; } void Set() { cout <<"请再次输入书号:"; cin >> bookid; cout << "请输入书名:"; cin >> name; cout << "请输入价格:"; cin >> price; cout << "请输入数量:"; cin >> num; } void Sets() { cout <<"请输入书号:"; cin >> bookid; } void Addnum() { int n; cout << "请输入归还的数量:"<<endl; cin >> n; num += n; } void Borrownum() { int n; cout << "请输入借出的数量:"<<endl; cin >> n; while(num-n<0){ cout <<"抱歉,本书库存不足,请修改借阅数量"<<endl; cout << "请重新输入借出的数量:"<<endl; cin >> n; } num -= n; } public: string name; int price; int num; int bookid; }; class Stu { public: Stu(int bid =0 , string sid =" ", int da=0) { bookid =bid; stuid =sid; date =da; } void Show() { cout << "\t书号:" << std::left << setw(20) << bookid << "借阅者证件号:" << std::left << setw(20) << stuid << std::right << setw(6) << "\t归还期限:" << date << endl; } void Set() { cout <<"请输入书号:"; cin >> bookid; cout << "请输入借阅者证件号:"; cin >> stuid; cout << "请输入归还期限:"; cin >> date; } public: string stuid; int date; int bookid; }; void menu() { cout << endl; cout << endl; cout << " ******************************************************************" << endl; cout << " *********************欢迎光临图书管理系统*************************" << endl; cout << " **************** 请选择以下功能 *******************" << endl; cout << " **************** 0 - 退出系统 *******************" << endl; cout << " **************** 1 - 显示库存 *******************" << endl; cout << " **************** 2 - 查询图书 *******************" << endl; cout << " **************** 3 - 借阅图书 *******************" << endl; cout << " **************** 4 - 归还图书 *******************" << endl; cout << " **************** 5 - 采编入库 *******************" << endl; cout << " **************** 6 - 删除图书 *******************" << endl; cout << " **************** 7 - 借阅记录 *******************" << endl; cout << " ******************************************************************" << endl; cout << endl; cout << endl; } class Booklist //创建BookList类,数据成员有Book还有图书数量 { public: void save() //新建图书的话保存数据,用app方式打开文件 { ofstream fout(filename, ios::app); list<Book>::iterator it = BList.begin(); for (int i = 0; i < num-1; i++) //偏移迭代器,指向新加入的Book并写入文件 { it++; } for (; it != BList.end(); it++) { fout << (*it).bookid << ' ' << (*it).name << ' ' << (*it).price << ' ' << (*it).num << '\n'; } fout.close(); } void resave() { ofstream fout(filename, ios::out); //重新写入数据,因为删除了某个元素 if (fout.is_open()) { for (list<Book>::iterator it = BList.begin(); it != BList.end(); it++) { fout << (*it).bookid<< ' ' << (*it).name << ' ' << (*it).price << ' ' << (*it).num << '\n'; } } fout.close(); } void Show() { for (list<Book>::iterator it = BList.begin(); it != BList.end(); it++) { (*it).Show(); } } void adddata() //添加数据 { Book B; B.Set(); BList.push_back(B); num++; } int select() { Book B; B.Sets(); for (list<Book>::iterator it = BList.begin(); it != BList.end(); it++) { if ((*it).bookid == B.bookid){ cout << "本书已经在库中,请直接输入数量:"; cin >> num; (*it).num+=num; return 1; } else{ return 0; } } } void start() //程序一开始读取文件里的数据 { string na; int n; int p; ifstream fin(filename, ios::in); if (fin.is_open()) { while (fin >> na >> p >> n) { Book B(na, p, n); BList.push_back(B); num++; } } fin.close(); } void increase() { cout << "请输入书名:" << endl; string n; cin >> n; for (list<Book>::iterator it = BList.begin(); it != BList.end(); it++) { if ((*it).name == n) (*it).Addnum(); } resave(); } void decrease() { cout << "请输入书名:" << endl; string n; cin >> n; for (list<Book>::iterator it = BList.begin(); it != BList.end(); it++) { if ((*it).name == n) (*it).Borrownum(); } resave(); } void FindBook() { string name; cout << "请输入书名:"; cin >> name; for (list<Book>::iterator it = BList.begin(); it != BList.end(); it++) //遍历整个list,所以符合关键字的都会被找到 { int index = (*it).name.find(name); //如果没找到返回值是一个很大的数 if (index < (*it).name.length()) (*it).Show(); } } void DeleteBook() { string name; cout << "请输入书名:"; cin >> name; int i = 0; for (list<Book>::iterator it = BList.begin(); it != BList.end();it++) { if ((*it).name == name) break; ++i; } list<Book>::iterator it = BList.begin(); advance(it, i); BList.erase(it); --num; resave(); } public: list<Book>BList; int num = 0; }; class Stulist //创建BookList类,数据成员有Book还有图书数量 { public: void save() //新建图书的话保存数据,用app方式打开文件 { ofstream fout(file, ios::app); list<Stu>::iterator it = SList.begin(); for (int i = 0; i < num-1; i++) //偏移迭代器,指向新加入的Book并写入文件 { it++; } for (; it != SList.end(); it++) { fout << (*it).bookid << ' ' << (*it).stuid << ' ' << (*it).date << '\n'; } fout.close(); } void resave() { ofstream fout(file, ios::out); //重新写入数据,因为删除了某个元素 if (fout.is_open()) { for (list<Stu>::iterator it = SList.begin(); it != SList.end(); it++) { fout << (*it).bookid << ' ' << (*it).stuid << ' ' << (*it).date << '\n'; } } fout.close(); } void Show() { for (list<Stu>::iterator it = SList.begin(); it != SList.end(); it++) { (*it).Show(); } } void adddata() //添加数据 { Stu S; S.Set(); SList.push_back(S); num++; } void start() //程序一开始读取文件里的数据 { int bid; string sid; int da; ifstream fin(file, ios::in); if (fin.is_open()) { while (fin >> bid >> sid >> da) { Stu S (bid, sid, da); SList.push_back(S); num++; } } fin.close(); } void FindStu() { int stuid; cout << "请输入借阅者证件号:"; cin >> stuid; for (list<Stu>::iterator it = SList.begin(); it != SList.end(); it++) //遍历整个list,所以符合关键字的都会被找到 { int index = (*it).stuid.find(stuid); //如果没找到返回值是一个很大的数 if (index < (*it).stuid.length()) (*it).Show(); } } void DeleteStu() { int bookid; cout << "请输入书号:"; cin >> bookid; int i = 0; for (list<Stu>::iterator it = SList.begin(); it != SList.end();it++) { if ((*it).bookid == bookid) break; ++i; } list<Stu>::iterator it = SList.begin(); advance(it, i); SList.erase(it); --num; resave(); } public: list<Stu>SList; int num = 0; }; int main() { Booklist B1; Stulist S1; B1.start(); S1.start(); while (1) { menu(); int key; cout << "请输入要进行的操作:"; cin >> key; switch (key) { case 0: return 0; break; case 1: B1.Show(); break; case 2: B1.FindBook(); break; case 3: cout<<"现有库存情况为,请选择你想借阅的书籍:"<<endl; B1.Show(); B1.decrease(); S1.adddata(); S1.save(); break; case 4: cout<<"现有借阅情况为,请选择你想归还的书籍:"<<endl; S1.Show(); B1.increase(); S1.DeleteStu(); break; case 5: { int a; a=B1.select(); if(a==1){ B1.resave(); } else { B1.adddata(); B1.save(); } break; } case 6: B1.DeleteBook(); break; case 7: S1.Show(); break; } } }
标签:num,cout,管理系统,int,void,++,最终版,图书,BList 来源: https://www.cnblogs.com/rongzhang/p/16484156.html