Wednesday 21 December 2016

Library program in which Book has multiple authors and Shelf has multiple books using Arrays of structures


Have the following structs, for Book, Author and Shelf. Book has multiple authors and Shelf has multiple books. Write a program to create a functions new_author (), new_book (), print_book (), add_author_to_book (), new_shelf (), delete_shelf (), print_shelf(), add_book_to_shelf() with required return types and parameters.


#include<stdio.h>
#include <stdlib.h>
#define NA 100
#define NB 100
#define NS 100
int ca=0,cb=0,cs=0;
struct author
{
    int aid;
    char name[20];
   
}a[NA];
struct book
{
    char title[25];
    int bid,faid[5],cab;

}b[NB];
struct shelf
{
    int sid,fbid[100],csb;
}s[NS];

void add_author()
{
    a[ca].aid=ca;
    printf("enter the name of author");
    scanf("%s",&a[ca].name);
    ca++;
}
void add_book()
{
    int temp;
    b[cb].bid=cb;
   
    printf("enter the title of the book ");
    scanf("%s",&b[cb].title);
    b[cb].cab=0;
    cb++;
}
void add_author_to_book()
{
    int t1,t2,c1;
    printf("enter the book id and author id");
    scanf("%d%d",&t1,&t2);
    c1=b[t1].cab;
    b[t1].faid[c1]=t2;
    b[t1].cab++;
}
print_author()
{
    int i;
    for(i=0;i<ca;i++)
    {
        printf("\n%d\t%s",a[i].aid,a[i].name);
    }
}
print_book()
{
    int i,j;
    for(i=0;i<cb;i++)
    {
        printf("\n%d\t%s",b[i].bid,b[i].title);
        for(j=0;j<b[i].cab;j++)
        {
            printf("\n author = %s",a[b[i].faid[j]].name);
        }
    }
}
void add_shelf()
{
    s[cs].sid=cs;
    s[cs].csb=0;
    cs++;  
}  
void add_book_to_shelf()
{
    int b1,s1;
    printf("enter shelf id and book id");
    scanf("%d%d",&s1,&b1);
    s[s1].fbid[s[s1].csb]=b1;
    s[s1].csb++;
  
}
void print_shelfid()
{   
    int i;
    for( i = 0 ; i < cs; i++ )
         printf("%d\n", s[i].sid);   
}
void print_books_in_shelf()
{
    int sid1,i;
    printf("enter shelf id");
    scanf("%d",&sid1);
    for(i=0;i<=s[sid1].csb;i++)
    {
        printf("\n%s",b[s[sid1].fbid[i]].title);
    }
}
void delete_shelf()
{
    int sid2,i;
    printf("enter shelf id");
    scanf("%d",&sid2);
    if ( sid2 >cs )
      printf("Deletion not possible.\n");
   else
   {
      for ( i = sid2 - 1 ; i < cs ; i++ )
         s[i] = s[i+1];
       cs--;
    }
}
main()
{
    int choice;
    printf("\n Press 1. add author\n2. print authors \n");
    printf("3. add book\n4. add author to book\n5. print book");
    printf(" 6. add shelf\n7. print shelf \n");
    printf("8. add book to shelf\n9. print books in shelf \n10. delete shelf\n11. exit\n");
    while(1)
    {
      printf("enter ur choice");
      scanf("%d",&choice);
      switch(choice)
      {
          case 1:    add_author();
                  break;
        case 2:    print_author();
                break;
        case 3:    add_book();
                break;
        case 4:    add_author_to_book();
                break;
        case 5: print_book();
                break;
        case 6: add_shelf();
                break;
        case 7: print_shelfid();
                break;
        case 8:    add_book_to_shelf();
                break;
        case 9:    print_books_in_shelf();
                break;
        case 10: delete_shelf();
                break;
        case 11: exit(0);
        default: printf("invalid choice");
    }
    }
}

No comments:

Post a Comment