C Program To Create A Structure Library II

C Program :
/* Aim: Write a program to accept details for n books and write a menu driven program for the following:

1] Display all reference books
2] Find the total number of reference books
3] Display the edition of specific reference book */

#include<stdio.h>
#include<string.h>

struct library
{
 int id;
 int code;
 char title[80],publisher[20];

 union u
 {
  int no_of_copies;
  char month[20];
  int edition;
 }info[200];
 
 int cost;
};

void DisplayAll_Ref(struct library book1[],int size);
int Total_Ref(struct library book1[],int size);
void DisRef(struct library book1[],int size,char title[80]);

void main()
{
 struct library book1[200];
 char title[80]; 
 int i,size,ope;

 printf("\n How many books are there in library:- ");
 scanf("%d",&size);
 
 printf("\n Enter the details of book:- ");

 for(i=0;i<size;i++)
 {
 printf("\n Enter the id,title,and publisher for %dth book:- ",i+1);
 scanf("%d%s%s",&book1[i].id,book1[i].title,book1[i].publisher);

 do{
 printf("\n Enter the code: 1-Text Book,2-magzine,3-Reference:- ");
 scanf("%d",&book1[i].code);

 switch(book1[i].code)
 {
 case 1:
 printf("\n Enter the number of copies:- ");
 scanf(" %d",&book1[i].info[i].no_of_copies);
 break;

 case 2:
 printf("\n Enter the issue month date:- ");
 scanf(" %s",book1[i].info[i].month);
 break;

 case 3:
 printf("\n Enter the edition number:- ");
 scanf(" %d",&book1[i].info[i].edition);
 }}while(book1[i].code<1 book1="" code="" i="">3);
 
 printf("\n Enter the cost:- ");
 scanf("%d",&book1[i].cost);
 }

 do{
 printf("\n Which operation do you want to perform:- \n");
 printf("\n ----------Menu-------- \n");
 printf("\n1.Display all reference books\n");
 printf("2.Find the total number of reference books \n");
 printf("3.Display the edition of specific reference book \n");
 printf("4.Exit \n");
 printf("\n Enter your choice:- ");
 scanf("%d",&ope);
 
 switch(ope)
 {
 case 1:
 DisplayAll_Ref(book1,size);
 break;
 case 2:
 printf("\n There are %d reference books in the library \n \n",Total_Ref(book1,size)); 
 break;
 case 3:
 printf("\n Enter the title of book:- ");
 scanf("%s",title);
 DisRef(book1,size,title);
 break;
 }}while(ope!=4);

}

// DisplayAll_Ref Function

void DisplayAll_Ref(struct library book1[],int size)
{
 int i;

 printf("\n Details of all reference books are as follows:- \n");
 
 for(i=0;i<size;i++)
 {
 if(book1[i].code==3)
 {
  printf("\n Id = %d",book1[i].id);
  printf("\n Title = %s",book1[i].title);
  printf("\n Publisher = %s",book1[i].publisher);
  printf("\n Edition = %d",book1[i].info[i].edition);
 printf("\n Cost = %d",book1[i].cost);
 }
 printf("\n");
 }
}

//DisRef Function

void DisRef(struct library book1[],int size,char title[80])
{
 int i;

 printf("\n Details of all reference books with \"%s\" as title:- \n",title);
 for(i=0;i<size;i++)
 {
  if(strcmp(book1[i].title,title)==0)
  { 
  printf("\n Edition of \"%s\" is %d",book1[i].title,book1[i].info[i].edition);
  }  
 printf("\n");
 }
}

int Total_Ref(struct library book1[],int size)
{
 int i,j,count=0;

 for(i=0;i<size;i++)
 {
  if(book1[i].code==3)
  count++;
 }
 return count;
}


/* Output of above code:- 

[root@localhost ~]# cc e16a2.c
[root@localhost ~]# ./a.out

 How many books are there in library:- 3

 Enter the details of book:- 
 Enter the id,title,and publisher for 1th book:- 1 Halo Master

 Enter the code: 1-Text Book,2-magzine,3-Reference:- 1

 Enter the number of copies:- 101

 Enter the cost:- 999999

 Enter the id,title,and publisher for 2th book:- 2 Physics Newton

 Enter the code: 1-Text Book,2-magzine,3-Reference:- 3

 Enter the edition number:- 1

 Enter the cost:- 999

 Enter the id,title,and publisher for 3th book:- 3 Biology Robert

 Enter the code: 1-Text Book,2-magzine,3-Reference:- 3

 Enter the edition number:- 1

 Enter the cost:- 1000

 Which operation do you want to perform:- 

 ----------Menu-------- 

1.Display all reference books
2.Find the total number of reference books 
3.Display the edition of specific reference book 
4.Exit 

 Enter your choice:- 1

 Details of all reference books are as follows:- 


 Id = 2
 Title = Physics
 Publisher = Newton
 Edition = 1
 Cost = 999

 Id = 3
 Title = Biology
 Publisher = Robert
 Edition = 1
 Cost = 1000

 Which operation do you want to perform:- 

 ----------Menu-------- 

1.Display all reference books
2.Find the total number of reference books 
3.Display the edition of specific reference book 
4.Exit 

 Enter your choice:- 2

 There are 2 reference books in the library 
 

 Which operation do you want to perform:- 

 ----------Menu-------- 

1.Display all reference books
2.Find the total number of reference books 
3.Display the edition of specific reference book 
4.Exit 

 Enter your choice:- 3

 Enter the title of book:- Biology

 Details of all reference books with "Biology" as title:- 



 Edition of "Biology" is 1

*/ 

Comments