Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <regex>
- #include <algorithm>
- #include <map>
- using namespace std;
- struct Book {
- string name;
- string genre;
- unsigned rating;
- string tostr(const string& delim = " "s) {
- return name + delim + genre + delim + to_string(rating);
- }
- };
- class Books {
- public:
- Books(const Books&) = delete;
- Books(Books&&) = delete;
- Books& operator=(const Books&) = delete;
- Books& operator=(Books&&) = delete;
- Books() : buffer_(16U), count_(0U), books_(new Book[buffer_]) {}
- ~Books() {
- read_db_.is_open() ? read_db_.close() : void();
- this->save(db_path_);
- if (books_ != nullptr) {
- delete[] books_;
- books_ = nullptr;
- }
- }
- Book operator[](int index)const { return books_[index]; }
- void add(const Book& book) {
- if (count_ == buffer_) {
- auto books = new Book[buffer_ << 1U];
- for (auto i = 0U; i < count_; ++i) books[i] = books_[i];
- delete[] books_;
- books_ = books;
- }
- books_[count_] = book;
- ++count_;
- }
- bool load(const string& db_path) {
- read_db_.open(db_path);
- const regex re{ "#" };
- string recording;
- tuple<string, string, int> tmp;
- Book book;
- if (read_db_.is_open()) {
- this->clear_();
- while (getline(read_db_, recording)) {
- regex_token_iterator iter{ recording.begin(), recording.end(), re, -1 };
- book.name = iter->str();
- book.genre = (++iter)->str();
- book.rating = stoul((++iter)->str());
- this->add(book);
- }
- read_db_.close();
- db_path_ = db_path;
- return true;
- }
- return false;
- }
- void save(const string& db_path = ""s) {
- db_path_ = db_path.empty() ? db_path_ : db_path;
- write_db_.open(db_path_);
- if (write_db_.is_open()) {
- for (auto i = 0U; i < count_; ++i) write_db_ << books_[i].tostr("#"s) << '\n';
- write_db_.close();
- puts("Данные успешно сохранены!");
- } else {
- puts("Программе не удалось сохранить данные!");
- }
- }
- void show() {
- auto id = 0U;
- for (auto i = 0U; i < count_; ++i) cout << ++id << ". " << books_[i].tostr("; "s) << '\n';
- }
- map<unsigned, string, greater<>> interest() {
- map<string, unsigned> box;
- for (auto i = 0U; i < count_; ++i) {
- auto [genre, rating] = make_pair(books_[i].genre, books_[i].rating);
- box[genre] += rating;
- }
- map<unsigned, string, greater<>> reverse;
- for (const auto& [key, value] : box) reverse[value] = key;
- return reverse;
- }
- private:
- size_t buffer_;
- size_t count_;
- Book* books_;
- string db_path_;
- ifstream read_db_;
- ofstream write_db_;
- void clear_() {
- count_ = 0;
- }
- };
- int main() {
- system("chcp 1251 > nul");
- Books books;
- books.add({ "«Гроза», А. Н. Островского", "трагедия", 25 });
- books.add({ "«Идеалы» Ф фон Шиллер", "элегия", 11 });
- books.add({ "«Король забавляется», В. М. Гюго", "драма", 42 });
- books.add({ "«Понедельник начинается в субботу», А. и Б. Стругацкие", "комедия", 49 });
- books.add({ "«Василий Тёркин», А. Твардовский", "поэма", 65 });
- books.add({ "«Преступление и наказание», Ф. М. Достоевский", "роман", 51 });
- books.add({ "«Как важно быть серьезным», О. Уальд", "комедия", 58 });
- books.add({ "«Божественная комедия», А. Данте", "поэма", 27 });
- books.add({ "«Гамлет», У. Шекспир", "трагедия", 56 });
- books.add({ "«Юлия, или Новая Элоиза», Ж.-Ж. Руссо", "роман", 17 });
- const auto db_path = "books_db.txt"s;
- books.save(db_path);
- books.load(db_path);
- puts("\n\tБаза данных КНИГИ\n");
- books.show();
- puts("\n\tЖанровый рейтинг\n");
- auto interest = books.interest();
- for (const auto& [rating, genre] : interest) cout << genre << " = " << rating << '\n';
- system("pause > nul");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement