Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- void delete_char_array(char*& str){
- delete[] str;
- str = nullptr;
- }
- void copy_char_array(const char* input, char*& output){
- if (output) delete_char_array(output);
- if (!input) {
- output = nullptr;
- return;
- }
- int sz_input = strlen(input);
- output = new char[sz_input + 1];
- strcpy(output, input);
- }
- class Author{
- public:
- Author(const int _age = 0): name(nullptr), last_name(nullptr), email(nullptr), age(_age){}
- Author(const char* _name, const char* _last_name, const char* _email, const unsigned int _age):
- Author(_age){
- copy_char_array(_name, name);
- copy_char_array(_last_name, last_name);
- copy_char_array(_email, email);
- }
- Author(const Author& _other):Author(_other.age){
- copy_char_array(_other.name, name);
- copy_char_array(_other.last_name, last_name);
- copy_char_array(_other.email, email);
- }
- Author& operator=(const Author& _other){
- if (this == &_other) return *this;
- copy_char_array(_other.name, name);
- copy_char_array(_other.last_name, last_name);
- copy_char_array(_other.email, email);
- return *this;
- }
- ~Author(){
- if (name){
- delete []name;
- name = nullptr;
- }
- if (last_name){
- delete []last_name;
- last_name = nullptr;
- }
- if (email){
- delete []email;
- email = nullptr;
- }
- }
- void print_info(){
- cout << name << "\n" << last_name << "\n" << email << "\n" << age << "\n";
- }
- const char* get_name() const {return name;}
- void set_name(const char* _name){
- copy_char_array(_name, name);
- }
- const char* get_last_name() const {return last_name;}
- void set_last_name(const char* _last_name){
- copy_char_array(_last_name, last_name);
- }
- const char* get_email() const {return email;}
- void set_email(const char* _email){
- copy_char_array(_email, email);
- }
- const int get_age() const {return age;}
- void set_age(const unsigned int _age){
- age = _age;
- }
- friend ostream& operator<<(ostream &os, const Author &author);
- private:
- char* name;
- char* last_name;
- char* email;
- unsigned int age;
- };
- ostream& operator<<(ostream &os, const Author &author){
- return os << "Author's name: " << author.name << "\nAuthor's last name: " << author.last_name
- << "\nAuthor's email: " << author.email << "\nAuthor's age: " << author.age;
- }
- class Book{
- public:
- Book(const double _price = 0, const int _number_of_authors = 0): author(nullptr), name(nullptr), text(nullptr),
- price(_price), number_of_authors(_number_of_authors){}
- Book(const Author** _author, const char* _name, const char* _text, const double _price, const int _number_of_authors):
- Book(_price, _number_of_authors){
- /*author = new Author* [number_of_authors];
- for (int i = 0; i < number_of_authors; ++i){
- author[i] = new Author(*_author[i]);
- }*/
- copy_authors(_author, _number_of_authors);
- copy_char_array(_name, name);
- copy_char_array(_text, text);
- }
- Book(const Book& _other):Book(const_cast<const Author**> (_other.author), _other.name, _other.text, _other.price, _other.number_of_authors){}
- ~Book(){
- delete_char_array(name);
- delete_char_array(text);
- for (int i = 0; i < number_of_authors; ++i){
- delete[] author[i];
- author[i] = nullptr;
- }
- delete[] author;
- }
- Book& operator=(const Book &_other){
- if (this == &_other) return *this;
- copy_char_array(_other.name, name);
- copy_char_array(_other.text, text);
- copy_authors(const_cast<const Author**>(_other.author), _other.number_of_authors);
- price = _other.price;
- return *this;
- }
- const Author* const* get_authors() const {
- return author;
- }
- const int get_number_of_authors(){
- return number_of_authors;
- }
- const char* get_name(){
- return name;
- }
- const char* get_text(){
- return text;
- }
- const double get_price(){
- return price;
- }
- void set_new_price(const double _price){
- price = _price;
- }
- bool is_there_author_named(const char* compare_name, const char* compare_last_name){
- for (int i = 0; i < number_of_authors; ++i)
- if (strcmp(author[i]->get_name(), compare_name) == 0 &&
- strcmp(author[i]->get_last_name(), compare_last_name) == 0)
- return true;
- return false;
- }
- void print_text(ostream& os = cout){
- os << "The text of the book named: \"" << name << "\n";
- os << text << "\n";
- }
- friend ostream& operator<<(ostream& os, const Book &book);
- private:
- void copy_authors(const Author** _author_input, int _number_of_authors){
- number_of_authors = _number_of_authors;
- author = new Author* [number_of_authors];
- for (int i = 0; i < number_of_authors; ++i){
- author[i] = new Author(*_author_input[i]);
- }
- }
- Author** author;
- char* name;
- char* text;
- double price;
- int number_of_authors;
- };
- ostream& operator<<(ostream& os, const Book &book){
- os << "Book's name: " << book.name << "\n";
- os << "Number of authors: " << book.number_of_authors << "\n";
- os << "Book's authors:\n";
- for (int i = 0; i < book.number_of_authors; ++i)
- os << book.author[i] << "\n";
- os << "Book's text:\n";
- os << book.text << "\n";
- return os << "Book's price: " << book.price << endl;
- }
- struct BookCountPair{
- Book book;
- unsigned int count;
- BookCountPair(){}
- BookCountPair(const Book _book, const unsigned int _count): book(_book), count(_count){}
- BookCountPair(const BookCountPair &_other): BookCountPair(_other.book, _other.count){}
- };
- class Library{
- public:
- Library(const unsigned int _number_of_different_books = 0): book_pair(nullptr), number_of_different_books(_number_of_different_books){}
- Library(const BookCountPair** _book_pair, const unsigned int _number_of_different_books): Library(_number_of_different_books){
- copy_book_pairs(_book_pair, book_pair,_number_of_different_books);
- }
- ~Library(){
- for (int i = 0; i < number_of_different_books; ++i){
- delete[] book_pair[i];
- book_pair[i] = nullptr;
- }
- delete[] book_pair;
- }
- int operator[](const char* searched_name){
- for (int i = 0; i < number_of_different_books; ++i){
- if (strcmp(book_pair[i]->book.get_name(), searched_name) == 0)
- return book_pair[i]->count;
- }
- return 0;
- }
- void sort_books(){
- sort(book_pair, book_pair + number_of_different_books, compare_books);
- }
- bool is_a_book_available(const char* reqested_book_name){
- for (int i = 0; i < number_of_different_books; ++i)
- if (strcmp(book_pair[i]->book.get_name(), reqested_book_name) == 0){
- cout << "The book is available\n";
- return true;
- }
- return false;
- }
- void get_a_book_from_the_library(const char* requested_book_name){
- for (int i = 0; i < number_of_different_books; ++i)
- if (strcmp(book_pair[i]->book.get_name(), requested_book_name) == 0){
- if (book_pair[i]->count > 0){
- cout << "There are copies of the book in library. Here's yours!\n";
- book_pair[i]->count--;
- }
- else cout << "At the moment there are no available copies of the book in the library\n";
- }
- cout << "The book is not available in the library\n";
- }
- const Book** get_all_the_books_with_a_given_author(const Author& author, int &get_list_sz){
- Book** book_list = nullptr;
- int list_sz = 0;
- for (int i = 0; i < number_of_different_books; ++i)
- if (book_pair[i]->book.is_there_author_named(author.get_name(), author.get_last_name())){
- add_book_to_the_list(book_list, list_sz, book_pair[i]->book);
- list_sz++;
- }
- get_list_sz = list_sz;
- return const_cast<const Book**>(book_list);
- }
- private:
- static bool compare_books(BookCountPair *a, BookCountPair *b){
- return strcmp( a->book.get_name(), b->book.get_name()) <= 0;
- }
- void add_book_to_the_list(Book** &list, int list_sz, const Book& book){
- int new_sz = list_sz + 1;
- Book** new_list = new Book* [new_sz];
- for (int i = 0; i < list_sz; ++i)
- new_list[i] = new Book(*list[i]);
- new_list[list_sz] = new Book(book);
- for (int i = 0; i < list_sz; ++i)
- delete list[i];
- delete[] list;
- list = new_list;
- }
- void copy_book_pairs(const BookCountPair** from, BookCountPair** &book_pair, const unsigned int _number_of_different_books){
- number_of_different_books = _number_of_different_books;
- book_pair = new BookCountPair* [number_of_different_books];
- for (int i = 0; i < number_of_different_books; ++i)
- book_pair[i] = new BookCountPair(*from[i]);
- }
- BookCountPair** book_pair;
- unsigned int number_of_different_books;
- };
- int main(){
- char name[64] = "aleks";
- char last_name[64] = "ivanov";
- char email[64] = "[email protected]";
- unsigned int age = 42;
- Author a(name, last_name, email, age);
- //strcpy(name, a.get_name());
- //strcpy(last_name, a.get_last_name());
- //strcpy(email, a.get_email());
- //cout << name << "\n" << last_name << "\n" << email << "\n" << age << "\n";
- cout << a << endl;
- Author b(a);
- a.set_name("RadoShisharkata");
- cout << a << endl;
- cout << b << endl;
- Author c;
- c = b;
- b.set_name("RadoShisharkata2");
- cout << b << "\n";
- cout << c << "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment