Sunday, November 1, 2009

Chap10[D]f Creating a library system

Write a menu driven program that depicts the working of a library. The menu options should be:
1. Add book information
2. Display book information
3. List all books of given author
4. List the title of specified book
5. List the count of books in the library
6. List the books in the order of accession number
7. Exit Create a structure called library to hold accession number, title of the book, author name, price of the book, and flag indicating whether book is issued or not.


struct library
{
int num;
char title[20];
char author[20];
float price;
int flag;
};

void main()
{
void accbook(struct library *l,int count);
void addbook(struct library *l,int count);
int count,a,ac;
char key[20],j[1];
struct library l[100]={
{1,"Rabbit","MrR",5.6,1},
{3,"Hamster","MrH",10.1,1},
{2,"Rice","MrR",7.0,0},
{5,"Hamlet","MrH",20.7,0},
{4,"Mouse","MrM",15.3,1}
};
clrscr();

while(1)
{
a=menu();

if(a==1)
{
clrscr();
count=countbook(&l);
addbook(&l,count);
}
if(a==2)
{
clrscr();
displaybook(&l);
}
if(a==3)
{
clrscr();
printf("\nKey in the author's name\n");
scanf("%s",key);
authorbook(&l,key);
}
if(a==4)
{
clrscr();
printf("\nKey in the accession number of the book\n");
scanf("%d",&ac);
specbook2(&l,ac);
}
if(a==5)
{
clrscr();
count=countbook(&l);
printf("\nNumber of books in the library is %d",count);
}
if(a==6)
{
clrscr();
count=countbook(&l);
accbook(&l,count);
displaybook(&l);
}
if(a==7)
exit();
printf("\n\n\nHit q and enter to quit and any other value to go back to menu\n");
scanf("%s",j);
if(j[0]==113)
break;
clrscr();
}
}

menu()
{
int acc;


printf("\n1. Add book information");
printf("\n2. Display book information");
printf("\n3. List all books of given author");
printf("\n4. List the title of specified book (Accession Number)");
printf("\n5. List the count of books in the library");
printf("\n6. Sort the books in order of accession number and display them");
printf("\n7. Exit");
printf("\n\nTo access the menu, key in the corresponding number\n");
while(1)
{
scanf("%d",&acc);
if(acc<1||acc>7)
{
printf("\nYou keyed an invalid value, key again\n");
}
else
break;
}
return(acc);
}

countbook(struct library *l)
{
int i,count=0;

for(i=0;l[i].num!=0;i++)
count++;

return(count);
}

void addbook(struct library *l,int count)
{
char j[1];
int s=0,c=0,i;

while(1)
{
if(count>99)
{
printf("\nLibrary has no more space");
break;
}
s=0;
printf("\nKey in accession number\n");
scanf("%d",&l[count].num);
if(l[count].num==0)
s=1;
for(i=0;i {
if(l[i].num==l[count].num)
{
printf("\nSimilar accession number,Key again\n");

s=1;
}
}
if(s==1)
continue;
printf("\nKey in title (no spacings)\n");
scanf("%s",l[count].title);
printf("\nKey in author (no spacings)\n");
scanf("%s",l[count].author);
printf("\nKey in price\n");
scanf("%f",&l[count].price);

while(1)
{
printf("\nKey 0 if the book is borrowed otherwise key 1 if available\n");
scanf("%d",&l[count].flag);

if(l[count].flag==0||l[count].flag==1)
break;
}

c++;

printf("\nKey any value to continue adding books or key q to quit to menu");
scanf("%s",j);
if(j[0]==113)
break;

clrscr();
count++;
}

if(c==1)
printf("\nYou keyed in 1 new book");

else if (c>1)
printf("\nYou keyed in %d new books",c);
}

displaybook(struct library *l)
{
int i;

for(i=0;l[i].num!=0;i++)
{
printf("\nNo: %d, Title: %s, Author: %s, Price: %f",l[i].num,l[i].title,l[i].author,l[i].price);
if(l[i].flag==1)
printf(", Availability: Yes");

else
printf(", Availability: No");
}
}

specbook(struct library *l)
{
printf("\nNo: %d, Title: %s, Author: %s, Price: %f",l->num,l->title,l->author,l->price);
if(l->flag==1)
printf(", Availability: Yes");
else
printf(", Availability: No");
}

specbook2(struct library *l,int j)
{
int i,f=0;

for(i=0;l[i].num!=0;i++)
{
if(l[i].num==j)
{
specbook(&l[i]);
f=1;
break;
}
}
if(f==0)
printf("\nNo such book with this accession number");
}

authorbook(struct library *l,char *key)
{
int i,j,c,len,f=0;
len=strlen(key);
for(i=0;l[i].num!=0;i++)
{
for(j=0,c=0;l[i].author[j]!=0;j++)
{
if(*(key+j)==l[i].author[j])
c++;
}
if(c==len)
{
specbook(&l[i]);
f=1;
}
}
if(f==0)
printf("\nThere are no books with this author's name");
}

void accbook(struct library *l,int count)
{
int i,j;
struct library a1,a2;
for(i=0;l[i].num!=0;i++)
{
for(j=i+1;j {
if(l[i].num>l[j].num)
{
a1=l[i];
a2=l[j];
l[i]=a2;
l[j]=a1;
}
}
}
}

linkfloat()
{
float a,*b;
b=&a;
a=*b;
}

No comments:

Post a Comment