C Program To Create A Structure Library I
C Program :
/* Aim: Write a program to accept details for n books and write a menu driven program for the following: 1] Display all text books 2] Search text book according to title 3] Find the total cost of all books */ #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_TextBooks(struct library book1[],int size); void Search_TTitle(struct library book1[],int size,char title[20]); int Total_Cost(struct library book1[],int size); 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[i].code>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 text books \n"); printf("2.Search text book according to the title \n"); printf("3.Find the total cost of all books\n"); printf("4.Exit \n"); printf("\n Enter your choice:- "); scanf("%d",&ope); switch(ope) { case 1: DisplayAll_TextBooks(book1,size); break; case 2: printf("\n Enter the title of book to search for:- "); scanf("%s",title); Search_ATitle(book1,size,title); break; case 3: printf("\n The total cost of all books is %d \n \n",Total_Cost(book1,size)); break; }}while(ope!=4); } // DisplayAll_TextBooks Function void DisplayAll_TextBooks(struct library book1[],int size) { int i; printf("\n Details of all text books are as follows:- "); for(i=0;i<size;i++) { if(book1[i].code==1) { printf("\n Id = %d",book1[i].id); printf("\n Title = %s",book1[i].title); printf("\n Publisher = %s",book1[i].publisher); printf("\n Copies = %d",book1[i].info[i].no_of_copies); } printf("\n"); } } //Search_TTitle Function void Search_TTitle(struct library book1[],int size,char title[20]) { int i; printf("\n Details of all books with \"%s\" as title:- \n ",title); for(i=0;i<size;i++) { if(book1[i].code==1) { if(strcmp(book1[i].title,title)==0) { printf("\n Id = %d",book1[i].id); printf("\n Title = %s",book1[i].title); printf("\n Publisher = %s",book1[i].publisher); switch(book1.code) { case 1: printf("\n Copies = %d",book1[i].info[i].no_of_copies); break; case 2: printf("\n Issue month name = %s",book1[i].info[i].month); break; case 3: printf("\n Edition number = %d",book1[i].info[i].edition); break; } }} printf("\n"); } } // Total_Cost function int Total_Cost(struct library book1[],int size) { int i,total_cost=0; for(i=0;i<size;i++) { if(book1[i].code==1) total_cost+=book1[i].cost*book1[i].info[i].no_of_copies; else total_cost+=book1[i].cost; } return total_cost; }
/* Output of above code:-
[root@localhost ~]# cc e16a1.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 Mordern_Physics Albert_Einstein
Enter the code: 1-Text Book,2-magzine,3-Reference:- 3
Enter the edition number:- 1
Enter the cost:-9999
Enter the id,title,and publisher for 2th book:- 2 Cosmos Karl_Sagans
Enter the code: 1-Text Book,2-magzine,3-Reference:- 3
Enter the edition number:- 1
Enter the cost:-19999
Enter the id,title,and publisher for 3th book:- 3 Artificial_Intelligence AI
Enter the code: 1-Text Book,2-magzine,3-Reference:- 1
Enter the number of copies:- 101
Enter the cost:-999
Which operation do you want to perform:-
----------Menu--------
1.Display all text books
2.Search text book according to the title
3.Find the total cost of all books
4.Exit
Enter your choice:- 1
Details of all text books are as follows:-
Id = 3
Title = Artificial_Intelligence
Publisher = AI
Copies = 101
*/
Comments