Monday, September 14, 2009

C Program to Maintain a Small Library

#include"stdio.h"
#include"conio.h"
#include"string.h"
#include"math.h"
struct library
{
            int acc_no;
            char title[20];
            char author[15];
            int price;
            char flag;
};
struct library book[200];
int count,i=0;
int menu()
{
            int opt;
            printf("\n 1. Add Book Information\n 2. Display Book Information");
            printf("\n 3. List all Books of a Given Author\n 4. List the Title of a Specified Book");
            printf("\n 5. List the count of Books in the Library");
            printf("\n 6. List the Books in the Order of Accession No.");
            printf("\n 7. Exit");
            printf("\n Please Enter Your Option : ");
            scanf("%d",&opt);
            return opt;
}
void add()
{
            char ch;
            do{
            printf("\n --**:: Enter the Information of a New Book ::**--");
            printf("\n Accession no.                      : ");
            scanf("%d",&book[i].acc_no);
            printf("\n Title                        : ");
            scanf("%s",book[i].title);
            printf("\n Name of Author                              : ");
            scanf("%s",book[i].author);
            printf("\n Price of the Book                : ");
            scanf("%d",&book[i].price);
            fflush(stdin);
            printf("\n Has the book been Issued? : ");
            scanf("%c",&book[i].flag);
            fflush(stdin);
            textcolor(4);
            cprintf("\n\n Do you want to add another book (y/n) : ");
            textcolor(7);
            scanf("%c",&ch);
            count=i+1;
            i++;
            }while(ch=='y' || ch=='Y');
}
void display()
{
            int i;
            for(i=0;i
            {
                        printf("\n\n <---------------Book No. %d----------------->\n",i+1);
                        printf("\n Name                                   : %s",book[i].title);
                        printf("\n Author                     : %s",book[i].author);
                        printf("\n Accession No.        : %d",book[i].acc_no);
                        printf("\n Price                        : %d",book[i].price);
                        printf("\n Issued (STATUS)   : %c",book[i].flag);
            }
}
void searchau()
{
            char name[15];
            int i,cnt=0;
            printf("\n Enter the Author name to Search : ");
            scanf("%s",name);
            for(i=0;i
            {
                        if(strcmp(name,book[i].author)==0)
                                    printf("\n Book %d  : %s",++cnt,book[i].title);
            }
            if(cnt==0)
                        printf("\n NO RECORD FOUND\n");
            }
void searchacc()
{
            int i,acc,found=0;
            printf("\n Enter the Accession no. to Search the Book : ");
            scanf("%d",&acc);
            for(i=0;i
            {
                        if(acc==book[i].acc_no)
                        {
                                    printf("\n\n Book Title : %s",book[i].title);
                                    found=1;
                        }
            }
            if(found==0)
                        printf("\n NO RECORD FOUND");
}
void main()
{
            int opt;
            char ch;
            do{
            clrscr();
            opt=menu();
            switch(opt)
            {
                        case 1:
                           add();
                           break;
                        case 2:
                           display();
                           break;
                        case 3:
                           searchau();
                           break;
                        case 4:
                           searchacc();
                           break;
                        case 5:
                           printf("\n The Total No. of Books in Library is : %d",count);
                           break;
                        case 6:
                           display();
                           break;
                        default:
                           exit(0);
            }
            fflush(stdin);
            printf("\n Do you want to Continue (y/n) : ");
            scanf("%c",&ch);
            }while(ch=='y' || ch=='Y');
}

No comments:

Post a Comment