fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct {
  6. char title_[64];
  7. char author_[64];
  8. int pages_;
  9. } Book;
  10.  
  11. typedef struct {
  12. Book *data_;
  13. int size_;
  14. } Library;
  15.  
  16.  
  17. //TODO: write code here
  18. void shiftBooks(Library *library, int insert_position)
  19. {
  20. for(int book_index = library->size_ - 1; book_index >= insert_position; book_index--)
  21. {
  22. Book *source_book = library->data_ + book_index;
  23. Book *destination_book = library->data_ + book_index + 1;
  24.  
  25. strcpy(destination_book->title_, source_book->title_);
  26. strcpy(destination_book->author_, source_book->author_);
  27. destination_book->pages_ = source_book->pages_;
  28. }
  29. }
  30.  
  31. void addBookSorted(Library *library, char *title, char *author, int pages)
  32. {
  33. Book *temporary_book = realloc(library->data_, sizeof(Book) * (library->size_ + 1));
  34. if(!temporary_book)
  35. {
  36. return;
  37. }
  38. library->data_ = temporary_book;
  39. temporary_book = NULL;
  40.  
  41. int insert_position = -1;
  42. for(int book_index = 0; book_index < library->size_ && insert_position == -1; book_index++)
  43. {
  44. if(library->data_[book_index].pages_ > pages)
  45. {
  46. insert_position = book_index;
  47. }
  48. }
  49.  
  50. if(insert_position == -1)
  51. {
  52. insert_position = library->size_;
  53. } else {
  54. shiftBooks(library, insert_position);
  55. }
  56.  
  57. Book *new_book = library->data_ + insert_position;
  58. strcpy(new_book->title_, title);
  59. strcpy(new_book->author_, author);
  60. new_book->pages_ = pages;
  61.  
  62. library->size_++;
  63. }
  64.  
  65.  
  66. void printLibrary(Library *library)
  67. {
  68. printf("Books (size: %d)\n", library->size_);
  69. for (int book_index = 0; book_index < library->size_; book_index++)
  70. {
  71. Book current_book = library->data_[book_index];
  72. printf("%s by %s, %d pages\n", current_book.title_, current_book.author_, current_book.pages_);
  73. }
  74. }
  75.  
  76. int main()
  77. {
  78. Library library = { NULL, 0 };
  79.  
  80. int tc = 0;
  81. printf("Enter tc: ");
  82. scanf("%d", &tc);
  83.  
  84. if (tc == 1)
  85. {
  86. addBookSorted(&library, "Buch 1", "Autor 1", 10);
  87. addBookSorted(&library, "Buch 2", "Autor 2", 100);
  88. addBookSorted(&library, "Buch 3", "Autor 2", 1000);
  89. printLibrary(&library);
  90. }
  91. else if (tc == 2)
  92. {
  93. addBookSorted(&library, "Buch 1", "Autor 1", 10);
  94. addBookSorted(&library, "Buch 3", "Autor 2", 1000);
  95. addBookSorted(&library, "Buch 2", "Autor 2", 100);
  96. printLibrary(&library);
  97. }
  98. else if (tc == 3)
  99. {
  100. //...
  101. printLibrary(&library);
  102. }
  103.  
  104. //TODO: free data
  105. free(library.data_);
  106.  
  107. return 0;
  108. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Enter tc: