avr39ripe

cppGittAndriyLibraryStruct

Jun 22nd, 2021
625
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string.h>
  3.  
  4. struct Book
  5. {
  6.     char* title;
  7.     char* author;
  8.     char* publisher;
  9.     char* genre;
  10.  
  11.     void init(const char* titleP, const char* authorP, const char* publisherP, const char* genreP)
  12.     {
  13.         auto strLen{ strlen(titleP) + 1 };
  14.         title = new char[strLen];
  15.         strcpy_s(title, strLen, titleP);
  16.  
  17.         strLen = (strlen(authorP) + 1);
  18.         author = new char[strLen];
  19.         strcpy_s(author, strLen, authorP);
  20.  
  21.         strLen = (strlen(publisherP) + 1);
  22.         publisher = new char[strLen];
  23.         strcpy_s(publisher, strLen, publisherP);
  24.  
  25.         strLen = (strlen(genreP) + 1);
  26.         genre = new char[strLen];
  27.         strcpy_s(genre, strLen, genreP);
  28.     }
  29.  
  30.     void print()
  31.     {
  32.         std::cout << "Title: " << title << "\tAuthor: " << author
  33.             << "\tPublisher: " << publisher << "\tGenre: " << genre << '\n';
  34.     }
  35.  
  36.     void clean()
  37.     {
  38.         delete[] title;
  39.         delete[] author;
  40.         delete[] publisher;
  41.         delete[] genre;
  42.     }
  43. };
  44.  
  45. void listLibrary(Book* begin, Book* end)
  46. {
  47.     for (auto library{ begin }; library != end; ++library)
  48.     {
  49.         library->print();
  50.     }
  51. }
  52.  
  53. void cleanLibrary(Book* library, size_t librarySize)
  54. {
  55.     for (size_t i{ 0 }; i < librarySize; ++i)
  56.     {
  57.         library[i].clean();
  58.     }
  59. }
  60.  
  61. void librarySearch(Book* begin, Book* end, char* found, bool(*searchCriterion)(const Book& libraryS, char* found))
  62. {
  63.     for (auto library{ begin }; library != end; ++library)
  64.     {
  65.         if (searchCriterion(*library, found))
  66.         {
  67.             library->print();
  68.         }
  69.     }
  70. }
  71.  
  72. bool searchByTitle(const Book& library, char* found)
  73. {
  74.     return strstr(library.title, found);
  75. }
  76.  
  77. bool searchByAuthor(const Book& library, char* found)
  78. {
  79.     return strstr(library.author, found);
  80. }
  81.  
  82. void librarySort(Book* library, size_t librarySize, bool(*criteria)(const Book& libraryA, const Book& libraryB))
  83. {
  84.     Book tmp;
  85.     for (size_t head{ 0 }; head < librarySize; ++head)
  86.     {
  87.         for (size_t tail{ librarySize - 1 }; tail > head; --tail)
  88.         {
  89.             if (criteria(library[tail], library[head]))
  90.             {
  91.                 tmp = library[tail];
  92.                 library[tail] = library[head];
  93.                 library[head] = tmp;
  94.             }
  95.         }
  96.     }
  97. }
  98.  
  99. bool libraryByTitle(const Book& libraryA, const Book& libraryB)
  100. {
  101.     return strcmp(libraryA.title, libraryB.title) < 0;
  102. }
  103.  
  104. bool libraryByAuthor(const Book& libraryA, const Book& libraryB)
  105. {
  106.     return strcmp(libraryA.author, libraryB.author) < 0;
  107. }
  108.  
  109. bool libraryByPublisher(const Book& libraryA, const Book& libraryB)
  110. {
  111.     return strcmp(libraryA.publisher, libraryB.publisher) < 0;
  112. }
  113.  
  114. template <typename T>
  115. void pastInArr(T*& Arr, size_t& arrSize, size_t idxPastElem)
  116. {
  117.  
  118.     T* newArr{ new T[arrSize + 1] {} };
  119.     for (size_t i{ 0 }; i < arrSize; ++i)
  120.     {
  121.         *(newArr + i + (i >= idxPastElem)) = *(Arr + i);
  122.     }
  123.     delete[] Arr;
  124.     Arr = newArr;
  125.     arrSize++;
  126. }
  127.  
  128. template <typename T>
  129. void cutFromArr(T*& Arr, size_t& arrSize, size_t idxCutElem)
  130. {
  131.     T* newArr{ new T[arrSize--] { } };
  132.     for (size_t i{ 0 }; i < arrSize; ++i)
  133.     {
  134.         *(newArr + i) = *(Arr + i + (i >= idxCutElem));
  135.     }
  136.     delete[]Arr;
  137.     Arr = newArr;
  138. }
  139.  
  140. int main()
  141. {
  142.     size_t librarySize{ 10 };
  143.     Book* library{ new Book[librarySize]{nullptr} };
  144.  
  145.     library[0].init("Slidy na dorozi", "Valery Ananiev", "Nova knyga", "Novel");
  146.     library[1].init("Khaki", "Saigon", "Bilka", "Novel");
  147.     library[2].init("Mozambik", "Saigon, Martin Brest", "Bilka", "Novel");
  148.     library[3].init("72", "Oleksander Vdovychenko", "Bilka", "Prose");
  149.     library[4].init("Psyhi dvuh morey", "Yriy Rudenko", "DIPA", "Novel");
  150.     library[5].init("Svet i kyrpich", "Yriy Rudenko", "DIPA", "Novel");
  151.     library[6].init("Zemlya lishnyh", "Andrey Kruz", "Eksmo", "Fiction");
  152.     library[7].init("Zemly Haitany", "Artem Kamenystiy", "Alfa knyga", "Fantasy");
  153.     library[8].init("Kobzar", "Taras Shevchenko", "Shkola Kharkyv", "Poetry");
  154.     library[9].init("Marusya Churay", "Lina Kostenko", "A-ba-ba-ga-la-ma-ga", "Novel");
  155.  
  156.     auto libraryEnd{ library + librarySize };
  157.  
  158.     listLibrary(library, libraryEnd);
  159.  
  160.     char found[]{ "kyrpich" };
  161.     std::cout << "\nSearch by Title:\n";
  162.     librarySearch(library, libraryEnd, found, searchByTitle);
  163.  
  164.     char found2[]{ "Saigon" };
  165.     std::cout << "\nSearch by Author:\n";
  166.     librarySearch(library, libraryEnd, found2, searchByAuthor);
  167.  
  168.     librarySort(library, librarySize, libraryByTitle);
  169.     std::cout << "\nSorted by Title:\n";
  170.     listLibrary(library, libraryEnd);
  171.  
  172.     librarySort(library, librarySize, libraryByAuthor);
  173.     std::cout << "\nSorted by Author:\n";
  174.     listLibrary(library, libraryEnd);
  175.  
  176.     librarySort(library, librarySize, libraryByPublisher);
  177.     std::cout << "\nSorted by Publisher:\n";
  178.     listLibrary(library, libraryEnd);
  179.  
  180.     std::cout << "\nAdd book in library :\n";
  181.     pastInArr(library, librarySize, 4);
  182.     library[4].init("Sherlock", "Arthur Conan Doyle", "BBC Books", "Novel");
  183.     listLibrary(library, library + librarySize);
  184.  
  185.     std::cout << "\nDelete book from library :\n";
  186.     cutFromArr(library, librarySize, 4);
  187.     listLibrary(library, library + librarySize);
  188.  
  189.     cleanLibrary(library, librarySize);
  190.     delete[]library;
  191.  
  192.     return 0;
  193. }
RAW Paste Data