- 感谢大佬们的map模糊查找,使得本菜菜初步完成框架,实在是太困了,这个点,我已经睁不开眼了,但是我还是要坚持发完博客,测完数据。(利用STL实现的部分图书管理系统功能)。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
#include <vector>
#include <queue>
#include <unordered_map>
#include <fstream>
using namespace std;
class max_books {
int book_nums;
public:
max_books(int n) {
book_nums = n;
}
max_books() {}
int get_booknums() {
return book_nums;
}
};
class max_data {
int data_max;
public:
max_data(int n) { data_max = n; }
max_data() {}
int get_datanums() { return data_max; }
};
class date {
int year, month, day;
public:
date(int a, int b, int c) { year = a; month = b; day = c; }
date(){}
friend ostream& operator<<(ostream& out, date& p);
friend istream& operator>>(istream& in, date& p);
bool operator+(int n);
bool operator<(const date& p)const;
int get_year() { return year; }
int get_month() { return month; }
int get_day() { return day; }
};
ostream& operator<<(ostream& out, date& p) {
out << p.year << " " << p.month << " " << p.day << " ";
return out;
}
istream& operator>>(istream& in, date& p) {
int x, y, z;
int max_days=0;
int arr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
bool flag = 0;
while (in >> x >> y >> z) {
if (x % 4 == 0 && x % 100 != 0 || x % 100 == 0) {
flag = 1;
}
if(y > 12 || y < 1) {
continue;
}
else {
if (flag == 1) {
arr[1] = 29;
}
max_days = arr[y - 1];
}
if (y <= max_days) {
p.year = x;
p.month = y;
p.day = z;
break;
}
}
return in;
}
bool date::operator<(const date& p)const {
if (year != p.year) {return year < p.year;}
else if (month != p.month)
{return month < p.month;}
else {return day < p.day;}
}
////void test_date() {
// date p(2020,10,5);
// cout << p.get_year() << endl;
// cout << p.get_month() << endl;
// cout << p.get_day() << endl;
// date a;
// cin >> a;
// cout << a;
//}
class books {
string nums;
string name;
string zuozhe;
string press;
string book_size;
date add;
int sum_books;
int now_books;
public:
books(string anums, string aname, string azuozhe, string apress, string abook_size, int asum_books, int anow_books,date a_add) {
nums=anums;
name=aname;
zuozhe=azuozhe;
press=apress;
add = a_add;
book_size=abook_size;
sum_books=asum_books;
now_books=anow_books;
}
books(){}
void set_nums(string a_nums) { nums = a_nums; }
void set_name(string a_name) { name = a_name; }
void set_zuozhe(string a_zuozhe) { zuozhe = a_zuozhe; }
void set_press(string z_press) { press = z_press; }
void set_booksize(string size) { book_size = size; }
void set_sumbooks(int a) { sum_books = a; }
void set_nowbooks(int a) { now_books = a; }
void set_date(date a) { add = a; }
string get_nums() { return nums; }
string get_name() { return name; }
string get_zuozhe() { return zuozhe; }
string get_press() { return press; }
string get_book_size() { return book_size; }
int get_sum_books() { return sum_books; }
int get_now_books() { return now_books; }
date get_date() {
return add;
}
friend ostream& operator<<(ostream& out, books& p);
friend istream& operator>>(istream& in, books& p);
};
ostream& operator<<(ostream& out, books& p) {
out << p.nums << " " << p.name << " " << p.zuozhe << " " << p.press << " " << p.book_size << " " << p.add << endl;
return out;
}
istream& operator>>(istream& in, books& p) {
in >> p.nums >> p.name >> p.zuozhe >> p.press >> p.book_size >> p.add;
return in;
}
class caozuo {
vector<books>book_v;
multimap<string, int>book_nums;
multimap<string, int>book_name;
multimap<string, int>book_zuozhe;
multimap<string, int>book_press;
multimap<string, int>book_size;//书的类型
multimap<date, int>book_date;
public:
caozuo() {book_xie();}
~caozuo() {
book_du();
}
//文件读写函数
void book_xie();
void book_du();
//增加
void book_add(books b);
//最终要的查询操作
void c_booknums(string nums);
void c1_bookname(string name);
void c2_bookname(string s);
void c1_bookzuozhe(string zuozhe);
void c2_bookzuozhe(string s);
void c1_bookpress(string press);
//void c2_bookpress(string s);
void c1_booksize(string size);
//void c2_booksize(string size);
void c1_date(date a);
void c2_date(date a, date b);
//修改(需要重建map)
void xiu_booknums(string nums,books book);
//删除(需要重建map)
void del_book(string nums);
//重建map函数
void new_bookmap();
};
void caozuo::book_xie() {
books book;
ifstream ifs("caozuo.txt",ios::in);
if (!ifs) { return; }
while (ifs >> book) {
book_v.push_back(book);
book_nums.insert(make_pair(book.get_nums(), book_v.size() - 1));
book_name.insert(make_pair(book.get_name(), book_v.size() - 1));
book_zuozhe.insert(make_pair(book.get_zuozhe(), book_v.size() - 1));
book_press.insert(make_pair(book.get_press(), book_v.size() - 1));
book_size.insert(make_pair(book.get_book_size(), book_v.size() - 1));
book_date.insert(make_pair(book.get_date(), book_v.size() - 1));
}
ifs.close();
}
void caozuo::book_du() {
//cout << " * ****" << endl;
fstream ofs("caozuo.txt", ios::out);
vector<books>::iterator it;
for (it = book_v.begin(); it < book_v.end(); it++) {
ofs << *it;//文件读取工作一定要注意
}
}
void caozuo::book_add(books b) {
multimap<string, int>::iterator it;
it = book_nums.find(b.get_nums());
if (it == book_nums.end()) {
book_v.push_back(b);
b.set_sumbooks(b.get_sum_books() + 1);
b.set_nowbooks(b.get_now_books() + 1);
book_nums.insert(make_pair(b.get_nums(), book_v.size() - 1));
book_name.insert(make_pair(b.get_name(), book_v.size() - 1));
book_zuozhe.insert(make_pair(b.get_zuozhe(), book_v.size() - 1));
book_press.insert(make_pair(b.get_press(), book_v.size() - 1));
book_size.insert(make_pair(b.get_book_size(), book_v.size() - 1));
book_date.insert(make_pair(b.get_date(), book_v.size() - 1));
}
else {
b.set_sumbooks(book_v[it->second].get_sum_books() + 1);
b.set_nowbooks(book_v[it->second].get_now_books() + 1);
book_v[it->second] = b;
}
}
void caozuo::c_booknums(string nums) {
multimap<string, int>::iterator it;
it = book_nums.find(nums);
if (it != book_nums.end()) {
cout << book_v[it->second];
}
else {
cout << -1 << endl;
}
}
void caozuo::c1_bookname(string name) {
multimap<string, int>::iterator it;
it = book_name.find(name);
if (it != book_name.end()) {
cout << book_v[it->second];
}
else {
cout << -1 << endl;
}
}
void caozuo::c1_bookpress(string press) {
multimap<string, int>::iterator it;
pair < multimap<string, int>::iterator, multimap<string, int>::iterator>range;
range = book_press.equal_range(press);
for (it = range.first; it!= range.second; it++) {
cout << book_v[it->second];
}
}
void caozuo::c1_booksize(string size) {
multimap<string, int>::iterator it;
pair < multimap<string, int>::iterator, multimap<string, int>::iterator>range;
range = book_size.equal_range(size);
for (it = range.first; it != range.second; it++) {
cout << book_v[it->second];
}
}
void caozuo::c1_bookzuozhe(string zuoshe) {
multimap<string, int>::iterator it;
pair < multimap<string, int>::iterator, multimap<string, int>::iterator>range;
range = book_zuozhe.equal_range(zuoshe);
for (it = range.first; it != range.second; it++) {
cout << book_v[it->second];
}
}
void caozuo::c1_date(date a) {
multimap<date, int>::iterator it;
pair < multimap<date, int>::iterator, multimap<date, int>::iterator>range;
range = book_date.equal_range(a);
//因为只有一本时不容易输出
if (range.first == book_date.end()) {
return;
}
it = range.first;
do {
cout << book_v[it->second];
it++;
} while (it != range.second);
}
void caozuo::c2_date(date a, date b) {
multimap<date, int>::iterator it1, it2, it;
it1 = book_date.lower_bound(a);
it2 = book_date.upper_bound(b);
for (it = it1; it!=it2; it++) {
cout << book_v[it->second];
}
}
//模糊查找,重载find_if函数
class map_value_finder
{
public:
map_value_finder(const string& cmp_string) :map_cmp_string(cmp_string) {}
bool operator ()(const multimap<string, int>::value_type& pair)
{
int l;
l = pair.first.find(map_cmp_string);
if (l != pair.first.npos)
return true;
return false;
}
private:
const string& map_cmp_string;
};
//利用书名进行模糊查找
void caozuo::c2_bookname(string s) {
multimap<string, int>::iterator it, p1, p2, p, begin;
p1 = book_name.begin();
p2 = book_name.end();
begin = p1;
for (p = p1; p != p2; p++)
{
it = find_if(begin, book_name.end(), map_value_finder(s));
if (it == book_name.end()) {
return;
}
else {
cout << book_v[it->second];
}//book是存放所有书籍的vector
it++;
begin = it;
}
}
//利用作者进行模糊查找
void caozuo::c2_bookzuozhe(string s) {
multimap<string, int>::iterator it, p1, p2, p, begin;
p1 = book_zuozhe.begin();
p2 = book_zuozhe.end();
begin = p1;
for (p = p1; p != p2; p++)
{
it = find_if(begin, book_zuozhe.end(), map_value_finder(s));
if (it == book_zuozhe.end()) {
return;
}
else {
cout << book_v[it->second];
}
it++;
begin = it;
}
}
//重构map
void caozuo::new_bookmap() {
vector<books>::iterator it;
book_name.clear();
book_nums.clear();
book_zuozhe.clear();
book_press.clear();
book_size.clear();
book_date.clear();
int side = 0;
for (it = book_v.begin(); it < book_v.end(); it++) {
side = it - book_v.begin();
book_nums.insert(make_pair(it->get_nums(), side));
book_name.insert(make_pair(it->get_name(), side));
book_zuozhe.insert(make_pair(it->get_zuozhe(), side));
book_press.insert(make_pair(it->get_press(), side));
book_size.insert(make_pair(it->get_book_size(), side));
book_date.insert(make_pair(it->get_date(), side));
}
}
//修改操作
void caozuo::xiu_booknums(string nums,books book) {
multimap<string, int>::iterator it;
it = book_nums.find(nums);
if (it == book_nums.end()) {
cout << -1 << endl;
return;
}
else {
book_v[it->second] = book;
new_bookmap();
}
}
//删除操作
void caozuo::del_book(string nums) {
multimap<string, int>::iterator it;
it = book_nums.find(nums);
if (it == book_nums.end()) {
cout << -1 << endl;
return;
}
else {
int index = it->second;
book_v.erase(book_v.begin() +index );
new_bookmap();
}
}
//测试类
class test {
//测试数据
public:
void test_date1();
void test_max();
void test_book();
void test_caozuo_add();
void test_caozuo_find1();
void test_caozuo_find2();
void test_date2();
void test_newmap();
};
void test:: test_date1() {
date p(2020, 10, 5);
cout << p.get_year() << endl;
cout << p.get_month() << endl;
cout << p.get_day() << endl;
date a;
cin >> a;
cout << a;
}
void test::test_max() {
max_books p(500);
cout << p.get_booknums() << endl;
max_data q(500);
cout << q.get_datanums() << endl;
}
void test::test_book() {
date a(2020, 10, 5);
books p("5", "钟一淼的一生", "钟洋", "山东出版社", "文学", 500, 200, a);
cout << p.get_name() << endl;
cout << p.get_nums() << endl;
cout << p.get_press() << endl;
cout << p.get_zuozhe() << endl;
cout << p.get_book_size() << endl;
cout << p.get_sum_books() << endl;
cout << p.get_now_books() << endl;
}
void test::test_caozuo_add() {
caozuo p;
date b1(2020, 2, 10);
date b2(2021, 2, 10);
date b3(2020, 2, 10);
date b4(2020, 5, 4);
date b5(2019, 2, 10);
books a1("9789308021395","数学建模","司守奎","浙江出版社","数学",500,200,b1);
books a2("9787512429273", "Matlab数学建模方法", "卓金武", "北京航空航天大学出版社", "数学", 500, 200,b2);
books a3("9789308021395", "数学建模", "司守奎", "浙江出版社", "数学", 500, 200, b3);
books a4("9787532772622", "枪炮,病菌与钢铁", "贾雷德", "上海译文出版社", "历史", 500, 200, b4);
books a5("9787118113686", "LINGO软件以及应用", "司守奎", "国防工业出版社", "计算机", 500, 200, b5);
//books b;
//cin >> a;
////cin >> b;
p.book_add(a1);
p.book_add(a2);
p.book_add(a3);
p.book_add(a4);
p.book_add(a5);
//p.c2_bookname("钟");
//模糊查找
//p.c2_bookname("钟一淼");
p.c2_bookzuozhe("钟洋1");
}
void test::test_caozuo_find1() {
caozuo p;
p.c_booknums("9789308021395");
p.c1_bookname("Matlab数学建模方法");
p.c1_booksize("数学");
p.c1_bookzuozhe("司守奎");
}
void test::test_caozuo_find2() {
caozuo p;
p.c2_bookname("数学");
p.c2_bookzuozhe("司");
}
void test::test_date2() {
caozuo p;
date b(2020, 2, 10);
//cout << "*****" << endl;
p.c1_date(b);
date a(2019, 2, 10);
p.c1_date(a);
p.c2_date(a, b);
}
void test::test_newmap() {
caozuo p;
date a(2020, 10, 6);
books q("9787512429273", "Matlab数学建模方法555555", "卓金武", "北京航空航天大学出版社", "数学", 500, 200,a);
p.xiu_booknums("9787512429273", q);
p.del_book("9787532772622");
}
int main()
{
//test p;
//test_max();
//test_date();
//p.test_book();
//test_date2();
//caozuo p;
//test_newmap();
test p;
p.test_caozuo_add();
p.test_caozuo_find1();
p.test_caozuo_find2();
p.test_date2();
p.test_newmap();
}