fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define MAX_BOOKS 100
  5.  
  6. typedef struct {
  7. char author[50];
  8. char title[100];
  9. char publisher[50];
  10. int year;
  11. int num_pages;
  12. } Book;
  13.  
  14. void set(Book* book, const char* author, const char* title, const char* publisher, int year, int num_pages) {
  15. strcpy(book->author, author);
  16. strcpy(book->title, title);
  17. strcpy(book->publisher, publisher);
  18. book->year = year;
  19. book->num_pages = num_pages;
  20. }
  21.  
  22. void get(Book* book, char* author, char* title, char* publisher, int* year, int* num_pages) {
  23. strcpy(author, book->author);
  24. strcpy(title, book->title);
  25. strcpy(publisher, book->publisher);
  26. *year = book->year;
  27. *num_pages = book->num_pages;
  28. }
  29.  
  30. void show(Book* book) {
  31. printf("Author: %s\n", book->author);
  32. printf("Title: %s\n", book->title);
  33. printf("Publisher: %s\n", book->publisher);
  34. printf("Year: %d\n", book->year);
  35. printf("Number of pages: %d\n", book->num_pages);
  36. }
  37.  
  38. int main() {
  39. Book books[MAX_BOOKS];
  40. int num_books = 0;
  41.  
  42. // Добавляем книги в массив
  43. Book book1, book2, book3;
  44. set(&book1, "P", "Cool", "NN", 1990, 300);
  45. set(&book2, "G", "Very cool", "News", 2010, 250);
  46. set(&book3, "G", "Best", "WWW", 2011, 400);
  47. books[num_books++] = book1;
  48. books[num_books++] = book2;
  49.  
  50. // Выводим список книг заданного автора
  51. char author_search[50] = "Author1";
  52. printf("Books by author %s:\n", author_search);
  53. for (int i = 0; i < num_books; i++) {
  54. if (strcmp(books[i].author, author_search) == 0) {
  55. show(&books[i]);
  56. }
  57. }
  58.  
  59. // Выводим список книг выпущенных заданным издателем
  60. char publisher_search[50] = "Publisher2";
  61. printf("\nBooks published by %s:\n", publisher_search);
  62. for (int i = 0; i < num_books; i++) {
  63. if (strcmp(books[i].publisher, publisher_search) == 0) {
  64. show(&books[i]);
  65. }
  66. }
  67.  
  68. // Выводим список книг выпущенных после заданного года
  69. int year_search = 2005;
  70. printf("\nBooks published after %d:\n", year_search);
  71. for (int i = 0; i < num_books; i++) {
  72. if (books[i].year > year_search) {
  73. show(&books[i]);
  74. }
  75. }
  76.  
  77. return 0;
  78. }
  79.  
  80.  
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
Books by author Author1:

Books published by Publisher2:

Books published after 2005:
Author: G
Title: Very cool
Publisher: News
Year: 2010
Number of pages: 250