Advertisement
Divyansh_Chourey

Library management

May 1st, 2024
694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.27 KB | Source Code | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. struct book{
  5.     int num;
  6.     char title[20];
  7.     char author[20];
  8.     int year;
  9. };
  10. typedef struct book book;
  11.  
  12. void add_book(book *ptr, char t[20], char a[20], int y);
  13. void show(book *ptr);
  14. int count(book *ptr);
  15. void books_of(book *ptr, char a[20]);
  16.  
  17. int main(){
  18.     book b_arr[100] = {
  19.         1, "Wonder_of_universe", "Divyansh", 2005,
  20.         2, "Science_behind_us", "Yuvansh", 2004,
  21.         3, "Eternity", "Jatin", 2007,
  22.     };
  23.    
  24.     book *ptr;
  25.     ptr = &b_arr[0];
  26.  
  27.     int response=-1;
  28.     char book_title[20];
  29.     char book_author[20];
  30.     int book_year;
  31.     char w_author[20];
  32.     printf("\n\t----------Welcome to library----------\n");
  33.     printf("\tEnter (1) to Add book to the library\n");
  34.     printf("\tEnter (2) to Display all the book in library\n");
  35.     printf("\tEnter (3) to List all the book of a author\n");
  36.     printf("\tEnter (4) to Know number of book in library\n");
  37.     printf("\tEnter (0) to Exit the library.\n");
  38.  
  39.     while(response != 0){
  40.         printf("\nEnter you response:");
  41.         scanf("%d", &response);
  42.         if(response == 1){
  43.             printf("****Please avoid using spaces, instread use '_' and enter only first name of author****\n");
  44.             printf("Enter book title: ");
  45.             scanf("%s", &book_title);
  46.  
  47.             printf("Enter author name: ");
  48.             scanf("%s", &book_author);
  49.  
  50.             printf("Enter year of publication: ");
  51.             scanf("%d", &book_year);
  52.  
  53.             add_book(ptr, book_title, book_author, book_year);
  54.             printf("Book added sucessfully\n------------------------------\n");
  55.         }
  56.         else if(response == 2){
  57.             printf("\nFollwing are the books in library\n");
  58.             show(ptr);
  59.             printf("------------------------------\n");
  60.         }
  61.         else if(response == 3){
  62.             printf("Enter name of author: ");
  63.             scanf("%s", &w_author);
  64.             books_of(ptr, w_author);
  65.             printf("------------------------------\n");
  66.         }
  67.         else if(response == 4){
  68.             printf("There are total %d books in library\n", count(ptr));
  69.             printf("------------------------------\n");
  70.         }
  71.     }
  72.    
  73.     return 0;
  74. }
  75.  
  76. void show(book *ptr){
  77.     int n = count(ptr);
  78.     for(int i=0; i<n; i++){
  79.     printf("Book Number: %d\n", ptr->num);
  80.     printf("Book Title: %s\n", ptr->title);
  81.     printf("Author of book: %s\n", ptr->author);
  82.     printf("Year of publication: %d\n", ptr->year);
  83.     printf("\n");
  84.     ptr++;
  85.     }
  86.  
  87. }
  88.  
  89. void add_book(book *ptr, char t[20], char a[20], int y){
  90.     int n = count(ptr);
  91.     n++;
  92.     ptr = ptr + (n-1);
  93.     ptr->num = n;
  94.     strcpy(ptr->title, t);
  95.     strcpy(ptr->author, a);
  96.     ptr->year = y;
  97.  
  98. }
  99.  
  100. int count(book *ptr){
  101.     int c=0;
  102.     for(int i=0; i<100; i++){
  103.         if(ptr->num != 0){
  104.             c++;
  105.         }
  106.         ptr++;
  107.     }
  108.     return c;
  109. }
  110.  
  111. void books_of(book *ptr, char au[20]){
  112.     int c, n=0;
  113.     for(int i=0; i<100; i++){
  114.         c = strcmp(au, ptr->author);
  115.         if(c==0){
  116.             printf("Avalible book: %s\n", ptr->title);
  117.             n++;
  118.         }
  119.         ptr++;
  120.        
  121.     }
  122.     if(n == 0){
  123.         printf("There are no such books avalible.\n");
  124.     }
  125. }
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement