Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- struct Book
- {
- char* title;
- char* author;
- char* publisher;
- char* genre;
- void init(const char* titleP, const char* authorP, const char* publisherP, const char* genreP)
- {
- auto strLen{ strlen(titleP) + 1 };
- title = new char[strLen];
- strcpy_s(title, strLen, titleP);
- strLen = (strlen(authorP) + 1);
- author = new char[strLen];
- strcpy_s(author, strLen, authorP);
- strLen = (strlen(publisherP) + 1);
- publisher = new char[strLen];
- strcpy_s(publisher, strLen, publisherP);
- strLen = (strlen(genreP) + 1);
- genre = new char[strLen];
- strcpy_s(genre, strLen, genreP);
- }
- void print()
- {
- std::cout << "Title: " << title << "\tAuthor: " << author
- << "\tPublisher: " << publisher << "\tGenre: " << genre << '\n';
- }
- void clean()
- {
- delete[] title;
- delete[] author;
- delete[] publisher;
- delete[] genre;
- }
- };
- void listLibrary(Book* begin, Book* end)
- {
- for (auto library{ begin }; library != end; ++library)
- {
- library->print();
- }
- }
- void cleanLibrary(Book* library, size_t librarySize)
- {
- for (size_t i{ 0 }; i < librarySize; ++i)
- {
- library[i].clean();
- }
- }
- void librarySearch(Book* begin, Book* end, char* found, bool(*searchCriterion)(const Book& libraryS, char* found))
- {
- for (auto library{ begin }; library != end; ++library)
- {
- if (searchCriterion(*library, found))
- {
- library->print();
- }
- }
- }
- bool searchByTitle(const Book& library, char* found)
- {
- return strstr(library.title, found);
- }
- bool searchByAuthor(const Book& library, char* found)
- {
- return strstr(library.author, found);
- }
- void librarySort(Book* library, size_t librarySize, bool(*criteria)(const Book& libraryA, const Book& libraryB))
- {
- Book tmp;
- for (size_t head{ 0 }; head < librarySize; ++head)
- {
- for (size_t tail{ librarySize - 1 }; tail > head; --tail)
- {
- if (criteria(library[tail], library[head]))
- {
- tmp = library[tail];
- library[tail] = library[head];
- library[head] = tmp;
- }
- }
- }
- }
- bool libraryByTitle(const Book& libraryA, const Book& libraryB)
- {
- return strcmp(libraryA.title, libraryB.title) < 0;
- }
- bool libraryByAuthor(const Book& libraryA, const Book& libraryB)
- {
- return strcmp(libraryA.author, libraryB.author) < 0;
- }
- bool libraryByPublisher(const Book& libraryA, const Book& libraryB)
- {
- return strcmp(libraryA.publisher, libraryB.publisher) < 0;
- }
- template <typename T>
- void pastInArr(T*& Arr, size_t& arrSize, size_t idxPastElem)
- {
- T* newArr{ new T[arrSize + 1] {} };
- for (size_t i{ 0 }; i < arrSize; ++i)
- {
- *(newArr + i + (i >= idxPastElem)) = *(Arr + i);
- }
- delete[] Arr;
- Arr = newArr;
- arrSize++;
- }
- template <typename T>
- void cutFromArr(T*& Arr, size_t& arrSize, size_t idxCutElem)
- {
- T* newArr{ new T[arrSize--] { } };
- for (size_t i{ 0 }; i < arrSize; ++i)
- {
- *(newArr + i) = *(Arr + i + (i >= idxCutElem));
- }
- delete[]Arr;
- Arr = newArr;
- }
- int main()
- {
- size_t librarySize{ 10 };
- Book* library{ new Book[librarySize]{nullptr} };
- library[0].init("Slidy na dorozi", "Valery Ananiev", "Nova knyga", "Novel");
- library[1].init("Khaki", "Saigon", "Bilka", "Novel");
- library[2].init("Mozambik", "Saigon, Martin Brest", "Bilka", "Novel");
- library[3].init("72", "Oleksander Vdovychenko", "Bilka", "Prose");
- library[4].init("Psyhi dvuh morey", "Yriy Rudenko", "DIPA", "Novel");
- library[5].init("Svet i kyrpich", "Yriy Rudenko", "DIPA", "Novel");
- library[6].init("Zemlya lishnyh", "Andrey Kruz", "Eksmo", "Fiction");
- library[7].init("Zemly Haitany", "Artem Kamenystiy", "Alfa knyga", "Fantasy");
- library[8].init("Kobzar", "Taras Shevchenko", "Shkola Kharkyv", "Poetry");
- library[9].init("Marusya Churay", "Lina Kostenko", "A-ba-ba-ga-la-ma-ga", "Novel");
- auto libraryEnd{ library + librarySize };
- listLibrary(library, libraryEnd);
- char found[]{ "kyrpich" };
- std::cout << "\nSearch by Title:\n";
- librarySearch(library, libraryEnd, found, searchByTitle);
- char found2[]{ "Saigon" };
- std::cout << "\nSearch by Author:\n";
- librarySearch(library, libraryEnd, found2, searchByAuthor);
- librarySort(library, librarySize, libraryByTitle);
- std::cout << "\nSorted by Title:\n";
- listLibrary(library, libraryEnd);
- librarySort(library, librarySize, libraryByAuthor);
- std::cout << "\nSorted by Author:\n";
- listLibrary(library, libraryEnd);
- librarySort(library, librarySize, libraryByPublisher);
- std::cout << "\nSorted by Publisher:\n";
- listLibrary(library, libraryEnd);
- std::cout << "\nAdd book in library :\n";
- pastInArr(library, librarySize, 4);
- library[4].init("Sherlock", "Arthur Conan Doyle", "BBC Books", "Novel");
- listLibrary(library, library + librarySize);
- std::cout << "\nDelete book from library :\n";
- cutFromArr(library, librarySize, 4);
- listLibrary(library, library + librarySize);
- cleanLibrary(library, librarySize);
- delete[]library;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment