fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define MAX_BOOKS 10
  5.  
  6. struct Book {
  7. char author[50];
  8. char title[100];
  9. char publisher[50];
  10. int year;
  11. int num_of_pages;
  12. };
  13.  
  14. void setBook(struct Book *book, char author[], char title[], char publisher[], int year, int num_of_pages) {
  15. strcpy(book->author, author);
  16. strcpy(book->title, title);
  17. strcpy(book->publisher, publisher);
  18. book->year = year;
  19. book->num_of_pages = num_of_pages;
  20. }
  21.  
  22. void getBook(struct Book book) {
  23. printf("Author: %s\n", book.author);
  24. printf("Title: %s\n", book.title);
  25. printf("Publisher: %s\n", book.publisher);
  26. printf("Year: %d\n", book.year);
  27. printf("Number of pages: %d\n", book.num_of_pages);
  28. }
  29.  
  30. void showBooksByAuthor(struct Book books[], int num_books, char author[]) {
  31. printf("Books by author %s:\n", author);
  32. for (int i = 0; i < num_books; i++) {
  33. if (strcmp(books[i].author, author) == 0) {
  34. getBook(books[i]);
  35. }
  36. }
  37. }
  38.  
  39. void showBooksByPublisher(struct Book books[], int num_books, char publisher[]) {
  40. printf("Books by publisher %s:\n", publisher);
  41. for (int i = 0; i < num_books; i++) {
  42. if (strcmp(books[i].publisher, publisher) == 0) {
  43. getBook(books[i]);
  44. }
  45. }
  46. }
  47.  
  48. void showBooksAfterYear(struct Book books[], int num_books, int year) {
  49. printf("Books released after year %d:\n", year);
  50. for (int i = 0; i < num_books; i++) {
  51. if (books[i].year > year) {
  52. getBook(books[i]);
  53. }
  54. }
  55. }
  56.  
  57. int main() {
  58. struct Book books[MAX_BOOKS];
  59. setBook(&books[0], "G", "B", "N", 2000, 300);
  60. setBook(&books[1], "G", "BB", "N", 1995, 300);
  61. setBook(&books[2], "P", "H", "V", 2010, 400);
  62.  
  63. showBooksByAuthor(books, 3, "G");
  64. showBooksByPublisher(books, 3, "V");
  65. showBooksAfterYear(books, 3, 2001);
  66.  
  67. return 0;
  68. }
  69.  
Success #stdin #stdout 0.01s 5308KB
stdin
Standard input is empty
stdout
Books by author G:
Author: G
Title: B
Publisher: N
Year: 2000
Number of pages: 300
Author: G
Title: BB
Publisher: N
Year: 1995
Number of pages: 300
Books by publisher V:
Author: P
Title: H
Publisher: V
Year: 2010
Number of pages: 400
Books released after year 2001:
Author: P
Title: H
Publisher: V
Year: 2010
Number of pages: 400