Slip
no : 19_2 write a menu driven prog to accept and display book info of libarary
(using struture)



#include<conio.h>
#include<stdio.h>
#include<string.h>
struct
Book
{
int bid;
char
bname[20],aname[20],pub[30]; int cost;
}b[20];
void
accept(struct Book bk[],int n)
{
|
int i;
|
|
|
for(i=0;i<n;i++)
|
|
|
{
|
|
|
printf("\n Enter book id :
");
|
|
|
scanf("%d",&bk[i].bid);
|
|
|
printf("\n
Enter book name : ");
|
|
|
scanf("%s",bk[i].bname);
|
|
|
printf("\n
Enter author name : ");
|
|
|
scanf("%s",bk[i].aname);
|
|
|
printf("\n
Enter publication : ");
|
|
|
scanf("%s",bk[i].pub);
|
|
|
printf("\n Enter book cost :
");
|
|
|
scanf("%d",&bk[i].cost);
|
|
}
|
}
|
|
void
display(struct Book bk[],int n)
{
int i;
printf("\nbid\tbname\tanme\tpublication\tcost\n");
for(i=0;i<n;i++)
{ if(bk[i].cost<=500)
{
printf("\n%d\t%s\t%s\t%s\t
%d",bk[i].bid,bk[i].bname,bk[i].aname,bk[i].pub,bk[i].cost);
}
}
}
void
display_all(struct Book bk[],int n)
{
int i;
printf("\nbid\tbname\tanme\tpublication\tcost\n"); for(i=0;i<n;i++)
{ printf("\n%d\t%s\t%s\t%s\t
%d",bk[i].bid,bk[i].bname,bk[i].aname,bk[i].pub,bk[i].cost);
}
}
void
search_author(struct Book bk[],int n,char name[])
{
int
i,flag=0;; printf("\nbid\tbname\tanme\tpublication\tcost\n");
for(i=0;i<n;i++)
{ if(strcmp(bk[i].aname,name)==0)
{
printf("\n%d\t%s\t%s\t%s\t
%d",bk[i].bid,bk[i].bname,bk[i].aname,bk[i].pub,bk[i].cost);
flag=1;
}
}
if(flag==0)
printf("%s not found",name);
}
void
search_pub(struct Book bk[],int n,char pname[])
{
int
i,flag=0;; printf("\nbid\tbname\tanme\tpublication\tcost\n");
for(i=0;i<n;i++)
{ if(strcmp(bk[i].pub,pname)==0)
{
printf("\n%d\t%s\t%s\t%s\t
%d",bk[i].bid,bk[i].bname,bk[i].aname,bk[i].pub,bk[i].cost);
flag=1;
}
}
if(flag==0)
printf("%s not
found",pname);
}
int main()
{
int
n,ch;char name[30]; clrscr();
do
{
printf("\n 1.Add book \n 2.Books
of specific author \n 3.Books by specific publisher \n 4.Book costing below 500
\n 5.All Books: \n 0.exit");
printf("\n
Enter your choice : "); scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n
Enter how many books "); scanf("%d",&n);
accept(b,n);
break;
case 2: printf("\n Enter author name : ");
scanf("%s",name); search_author(b,n,name); break;
case 3 :printf("\n Enter publisher : ");
scanf("%s",name); search_pub(b,n,name); break;
case 4: display(b,n); break;
case 5 :display_all(b,n); break;
case 0: break;
default: printf("\n Inavlid
choice ");
}
}while(ch!=0);
getch();
}

No comments:
Post a Comment