#include "MangerBook.h" #include "Book.h" #include #include #include #include MangerBook::MangerBook()//构造函数 { this->Book_Array = new vector; this->Init_Book();//初始化 } void MangerBook::Init_Book() { /* ifstream fi; fi.open("D:/c++一些代码/library class版/library class版/图书信息.txt",ios::in); if (!fi.is_open()) { std::cerr << "无法打开文件" << std::endl; return; } string _name; int _year,_month,_day; int _isleadbook; int _times; while(fi>>_name&&fi>>_year&&fi>>_month && fi>>_day&&fi>>_isleadbook&&fi>>_times) { Date _data(_year,_month,_day); Book *_pstu = NULL; if(_isleadbook==0) _pstu = new Book(_name,_data,true,_times); else _pstu = new Book(_name,_data,false,_times); this->Book_Array->push_back(_pstu); } fi.close(); */ //初始六本书,本来想用文件输入,但是无法输出,最后也没有解决,上面注释是输入失败的代码 Book* _pstu1 = NULL; Date _data1(2024, 3, 4); _pstu1 = new Book("数据结构基础", _data1, false, 117); this->Book_Array->push_back(_pstu1); Book* _pstu2 = NULL; Date _data2(2023, 10, 17); _pstu2 = new Book("红楼梦", _data2, true, 11); this->Book_Array->push_back(_pstu2); Book* _pstu3 = NULL; Date _data3(2024, 5, 16); _pstu3 = new Book("计算机网络", _data3, false, 98); this->Book_Array->push_back(_pstu3); Book* _pstu4 = NULL; Date _data4(2023, 10, 4); _pstu4 = new Book("离散数学", _data1, true, 11); this->Book_Array->push_back(_pstu4); Book* _pstu5 = NULL; Date _data5(2023, 2, 16); _pstu5 = new Book("操作系统", _data5, false, 110); this->Book_Array->push_back(_pstu5); Book* _pstu6 = NULL; Date _data6(2023, 10, 1); _pstu6 = new Book("工科数学分析", _data6, false, 111); this->Book_Array->push_back(_pstu6); } void MangerBook::ShowMenu() { 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; } void MangerBook::Exit_Sys()//退出系统函数 { cout << "谢谢使用" << endl; exit(-1); } bool cmp(Book* book1, Book* book2)//设置排列顺序从大到小 { return book1->getTimes() > book2->getTimes(); } void MangerBook::LoanRanking()//借出排行,使用STL,从大到小排列 { sort(Book_Array->begin(), Book_Array->end(), cmp); for (int i = 0; i < (int)Book_Array->size(); i++) { this->Book_Array->at(i)->ShowInfo(); } system("pause"); system("cls"); } void MangerBook::ShowBook()//显示在库图书信息 { for (int i = 0; i < (int)Book_Array->size(); i++) { this->Book_Array->at(i)->ShowInfo(); } system("pause"); system("cls"); } void MangerBook::AddBook()//添加书籍 { int i = 1;//记录添加 while (true) { char flag; string _name; cout << "请输入添加第" << i << "本书的名字" << endl; cin >> _name; Book* _pstu = NULL; Date _data(0, 0, 0); _pstu = new Book(_name, _data, false, 0);//新入库图书默认借出次数为零 this->Book_Array->push_back(_pstu); i++; cout << "是否继续录入信息?" << endl; cout << "Y/y继续录入,N/n结束录入" << endl; while (true) { //非法检测 cin >> flag; if (flag == 'Y' || flag == 'y' || flag == 'N' || flag == 'n') { break; } else { cout << "输入的指令非法!请重新输入!" << endl; } } if (flag == 'Y' || flag == 'y') { continue; } else { break; } } cout << "成功录入了" << i - 1 << "本书信息!" << endl; system("pause"); system("cls"); } void MangerBook::SearchBook() { cout << "请输入待查询书籍名称" << endl; string searchname; cin >> searchname; int index; index = IndexBook(searchname);//寻找在数组中位置 if (index != -1) { this->Book_Array->at(index)->ShowInfo(); } system("pause"); system("cls"); } int MangerBook::IndexBook(string name)//索引函数 { int ret = -1; for (int i = 0; i < (int)Book_Array->size(); i++) { if (this->Book_Array->at(i)->getName() == name) { ret = i; break; } } return ret; } void MangerBook::SelectBook() { cout << "请输入待删除书籍名称" << endl; string searchname; cin >> searchname; int index; index = IndexBook(searchname); if (index != -1) { if (this->Book_Array->at(index)->getStatue() == false) {//书籍不在库则无法删除 this->Book_Array->erase(this->Book_Array->begin() + index); cout << "删除成功!" << endl; } else { cout << "书籍不在库!,删除失败!" << endl; } } else { cout << "书籍信息不存在!,删除失败!" << endl; } system("pause"); system("cls"); } void MangerBook::LoanBook()//借出 { cout << "请输入书籍名称" << endl; string searchname; cin >> searchname; int c_year, c_month, c_day; int index; index = IndexBook(searchname); if (index != -1) { if (this->Book_Array->at(index)->getStatue() == false) {//书籍不在库则无法借出 cout << "请输入借出日期(年 月 日)" << endl; cin >> c_year >> c_month >> c_day; Date c_data(c_year, c_month, c_day); // 检查还书日期的合法性 if (!LegalDate(c_data)) { cout << "日期非法,请重新输入!" << endl; // 添加循环以重复要求有效日期 while (true) { cout << "请输入借出日期(年 月 日)" << endl; cin >> c_year >> c_month >> c_day; c_data = Date(c_year, c_month, c_day); if (LegalDate(c_data)) { break; } else { cout << "日期非法,请重新输入!" << endl; } } } Book* _pstu1 = NULL; Date _data1(c_year, c_month, c_day); _pstu1 = new Book(searchname, _data1, true, this->Book_Array->at(index)->getTimes() + 1); int insertIndex = index; // 要插入的位置(可以根据需求修改) if (insertIndex >= 0 && insertIndex < (int)this->Book_Array->size()) { delete this->Book_Array->at(insertIndex); // 删除原来的对象,避免内存泄漏 this->Book_Array->at(insertIndex) = _pstu1; // 插入新的 Book 对象 } else { cout << "插入位置无效!" << endl; } cout << "借出成功,请妥善保管" << endl; } else { cout << "书籍不在库!,借出失败!" << endl; } } else { cout << "书籍信息不存在!,借出失败!" << endl; } system("pause"); system("cls"); } bool isLeapyear(int year)//判断是否是闰年 { if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) return true; return false; } bool MangerBook::LegalDate(const Date& date) //检测日期合法性 { int y = date.getYear(); int m = date.getMonth(); int d = date.getDay(); bool ret = true; if (m > 12 || m < 1) ret = false; else if (((m == 1) || (m == 3) || (m == 5) || (m == 7) || (m == 8) || (m == 10) || (m == 12)) && d > 31) ret = false; else if (((m == 4) || (m == 6) || (m == 9) || (m == 11)) && d > 30) ret = false; if (isLeapyear(y) && m == 2 && d > 29) ret = false; else if (!isLeapyear(y) && m == 2 && d > 28) ret = false; return ret; } int diffDay(int year1, int year2, int month2, int day2)//计算相差多少年转化为天,即总天数差 { int ans = 0; if (year1 == year2) return 0; else if (year1 > year2) return -1e6; int cntyear = abs(year2 - year1); for (int i = 0; i < cntyear; i++) { ans += 365; } for (int i = min(year1, year2); i <= max(year1, year2); i++) { if (isLeapyear(i)) { ans++; } } if (isLeapyear(year2) && ((month2 < 2) || (month2 == 2 && day2 < 29))) ans--; return ans; } int Daytoend(int y, int m, int d) //计算到年底的天数 { int tm = 0, tEY = 0; int month[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; if (isLeapyear(y)) month[1] = 29; for (m -= 1; m <= 11; m++) tm += month[m]; tEY = tm - d; return tEY; } int MangerBook::CntDay(const Date& date1, const Date& date2)//计算相差多少天 { int cntday1 = diffDay(date1.getYear(), date2.getYear(), date2.getMonth(), date2.getDay()); int cnt_day_end1 = Daytoend(date1.getYear(), date1.getMonth(), date1.getDay()); int cnt_day_end2 = Daytoend(date2.getYear(), date2.getMonth(), date2.getDay()); int CalculateDateDifference = cntday1 + cnt_day_end1 - cnt_day_end2; //用总天数差加上两个日期到当年年底天数之差即为结果 return CalculateDateDifference; } void MangerBook::ReturnkBook()//归还书籍 { cout << "请输入归还书籍名称" << endl; string searchname; cin >> searchname; int r_year; int r_month; int r_day; int index; index = IndexBook(searchname); if (index != -1) { if (this->Book_Array->at(index)->getStatue() == true) { cout << "请输入归还日期(年 月 日)" << endl; cin >> r_year >> r_month >> r_day; Date r_data(r_year, r_month, r_day); // 检查还书日期的合法性 if (!LegalDate(r_data)) { cout << "日期非法,请重新输入!" << endl; // 添加循环以重复要求有效日期 while (true) { cout << "请输入归还日期(年 月 日)" << endl; cin >> r_year >> r_month >> r_day; r_data = Date(r_year, r_month, r_day); if (LegalDate(r_data)) { break; } else { cout << "日期非法,请重新输入!" << endl; } } } // 计算租借天数 int chargeDay = CntDay(this->Book_Array->at(index)->getDate(), r_data); while (chargeDay < 0) { cout << "日期非法,请重新输入!" << endl; cout << "请输入归还日期(年 月 日)" << endl; cin >> r_year >> r_month >> r_day; r_data = Date(r_year, r_month, r_day); chargeDay = CntDay(this->Book_Array->at(index)->getDate(), r_data); } // this->Book_Array->at(index)->outTurnin(); double charge = chargeDay * 0.1; this->Book_Array->at(index)->Show_LoanTime(); cout << "借出" << chargeDay << "天,日租1角" << "共需支付" << charge << "元" << endl; cout << "归还成功,谢谢使用!" << endl; } else { cout << "已归还,请勿重复归还!" << endl; } } else { cout << "书籍信息不存在!,归还失败!" << endl; } system("pause"); system("cls"); } MangerBook::~MangerBook()//析构函数 { //dtor if (this->Book_Array) { this->Book_Array->clear(); delete[] this->Book_Array; this->Book_Array = NULL;//删除后,让对象指针指向空 } }