STEFAN_STOYANOV

zadacha ot 26.03.2025

Apr 2nd, 2025
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.96 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void delete_char_array(char*& str){
  5. delete[] str;
  6. str = nullptr;
  7. }
  8.  
  9. void copy_char_array(const char* input, char*& output){
  10. if (output) delete_char_array(output);
  11. if (!input) {
  12. output = nullptr;
  13. return;
  14. }
  15. int sz_input = strlen(input);
  16. output = new char[sz_input + 1];
  17. strcpy(output, input);
  18. }
  19.  
  20. class Author{
  21. public:
  22.  
  23. Author(const int _age = 0): name(nullptr), last_name(nullptr), email(nullptr), age(_age){}
  24.  
  25. Author(const char* _name, const char* _last_name, const char* _email, const unsigned int _age):
  26. Author(_age){
  27. copy_char_array(_name, name);
  28. copy_char_array(_last_name, last_name);
  29. copy_char_array(_email, email);
  30. }
  31.  
  32. Author(const Author& _other):Author(_other.age){
  33. copy_char_array(_other.name, name);
  34. copy_char_array(_other.last_name, last_name);
  35. copy_char_array(_other.email, email);
  36. }
  37.  
  38. Author& operator=(const Author& _other){
  39. if (this == &_other) return *this;
  40. copy_char_array(_other.name, name);
  41. copy_char_array(_other.last_name, last_name);
  42. copy_char_array(_other.email, email);
  43. return *this;
  44. }
  45.  
  46. ~Author(){
  47. if (name){
  48. delete []name;
  49. name = nullptr;
  50. }
  51. if (last_name){
  52. delete []last_name;
  53. last_name = nullptr;
  54. }
  55. if (email){
  56. delete []email;
  57. email = nullptr;
  58. }
  59. }
  60.  
  61. void print_info(){
  62. cout << name << "\n" << last_name << "\n" << email << "\n" << age << "\n";
  63. }
  64.  
  65. const char* get_name() const {return name;}
  66. void set_name(const char* _name){
  67. copy_char_array(_name, name);
  68. }
  69.  
  70. const char* get_last_name() const {return last_name;}
  71. void set_last_name(const char* _last_name){
  72. copy_char_array(_last_name, last_name);
  73. }
  74.  
  75. const char* get_email() const {return email;}
  76. void set_email(const char* _email){
  77. copy_char_array(_email, email);
  78. }
  79.  
  80. const int get_age() const {return age;}
  81. void set_age(const unsigned int _age){
  82. age = _age;
  83. }
  84.  
  85. friend ostream& operator<<(ostream &os, const Author &author);
  86.  
  87. private:
  88.  
  89. char* name;
  90. char* last_name;
  91. char* email;
  92. unsigned int age;
  93. };
  94.  
  95. ostream& operator<<(ostream &os, const Author &author){
  96. return os << "Author's name: " << author.name << "\nAuthor's last name: " << author.last_name
  97. << "\nAuthor's email: " << author.email << "\nAuthor's age: " << author.age;
  98. }
  99.  
  100. class Book{
  101. public:
  102. Book(const double _price = 0, const int _number_of_authors = 0): author(nullptr), name(nullptr), text(nullptr),
  103. price(_price), number_of_authors(_number_of_authors){}
  104.  
  105. Book(const Author** _author, const char* _name, const char* _text, const double _price, const int _number_of_authors):
  106. Book(_price, _number_of_authors){
  107. /*author = new Author* [number_of_authors];
  108. for (int i = 0; i < number_of_authors; ++i){
  109. author[i] = new Author(*_author[i]);
  110. }*/
  111. copy_authors(_author, _number_of_authors);
  112. copy_char_array(_name, name);
  113. copy_char_array(_text, text);
  114. }
  115.  
  116. Book(const Book& _other):Book(const_cast<const Author**> (_other.author), _other.name, _other.text, _other.price, _other.number_of_authors){}
  117. ~Book(){
  118. delete_char_array(name);
  119. delete_char_array(text);
  120. for (int i = 0; i < number_of_authors; ++i){
  121. delete[] author[i];
  122. author[i] = nullptr;
  123. }
  124. delete[] author;
  125. }
  126.  
  127. Book& operator=(const Book &_other){
  128. if (this == &_other) return *this;
  129. copy_char_array(_other.name, name);
  130. copy_char_array(_other.text, text);
  131. copy_authors(const_cast<const Author**>(_other.author), _other.number_of_authors);
  132. price = _other.price;
  133. return *this;
  134. }
  135.  
  136. const Author* const* get_authors() const {
  137. return author;
  138. }
  139.  
  140. const int get_number_of_authors(){
  141. return number_of_authors;
  142. }
  143.  
  144. const char* get_name(){
  145. return name;
  146. }
  147.  
  148. const char* get_text(){
  149. return text;
  150. }
  151.  
  152. const double get_price(){
  153. return price;
  154. }
  155.  
  156. void set_new_price(const double _price){
  157. price = _price;
  158. }
  159.  
  160. bool is_there_author_named(const char* compare_name, const char* compare_last_name){
  161. for (int i = 0; i < number_of_authors; ++i)
  162. if (strcmp(author[i]->get_name(), compare_name) == 0 &&
  163. strcmp(author[i]->get_last_name(), compare_last_name) == 0)
  164. return true;
  165. return false;
  166. }
  167.  
  168. void print_text(ostream& os = cout){
  169. os << "The text of the book named: \"" << name << "\n";
  170. os << text << "\n";
  171. }
  172.  
  173. friend ostream& operator<<(ostream& os, const Book &book);
  174.  
  175. private:
  176. void copy_authors(const Author** _author_input, int _number_of_authors){
  177. number_of_authors = _number_of_authors;
  178. author = new Author* [number_of_authors];
  179. for (int i = 0; i < number_of_authors; ++i){
  180. author[i] = new Author(*_author_input[i]);
  181. }
  182. }
  183. Author** author;
  184. char* name;
  185. char* text;
  186. double price;
  187. int number_of_authors;
  188. };
  189.  
  190. ostream& operator<<(ostream& os, const Book &book){
  191. os << "Book's name: " << book.name << "\n";
  192. os << "Number of authors: " << book.number_of_authors << "\n";
  193. os << "Book's authors:\n";
  194. for (int i = 0; i < book.number_of_authors; ++i)
  195. os << book.author[i] << "\n";
  196. os << "Book's text:\n";
  197. os << book.text << "\n";
  198. return os << "Book's price: " << book.price << endl;
  199. }
  200.  
  201. struct BookCountPair{
  202. Book book;
  203. unsigned int count;
  204. BookCountPair(){}
  205. BookCountPair(const Book _book, const unsigned int _count): book(_book), count(_count){}
  206. BookCountPair(const BookCountPair &_other): BookCountPair(_other.book, _other.count){}
  207. };
  208.  
  209. class Library{
  210. public:
  211. Library(const unsigned int _number_of_different_books = 0): book_pair(nullptr), number_of_different_books(_number_of_different_books){}
  212. Library(const BookCountPair** _book_pair, const unsigned int _number_of_different_books): Library(_number_of_different_books){
  213. copy_book_pairs(_book_pair, book_pair,_number_of_different_books);
  214. }
  215.  
  216. ~Library(){
  217. for (int i = 0; i < number_of_different_books; ++i){
  218. delete[] book_pair[i];
  219. book_pair[i] = nullptr;
  220. }
  221. delete[] book_pair;
  222. }
  223.  
  224. int operator[](const char* searched_name){
  225. for (int i = 0; i < number_of_different_books; ++i){
  226. if (strcmp(book_pair[i]->book.get_name(), searched_name) == 0)
  227. return book_pair[i]->count;
  228. }
  229. return 0;
  230. }
  231.  
  232. void sort_books(){
  233. sort(book_pair, book_pair + number_of_different_books, compare_books);
  234. }
  235.  
  236. bool is_a_book_available(const char* reqested_book_name){
  237. for (int i = 0; i < number_of_different_books; ++i)
  238. if (strcmp(book_pair[i]->book.get_name(), reqested_book_name) == 0){
  239. cout << "The book is available\n";
  240. return true;
  241. }
  242. return false;
  243. }
  244.  
  245. void get_a_book_from_the_library(const char* requested_book_name){
  246. for (int i = 0; i < number_of_different_books; ++i)
  247. if (strcmp(book_pair[i]->book.get_name(), requested_book_name) == 0){
  248. if (book_pair[i]->count > 0){
  249. cout << "There are copies of the book in library. Here's yours!\n";
  250. book_pair[i]->count--;
  251. }
  252. else cout << "At the moment there are no available copies of the book in the library\n";
  253. }
  254. cout << "The book is not available in the library\n";
  255. }
  256.  
  257. const Book** get_all_the_books_with_a_given_author(const Author& author, int &get_list_sz){
  258. Book** book_list = nullptr;
  259. int list_sz = 0;
  260. for (int i = 0; i < number_of_different_books; ++i)
  261. if (book_pair[i]->book.is_there_author_named(author.get_name(), author.get_last_name())){
  262. add_book_to_the_list(book_list, list_sz, book_pair[i]->book);
  263. list_sz++;
  264. }
  265. get_list_sz = list_sz;
  266. return const_cast<const Book**>(book_list);
  267. }
  268.  
  269. private:
  270. static bool compare_books(BookCountPair *a, BookCountPair *b){
  271. return strcmp( a->book.get_name(), b->book.get_name()) <= 0;
  272. }
  273.  
  274. void add_book_to_the_list(Book** &list, int list_sz, const Book& book){
  275. int new_sz = list_sz + 1;
  276. Book** new_list = new Book* [new_sz];
  277. for (int i = 0; i < list_sz; ++i)
  278. new_list[i] = new Book(*list[i]);
  279. new_list[list_sz] = new Book(book);
  280. for (int i = 0; i < list_sz; ++i)
  281. delete list[i];
  282. delete[] list;
  283. list = new_list;
  284. }
  285.  
  286. void copy_book_pairs(const BookCountPair** from, BookCountPair** &book_pair, const unsigned int _number_of_different_books){
  287. number_of_different_books = _number_of_different_books;
  288. book_pair = new BookCountPair* [number_of_different_books];
  289. for (int i = 0; i < number_of_different_books; ++i)
  290. book_pair[i] = new BookCountPair(*from[i]);
  291. }
  292. BookCountPair** book_pair;
  293. unsigned int number_of_different_books;
  294.  
  295. };
  296.  
  297. int main(){
  298. char name[64] = "aleks";
  299. char last_name[64] = "ivanov";
  300. char email[64] = "[email protected]";
  301. unsigned int age = 42;
  302. Author a(name, last_name, email, age);
  303. //strcpy(name, a.get_name());
  304. //strcpy(last_name, a.get_last_name());
  305. //strcpy(email, a.get_email());
  306. //cout << name << "\n" << last_name << "\n" << email << "\n" << age << "\n";
  307. cout << a << endl;
  308. Author b(a);
  309. a.set_name("RadoShisharkata");
  310. cout << a << endl;
  311. cout << b << endl;
  312. Author c;
  313. c = b;
  314. b.set_name("RadoShisharkata2");
  315. cout << b << "\n";
  316. cout << c << "\n";
  317.  
  318. }
Advertisement
Add Comment
Please, Sign In to add comment