当前位置:文档之家› 图书馆管理系统代码

图书馆管理系统代码

#include<class.h>#define DEBUG 0//宏定义决定测试代码是否编译0为不编译非0则编译const char book_path[20] = "books.txt";const char reader_path[20] = "readers.txt";//文件读写声明void read_from_files_to_vector();void write_all_to_files();#if DEBUGvoid scan_readers() {for(reader &s:readers) {wcout << s;}}#endifint main(){//完成操作后需要在控制台按5中退出才会保存到文件wcin.imbue(china);wcout.imbue(china);read_from_files_to_vector();#if DEBUGcout << "size of readers = " << readers.size() << endl;cout << "size of books = " << books.size() << endl;scan_book();scan_readers();#endifstring password = "123456";//密码已改cout << "------------欢迎使用中南大学网络在线图书系统------------"<<endl;//这里可以添加你们小组的名单或一些装饰的字符cout << "--------------------请选择登录方式--------------------" << endl;cout << " 1 管理员登陆2读者登陆" << endl;int a;cin >> a;cleanScreen();if (a == 1){string str;for (int j = 0; j <= 4; j++){cout << "请输入管理密码,你有" << (4-j) << "次机会" << endl;cin >> str;if (password == str){break;}else cout << "密码错误!请重新输入" << endl;if (j == 4)return 0;}cleanScreen();cout << "-------------欢迎来到图书管理系统-------------" << endl;cout << "成功登陆!你有以下操作可供选择" << endl;cout << endl;//操作循环cout << " 1 浏览图书信息" << endl;cout << " 2 图书信息修改" << endl;cout << " 3 图书信息添加" << endl;cout << " 4 图书信息删除" << endl;cout << " 5 保存信息并退出系统" << endl;cout << "--------------请选择操作选项编号--------------" << endl;int b;cin >> b;cleanScreen();while (b != 5) {switch (b){case 1:scan_book(); break;case 2:modify_book(); break;case 3:add_book(); break;case 4:delete_book(); break;default:cout << "输入错误,请重新输入"; break;}cout << endl;cout << "---------------请进行下一次操作---------------" << endl;cout << " 1 浏览图书信息" << endl;cout << " 2 图书信息修改" << endl;cout << " 3 图书信息添加" << endl;cout << " 4 图书信息删除" << endl;cout << " 5 退出系统" << endl;cout << "--------------请选择操作选项编号--------------" << endl;cin >> b;cleanScreen();}write_all_to_files();return 0;}else if (a == 2){int readerIndex = 0;//记录读者下标//输入学号登陆cout << "--------------请输入学号--------------" << endl;wchar_t studentnumber[20];for (int m = 0; m <= 9; m++){cout << "请输入学号,你有" << (9-m) << "次机会" << endl;wcin >> studentnumber;int t;for (t = 0; t < readers.size(); t++){if (wcscmp(studentnumber, readers[t].cardnum) == 0){readerIndex = t;break;}}//判断跳出循环原因if (t < readers.size())break;else{cout << "登录账号不存在,请重新输入学号" << endl;continue;}if (m == 9)return 0;}cleanScreen();cout << "------------欢迎来到读者自助系统------------" << endl;wcout << L"欢迎你," << readers[readerIndex].name << L"!你有以下操作可供选择" << endl;cout << endl;//操作循环cout << " 1 图书浏览" << endl;cout << " 2 图书查询" << endl;cout << " 3 图书借阅" << endl;cout << " 4 图书返还" << endl;cout << " 5 退出系统" << endl;cout << "---------------请选择操作选项--------------" << endl;int c;cin >> c;cleanScreen();while (c != 5){switch (c){case 1:scan_book(); break;case 2:search_book(); break;case 3:borrow_book(readerIndex); break;case 4:return_book(readerIndex); break;default:cout << "输入错误!请重新输入" << endl; break;}cout << endl;cout << "--------------请进行下一次操作--------------" << endl;cout << " 1 图书浏览" << endl;cout << " 2 图书查询" << endl;cout << " 3 图书借阅" << endl;cout << " 4 图书返还" << endl;cout << " 5 退出系统" << endl;cout << "---------------请选择操作选项---------------" << endl;cin >> c;cleanScreen();}write_all_to_files();return 0;}return 0;}//main 函数结束//写入/读取文件void write_all_to_files(){wofstream outfile;outfile.imbue(locale("chs"));//写入书本文件outfile.open(book_path);outfile << books.size() << '\n';for (int i = 0; i < books.size(); i++){outfile << books[i];}outfile.close();outfile.clear();//写入读者文件,由于程序中没有针对读者的删改(备用,将0改为1后即可编译这段代码) #if 0outfile.open(path::reader_path);outfile << readers.size() << '\n';for (int i = 0; i < readers.size(); i++){outfile << readers[i];}outfile.close();#endifreturn;}void read_from_files_to_vector(){wifstream infile;infile.imbue(locale("chs"));//用于设置读写中文book bb;reader rr;//在程序目录下没有相应文件时创建空文件(备用,将0改为1后即可编译这段代码) #if 0wofstream creat_if_no;creat_if_no.imbue(locale("chs"));creat_if_no.open(book_path,ios::_Noreplace);creat_if_no.close();creat_if_no.clear();creat_if_no.open(reader_path, ios::_Noreplace);creat_if_no.close();creat_if_no.clear();#endif//读取书本数据infile.open(book_path);int size = 0;infile >> size;while (!infile.eof() && size != 0){infile >> bb;books.push_back(bb);size--;}infile.close();infile.clear();//读取读者数据infile.open(reader_path);infile >> size;while (!infile.eof() && size != 0){infile >> rr;readers.push_back(rr);size--;}infile.close();return;}#include "classes.h"locale china("chs");//use china character/*void printout_book(book &b){cout << b.id << '\n'<< b.title << '\n'<< b.firstauthor << '\n'<< b.publisher <<'\n'<< b.birthday << '\n'<< b.location << '\n'<< b.price << '\n'<< b.number << endl;}*///这个函数我用重载运算符<< 的函数代替了//对应全局变量的定义vector<book> books;vector<reader> readers;//book重载操作符函数定义wistream& operator >> (wistream& in, book &b){cout << "请输入书号" << endl;in >> b.id;cout << "请输入书名" << endl;in >> b.title;cout << "请输入作者名" << endl;in >> b.firstauthor;cout << "请输入出版社" << endl;in >> b.publisher;cout << "请输入印刷日期(年月)" << endl;in >> b.birthday;cout << "请输入所属类别" << endl;in >> b.location;cout << "请输入价格" << endl;in >> b.price;cout << "请输入数量" << endl;in >> b.number;return in;}wostream& operator<<(wostream& out, book &b){out << b.id << ' '<< b.title << ' '<< b.firstauthor << ' '<< b.publisher << ' '<< b.birthday.year << ' '<< b.birthday.month << ' '<< b.location << ' '<< b.price << ' '<< b.number << endl;return out;}wifstream& operator >> (wifstream& in, book &b){in >> b.id >> b.title >> b.firstauthor>> b.publisher >> b.birthday.year >> b.birthday.month>> b.location >> b.price >> b.number;return in;}wofstream& operator<<(wofstream& out, book &b){out << b.id << ' '<< b.title << ' '<< b.firstauthor << ' '<< b.publisher << ' '<< b.birthday << ' '<< b.location << ' '<< b.price << ' '<< b.number << '\n';return out;}//reader重载操作符函数定义wistream& operator >> (wistream& in, reader &r){//由于没有对reader的操作,这个函数尚未被使用cout << "请输入读者姓名" << endl;in >> ;cout << "请输入性别(男为0,女为1)" << endl;cout << "请输入卡号" << endl;in >> r.cardnum;cout << "请输入学位(学士为0,硕士为1,博士为2)" << endl;in >> r.e;r.already = 0;//已借书本数默认为0return in;}wostream& operator<<(wostream& out, reader &r){out << r.cardnum << ' ';out << << ' ';out << r.s << ' ';out << r.e << ' ';out << r.already << endl;return out;}wifstream& operator >> (wifstream& in, reader &r){in >> r.cardnum >> >> r.s>> r.e >> r.already;return in;}wofstream& operator<<(wofstream& out, reader &r){out << r.cardnum << ' '<< << ' '<< r.s << ' '<< r.e << ' '<< r.already << '\n';return out;}//main函数中调用定义void scan_book() //建立浏览函数{for (int i = 0; i < books.size(); i++){wcout << books[i];}}void search_book() //建立搜索函数{cout << "************你有以下查询方式可供选择************" << endl; cout << " 1 按书名查询" << endl;cout << " 2 按编号查询" << endl;cout << " 3 按作者查询" << endl;cout << "************请选择查询方式************" << endl;cin >> g;switch (g){case 1:{wchar_t name[20];cout << "************请输入书名************" << endl;wcin >> name;for (int r = 0; r < books.size(); r++){if (wcscmp(books[r].title, name) == 0){wcout << books[r];return;}}cout << "查无此书" << endl;return;break;}case 2:{int str;cout << "************请输入图书编号************" << endl;cin >> str;for (int l = 0; l < books.size(); l++){if (books[l].id == str){wcout << books[l];return;}}cout << "查无此书" << endl;return;break;}case 3:{wchar_t auth[10];cout << "************请输入图书作者************" << endl;wcin >> auth;for (int p = 0; p < books.size(); p++){if (wcscmp(books[p].firstauthor, auth) == 0){wcout << books[p];return;}}cout << "查无此书" << endl;return;break;}default:{cout << "查无此书" << endl;return;}}}//实现逻辑有问题,无法建立书-借书人的对应关系void borrow_book(int t) //建立借书函数{if (readers[t].already >= 5){cout << "抱歉,你的借书数已满,不可再借" << endl;return;}int log;cout << "************请输入要借出图书的登陆号************" << endl;wcin >> log;for (int i = 0; i < books.size(); ++i){if (log == books[i].id){if (books[i].number > 0){int y;cout << "确认借出书籍吗?确认请输入1 取消请输入0" << endl;cin >> y;if (y == 1){cout << "借阅成功!";--(books[i].number);++(readers[t].already);return;}else return;}else{cout << "图书已借完" << endl;return;}}}cout << "图书不存在!" << endl;return;}void return_book(int t) //建立还书函数{int log;cout << "************请输入要归还的图书的登陆号************" << endl;wcin >> log;for (int i = 0; i < books.size(); i++){if (log == books[i].id){cout << "图书归还成功" << endl;++books[i].number;--readers[t].already;return;}}cout << "图书不存在于本图书馆无需归还!" << endl;return;}void modify_book() //建立修改函数{int log;cout << "************请输入要修改的图书编号************" << endl;wcin >> log;for (int i = 0; i < books.size(); i++){if (log == books[i].id){cout << "需修改图书信息如下" << endl;wcout << books[i];cout << "************请输入修改后的信息************" << endl;wcin >> books[i];return;}}cout << "图书不存在无法修改" << endl;return;}void delete_book(){int log;cout << "************请输入要删除的图书编号************" << endl;cin >> log;//迭代器实现删除指定图书vector<book>::iterator itor2;for (vector<book>::iterator iter = books.begin(); iter != books.end(); ){if (log == iter->id){cout << "将删除的图书信息如下" << endl;wcout << *iter;int sure = 0;cout << "确定删除?(1确定,0取消)" << endl;cin >> sure;if (sure == 0)return;//开始在内存中删除itor2 = iter;books.erase(itor2);cout << "编号为" << log << "的图书已删除" << endl;return;}elseiter++;}cout << "图书不存在无法删除" << endl;return;}void add_book(){book new_book;cout << "************请输入新图书信息************" << endl;wcin >> new_book;books.push_back(new_book);return;}。

相关主题