#include <iostream>
#include<vector>
using namespace std;
/*
* 日期类
*/
class Calendar {
friend istream& operator>>(istream& in, Calendar& c);
friend ostream& operator<<(ostream& out, Calendar& c);
private:
int year = 0;
int month = 0;
int day = 0;
//判断是否闰年
bool IsLeapYear()
{
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
public:
Calendar() {}
Calendar(int year, int month, int day) {
this->year = year;
this->month = month;
this->day = day;
}
//重载赋值运算符
Calendar operator=(Calendar& c)
{
this->year = c.getYear();
this->month = c.getMonth();
this->day = c.getDay();
return *this;
}
//重载加法运算符
Calendar& operator+(int i)
{
Calendar temp;
temp = *this;
int months[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
while (i >= 365)
{
if (temp.month >= 3)
{
temp.year++;
if (IsLeapYear())
i = i - 366;
else i = i - 365;
}
if (temp.month <= 2)
{
if (IsLeapYear())
i = i - 366;
else i = i - 365;
temp.year++;
}
}
while (i >= 28){
if (IsLeapYear() && temp.month == 2)
i = i - 29;
else i = i - months[temp.month];
temp.month++;
if (temp.month > 12)
{
temp.month = 1;
temp.year++;
}
}
if ((temp.day + i) > months[temp.month])
{
if (IsLeapYear() && temp.month == 2)
i = i - 29;
i = i - months[temp.month];
if (temp.month == 12)
{
temp.month = 1;
temp.year++;
}
else temp.month++;
}
temp.day = temp.day + i;
if (temp.day <= 0)
{
if (temp.month == 1)
{
temp.year--;
temp.month = 12;
}
else temp.month--;
if (IsLeapYear() && temp.month == 2)
temp.day = 29 + temp.day;
temp.day = months[temp.month] + temp.day;
}
return temp;
}
//重载前置++运算符
Calendar operator ++();
//重载后置++运算符
Calendar operator ++(int);
//get set 函数若干
int getYear() {
return this->year;
}
void setYear(int year) {
this->year = year;
}
int getMonth() {
return this->month;
}
void setMonth(int month) {
this->month = month;
}
int getDay() {
return this->day;
}
void setDay(int day) {
this->day = day;
}
};
//运算符输出重载
ostream& operator<<(ostream& out, Calendar& calendar) {
out << calendar.getYear() << "年/" << calendar.getMonth() << "月/" << calendar.getDay() << "日" << " ";
return out;
}
//运算符输入重载并做合法性检查
istream& operator>>(istream& in, Calendar& c) {
cout << "请输入日期" << endl;
int a[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
in >> c.year >> c.month >> c.day;
int flag = 0;
if ((c.year % 4 == 0 && c.year % 100 != 0) || (c.year % 400 == 0)) {//如果是闰年
a[1] = 29;
if (0 < c.day && c.day <= a[c.month - 1] && c.month > 0 && c.month <= 12)
flag = 1;
else
flag = 0;
}
else {
if (0 < c.day && c.day <= a[c.month - 1] && 0 < c.month && 12 >= c.month)
flag = 1;
else
flag = 0;
}
if (!flag) {
cout << "输入不合法" << endl;
c.year = 0;
c.month = 0;
c.day = 0;
}
return in;
}
//重载前置++
Calendar Calendar::operator ++()
{
Calendar x;
if (month == 2)
{
if (IsLeapYear() == 1)
{//如果是闰月
if (day == 29)
{
day = 1;
month++;
}
else day++;
}
else
{
if (day == 28)
{
day = 1;
month++;
}
else day++;
}
}
else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
if (month == 12)
{
if (day == 31)
{
day = 1;
month = 1;
year++;
}
else day++;
}
else
{//其他月份 1357
if (day == 31)
{
day = 1;
month++;
}
else day++;
}
}
else
{
if (day == 30)
{
day = 1;
month++;
}
else day++;
}
x.year = year;
x.month = month;
x.day = day;
return x;
}
//重载后置++
Calendar Calendar::operator ++(int)
{
Calendar x;
x.year = year;
x.month = month;
x.day = day;
if (month == 2)
{
if (IsLeapYear() == 1)
{
if (day == 29)
{
day = 1;
month++;
}
else day++;
}
else
{
if (day == 28)
{
day = 1;
month++;
}
else day++;
}
}
else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
if (month == 12)
{
if (day == 31)
{
day = 1;
month = 1;
year++;
}
else day++;
}
else
{
if (day == 31)
{
day = 1;
month++;
}
else day++;
}
}
else
{
if (day == 30)
{
day = 1;
month++;
}
else day++;
}
return x;
}
//借阅记录类
//一条借阅记录所包含的内容,最终保存到vector数组中进行维护
class Record {
private:
int number;//记录编号
Calendar date;
string serial_number;//图书编号
string id;//读者id
string status;//操作状态
string book_type;//图书类型
public:
void show_info_recoed()
{
cout << "第" << this->number << "条记录: "
<< "操作日期: " << this->date << " "
<< "图书编号: " << this->serial_number << " "
<< "图书类型: " << this->book_type << " "
<< "读者id: " << this->id << " "
<< "操作类型: " << this->status << " ";
}
};
//借阅限制信息类
class Borrow_limit {
private:
int max_book = 0;//最大借阅数量
int max_days = 0;//最大借阅天数
public:
void setMax_book(int number)
{
this->max_book = number;
}
void setMax_days(int number)
{
this->max_days = number;
}
int getMax_book()
{
return this->max_book;
}
int getMax_days()
{
return this->max_days;
}
};
//图书类
class Book {
private:
string serial_number;//图书编号 为主键唯一标识***
string book_nume;
string author;
string publication;//出版社
Calendar publication_date;//出版日期
string book_type;
int total_number;
int number_in_lib;
vector<Record> record; //借阅记录向量
public:
//每创建一本书就输入全部信息
Book()
{
cout << "请输入图书编号:";
cin >> this->serial_number;
cout << "请输入书名: ";
cin >> this->book_nume;
cout << "请输入作者: ";
cin >> this->author;
cout << "请输入图书出版社: ";
cin >> this->publication;
cout << "请输入图书出版时间: ";
cin >> this->publication_date;
cout << "请输入图书类型: ";
cin >> this->book_type;
cout << "请输入图书总数量: ";
cin >> this->total_number;
//初始化参数,图书总量即为在管藏书量
this->number_in_lib = this->total_number;
//在管理类中加载关于本书的借阅信息 ------------------------------------------
}
//展示本书的所有信息
void show_book_info()
{
cout << "图书编号:";
cout << this->serial_number;
cout << "\t作者: ";
cout << this->author;
cout << "\t书名: ";
cout << "<<"<<this->book_nume<<">>";
cout << "\t图书出版社: ";
cout << this->publication;
cout << "\t图书出版时间: ";
cout << this->publication_date;
cout << "\t图书类型: ";
cout << this->book_type;
cout << "\t图书总数量: ";
cout << this->total_number;
}
string getSerial_number()
{
return this->serial_number;
}
};
//读者类
class Reader {
private:
string id; //主键id唯一标识***
string name;
string major;
string group;
int has_borrowed;
Borrow_limit bl;
vector<Record>borrow_record;//读者借阅记录向量
public:
//根据id姓名专业班级创建一个新的读者
Reader() {
cout << "请输入借阅者id:";
cin>>this->id;
cout << "请输入借阅者姓名:";
cin>>this->name;
cout << "请输入借阅者专业:";
cin>>this->major;
cout << "请输入借阅者班级:";
cin>>this->group;
//此时为初始化已借数量置零
this->has_borrowed = 0;
//默认借阅限制
this->bl.setMax_book(10);//设置最大借阅书的数量为10
this->bl.setMax_days(60);//设置最大借阅天数为60
//在操作函数中加载当前读者的借阅记录 -------------------------------------------------------------------------
}
string getId()
{
return this->id;
}
void setId(string id)
{
this->id = id;
}
string getName()
{
return this->name;
}
void setName(string name)
{
this->name = name;
}
string getMajor()
{
return this->major;
}
void setMajor(string major)
{
this->major = major;
}
string getGroup()
{
return this->group;
}
void setGroup(string group)
{
this->group = group;
}
int getHas_borrowed()
{
return this->has_borrowed;
}
void setHas_borrowed(int number)
{
this->has_borrowed = number;
}
void setBorrow_limit(int num1, int num2)
{
this->bl.setMax_book(num1);
this->bl.setMax_days(num2);
}
//展示读者全部信息
void show_reader_info()
{
cout << "id:" << this->getId()
<< "\t姓名:" << this->getName()
<< "\t专业:" << this->getMajor()
<< "\t班级:" << this->getGroup()
<< "\t已借阅数量:" << this->getHas_borrowed()
<< "\t借书限制:"
<< "\t最大借书数目:" << this->bl.getMax_book()
<< "\t最大借阅天数:" << this->bl.getMax_days() << endl;
}
//方便调用find
//根据id查询
bool operator==(const string& x)
{
return (this->id == x.c_str());
}
};
//操作类
//维护图书,读者数组,进行增删改查操作,以及借阅,续借,归还操作等
class Administrator
{
private:
//读者数组
vector<Reader> readerList;
//图书数组
vector<Book> bookList;
//借阅记录类
vector<Record> recordList;
public:
/*初始化操作*/
//加载文件
Administrator()
{
//加载文件中的图书、读者、借阅记录到内存中
}
//保存文件
~Administrator()
{
//将数组中的信息保存到文件中
}
/*阅读者操作*/
//显示所有阅读者信息
void show_readers()
{
if (readerList.size() == 0)
cout << "当前没有读者" << endl;
for (vector<Reader>::iterator it = this->readerList.begin(); it != this->readerList.end(); it++)
{
it->show_reader_info();
}
}
//添加一个借阅者
void reader_add()
{
Reader temp;
readerList.push_back(temp);
cout << "添加成功" << endl;
}
//根据id删除阅读者
void delete_reader_by_id(string id)
{
vector<Reader>::iterator b_t;
b_t = find(readerList.begin(), readerList.end(), id);
if (b_t != readerList.end())
{
cout << "成功删除读者:" << endl;
(*b_t).show_reader_info();
readerList.erase(b_t);
}
else
cout << "未找到此读者!" << endl;
}
// 根据姓名删除阅读者 可以删除重名的读者
void delete_reader_by_name(string name)
{
//如果容器的元素数量发生变化,那么迭代器就有可能失效
vector<Reader>::iterator b_t;
int flag = 1;
for (b_t = this->readerList.begin(); b_t != this->readerList.end();)
{
if ((*b_t).getName() == name)
{
cout << "成功删除读者:" << endl;
(*b_t).show_reader_info();
b_t = readerList.erase(b_t);
flag = 0;
}
else
{
b_t++;
}
}
if (flag) cout << "未找到相关读者" << endl;
}
// 根据id查询阅读者
void find_reader_by_id(string id)
{
vector<Reader>::iterator b_t;
int flag = 1;
for (b_t = this->readerList.begin(); b_t != this->readerList.end(); b_t++)
{
if ((*b_t).getId() == id)
{
(*b_t).show_reader_info();
flag = 0;
break;
}
}
if (flag) cout << "未找到相关id的读者" << endl;
}
// 根据姓名查询阅读者
void find_reader_by_name(string name)
{
vector<Reader>::iterator b_t;
int flag = 1;
for (b_t = this->readerList.begin(); b_t != this->readerList.end(); b_t++)
{
if ((*b_t).getName() == name)
{
(*b_t).show_reader_info();
flag = 0;
}
}
if (flag) cout << "未找到相关姓名的读者" << endl;
}
// 根据专业查询阅读者
void find_reader_by_major(string major)
{
vector<Reader>::iterator b_t;
int flag = 1;
for (b_t = this->readerList.begin(); b_t != this->readerList.end();b_t++)
{
if ((*b_t).getMajor() == major)
{
(*b_t).show_reader_info();
flag = 0;
}
}
if (flag) cout << "未找到相关专业的读者" << endl;
}
//根据专业和班级查询阅读者
void find_reader_by_major_and_class(string major,string group)
{
vector<Reader>::iterator b_t;
int flag = 1;
for (b_t = this->readerList.begin(); b_t != this->readerList.end(); b_t++)
{
if ((*b_t).getMajor() == major && (*b_t).getGroup() == group)
{
(*b_t).show_reader_info();
flag = 0;
}
}
if (flag) cout << "未找到相关专业的读者" << endl;
}
/*图书操作*/
//显示所有图书信息
void show_books()
{
if (bookList.size() == 0)
cout << "当前馆藏没有图书" << endl;
for (vector<Book>::iterator it = this->bookList.begin(); it != this->bookList.end(); it++)
{
it->show_book_info();
cout << endl;
}
}
//添加一本书
void book_add()
{
Book temp;
bookList.push_back(temp);
cout << "添加成功" << endl;
}
//根据图书编号删除图书
void delete_book_by_serial_number(string serial_number)
{
bookList.erase(bookList.begin());
}
//根据出版社删除图书
//根据作者删除图书
//根据类型删除图书
//根据编号查询图书
//根据作者查询图书
//根据出版社查询图书
//根据类型查询图书
};
int main()
{
return 0;
}
前面写的一个calendar类导致book在vector中erase无法使用,怎么该?
你问这个类写的是不是有问题,又不把类的代码贴出来。
另外,不要贴图,不要贴图,贴代码。
Book添加重载 == 运算符