Sei sulla pagina 1di 12

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

h> typedef struct book { char isbn[16]; char name[64]; char author[64]; char subject[64]; char publisher[64]; double price; int total_copies; int available_copies; }book_t; typedef struct node { book_t *b; struct node *prev; struct node *next; }node_t; typedef struct list { node_t *head; int count; }list_t; void init_list(list_t *list) { list->head = NULL; } void add_book(list_t *list,book_t *b); void insert_sorted(list_t *list,book_t *b); node_t* create_node(book_t *b); void add_last(list_t *bk_list, book_t *b); void add_first(list_t *bk_list, book_t *b); void print_fn(book_t *b,FILE *file,list_t *list); //void write_book(book_t *b,FILE *db,list_t *list); void display(list_t *list,book_t *b); void delete_first(list_t *list); void delete_book(list_t *list,char isbn[]); void delete_all(list_t *list); node_t* search_book(list_t *list,char isbn[]); void save_database(list_t *list); void load_database(list_t *list); node_t* find_books_by_name(list_t *list,char name_book[]); void find_books_by_author(list_t *list,char author_book[]); void find_books_by_subject(list_t *list,char subject_book[]); book_t* select_book(list_t *list); void display_book(book_t *b); //void load_report(list_t *rep_list,book_t *bk); void save_report(book_t *bk); void node_display(node_t *temp); int main() { book_t *b=NULL;

int choice; int j=0; list_t list; init_list(&list); node_t *anyone; load_database(&list); do { printf("\n-------------------------------------------------------"); printf("\n Main Menu \n 1.Display Books \n 2.Search Book By Name \n 3.Search Boo k By Author\n 4.Search Book By Subject"); printf("\n 5.Add New Book \n 6.Select Book \n 7.Delete Book \n 8.Delete First \n 9.Delete All"); printf("\n enter your choice "); scanf("%d",&choice); switch(choice) { case 1://Display Books display(&list,b); break; case 2://Search Book By Name char name_book[64]; printf("\n Enter the Name of book you want to search "); scanf("%s",name_book); anyone=find_books_by_name(&list,name_book); if(anyone==0) { printf("\n book is not available"); } else node_display(anyone); break; case 3://Search Book By Author char author_book[64]; printf("\n Enter the Name of Author of Book you want to search "); scanf("%s",author_book); find_books_by_author(&list,author_book); break; case 4://Search Book By Subject char subject_book[64]; printf("\n Enter the Subject of book you want to search "); scanf("%s",subject_book); find_books_by_subject(&list,subject_book); break; case 5://Add New Book printf("\n Add new book "); b = (book_t*)malloc (sizeof(book_t)); add_book(&list,b); insert_sorted(&list,b); break; case 6://Select Book book_t *cur_book; cur_book = (book_t*)malloc (sizeof(book_t)); list_t rep_list; init_list(&rep_list);

cur_book=select_book(&list); if(cur_book==0) { printf(" "); } else { int ch; //load_report(&rep_list,cur_book); //add_last(&rep_list,cur_book); do { printf("\n Do you want to perform any further operation on selec ted book"); printf("\n ***********Book Menu*****************"); printf("\n0. Main menu \n1. Display Details \n2. Issue Book \n3. Return Book \n4. Add Copy \n5. Delete Copy"); printf("\n Enter Your Choice "); scanf("%d",&ch); switch(ch) { case 1: //Display details display_book(cur_book); break; case 2: //Issue Book if(cur_book->available_copies > 0) { cur_book->available_copies=cur_book->available _copies-1; display_book(cur_book); } else printf("\n Book Can Not Be Issued"); break; case 3: //return book cur_book->available_copies=cur_book->available_copie s+1; if(cur_book->available_copies>cur_book->total_co pies) { cur_book->available_copies=cur_book->available_copies-1; } display_book(cur_book); break; case 4: //Add copy cur_book->total_copies=cur_book->total_copies+1; cur_book->available_copies=cur_book->available_c opies+1; display_book(cur_book); break; case 5://Delete Copy cur_book->total_copies=cur_book->total_copies-1; cur_book->available_copies=cur_book->available_c opies-1; display_book(cur_book); break;

} }while(ch!=0); save_report(cur_book); } break; case 7: //Delete Book char isbn1[16]; printf("Enter The ISBN Number Of Book You Want To Delete "); scanf("%s",isbn1); delete_book(&list,isbn1); break; case 8://Delete First delete_first(&list); break; case 9://Delete All delete_all(&list); break; } }while(choice!=0); save_database(&list); return 0; } void add_book(list_t *list,book_t *b) { char isbnno[16]; node_t *trav; int flag=0; printf("\n Enter Unique ISBN : "); scanf("%s",isbnno); trav=list->head; while(trav!=NULL) { if(strcmp(isbnno,trav->b->isbn)==0) { printf("\n ISBN number is already exist "); flag=1; printf("\n Enter Again "); scanf("%s",isbnno); trav=list->head; } else { trav=trav->next; } } if(flag==0) { strcpy(b->isbn,isbnno); } printf("\n Enter Name : "); scanf("%s",&b->name); printf("\n Enter Author : "); scanf("%s",&b->author); printf("\n Enter Subject : "); scanf("%s",&b->subject); printf("\n Enter Publisher : ");

scanf("%s",&b->publisher); printf("\n Enter Price : "); scanf("%lf",&b->price); printf("\n Enter Total Copies : "); scanf("%d",&b->total_copies); int av; printf("\n Enter Available Copies : "); scanf("%d",&av); do { if(av>b->total_copies) { printf("\n Available is greater than total "); printf("\n Enter Again "); scanf("%d",&av); } }while(b->total_copies<av); b->available_copies=av; } node_t* create_node(book_t *b) { node_t *newnode = (node_t*)malloc(sizeof(node_t)); newnode->b = (book_t*)malloc (sizeof(book_t)); newnode->b= b; newnode->next = NULL; newnode->prev = NULL; return newnode; } void add_first(list_t *list, book_t *b) { node_t *newnode = create_node(b); if(list->head==NULL) { list->head=newnode; } else { newnode->next = list->head; list->head->prev = newnode; list->head = newnode; } } void add_last(list_t *list, book_t *b) { node_t *trav; node_t *newnode = create_node(b); if(list->head==NULL) { list->head=newnode; } else { trav =list->head; while(trav->next!=NULL) trav = trav->next;

newnode->prev = trav; trav->next = newnode; } } void insert_sorted(list_t *list,book_t *b) { node_t *trav, *temp; node_t *newnode=create_node(b); if(list->head==NULL) { list->head = newnode; } else { trav = list->head; while(trav!=NULL && strcmp(b->name,trav->b->name)>0) { trav = trav->next; } if(trav==NULL) add_last(list,b); else if(trav==list->head) add_first(list,b); else { temp = trav->prev; newnode->next = trav; newnode->prev = temp; temp->next = newnode; trav->prev = newnode; } } } void display(list_t *list,book_t *b) { node_t *trav; if(list->head==NULL) { printf("\n List is Empty"); } trav=list->head; while(trav!=NULL) { printf("\n%s %s %s ",trav->b->isbn,trav->b->name,trav->b->author); printf("%s %s %lf ",trav->b->subject,trav->b->publisher,trav->b->pric e); printf("%d %d \n",trav->b->total_copies,trav->b->available_copies); trav=trav->next; } } void delete_first(list_t *list)

{ node_t *trav; trav=list->head; if(list->head == NULL) { printf("\n Empty List"); } else if(list->head->next==NULL) { list->head=NULL; free(trav); } else { list->head=trav->next; trav->next->prev=NULL; free(trav); } } void delete_all(list_t *list) { while(list->head!=NULL) { delete_first(list); } } node_t* search_book(list_t *list,char isbn[]) { node_t *trav; int flag=0; trav=list->head; if(list->head==NULL) { printf("\n List is Empty"); } else { while(trav!=NULL) { if(strcmp(isbn,trav->b->isbn)==0) { printf("\n Record Found"); flag=1; return trav; } else { trav=trav->next; } } if(flag==0) { printf("\n Record Not Found"); return 0;

} } } void delete_book(list_t *list,char isbn[]) { node_t *temp; temp=search_book(list,isbn); if(temp==0) { printf("\n Book is not available to delete"); } else if(temp==list->head) { list->head=NULL; free(list->head); printf("\n Book is deleted now"); } else if(temp->next == NULL) { temp->prev->next=NULL; free(temp); printf("\n Book is deleted now"); } else { temp->prev->next = temp->next; temp->next->prev = temp->prev; free(temp); printf("\n Book is deleted now"); } } void save_database(list_t *list) { node_t *trav; FILE *fp; fp=fopen("d://book/master.db","wb"); trav=list->head; while(trav!=NULL) { fwrite(trav->b ,sizeof(book_t),1,fp); trav=trav->next; } fclose(fp); } void load_database(list_t *list) { book_t *b; FILE *fp; fp=fopen("d://book/master.db","rb"); if(fp==NULL) { printf(" "); } else

{ int cnt=1; do { b=(book_t*)malloc(sizeof(book_t)); cnt=fread(b,sizeof(book_t),1,fp); if(cnt==1) { add_last(list,b); } }while(cnt==1); fclose(fp); } } node_t* find_books_by_name(list_t *list,char name_book[]) { node_t *any; int flag=0; any=list->head; if(list->head==NULL) { printf("\n List is Empty"); } else { while(any!=NULL) { if(strcmp(name_book,any->b->name)==0) { printf("\n Record Found"); flag=1; return any; } else { any=any->next; } } if(flag==0) { printf("\n Record Not Found"); return 0; } } } void find_books_by_author(list_t *list,char author_book[]) { node_t *any,*temp; int flag=0; any=list->head; if(list->head==NULL) { printf("\n List is Empty"); } else { while(any!=NULL)

{ if(strcmp(author_book,any->b->author)==0) { printf("\n Record Found"); temp=any; printf("\n%s %s %s ",temp->b->isbn,temp->b->name,te mp->b->author); printf("%s %s %lf ",temp->b->subject,temp->b->publisher,tem p->b->price); printf("%d %d \n",temp->b->total_copies,temp->b->available_c opies); } any=any->next; } } if(flag==0) { printf("\n Record Not Found"); } } void find_books_by_subject(list_t *list,char subject_book[]) { node_t *any,*temp; int flag=0; any=list->head; if(list->head==NULL) { printf("\n List is Empty"); } else { while(any!=NULL) { if(strcmp(subject_book,any->b->subject)==0) { printf("\n Record Found"); flag=1; temp=any; printf("\n%s %s %s ",temp->b->isbn,temp->b->name,te mp->b->author); printf("%s %s %lf ",temp->b->subject,temp->b->publisher,tem p->b->price); printf("%d %d \n",temp->b->total_copies,temp->b->available_c opies); } any=any->next; } } if(flag==0) { printf("\n Record Not Found"); } }

void node_display(node_t *temp) { printf("\n%s %s %s ",temp->b->isbn,temp->b->name,temp->b->author); printf("%s %s %lf ",temp->b->subject,temp->b->publisher,temp->b->price); printf("%d %d \n",temp->b->total_copies,temp->b->available_copies); } book_t* select_book(list_t *list) { char isbn1[16]; printf("\n Enter the isbn number of book you want to select "); scanf("%s",isbn1); node_t *trav; trav=search_book(list,isbn1); if(trav==0) { printf("\n Book is not available please add before selecting "); return 0; } else return trav->b; } void display_book(book_t *b) { printf("\n%s %s %s ",b->isbn,b->name,b->author); printf("%s %s %lf ",b->subject,b->publisher,b->price); printf("%d %d \n",b->total_copies,b->available_copies); } /*void load_report(list_t *rep_list,book_t *bk) { FILE *fp; fp=fopen("d://book/report.db","rb"); if(fp==NULL) { printf(" "); } else { int cnt=1; do { bk=(book_t*)malloc(sizeof(book_t)); cnt=fread(bk,sizeof(book_t),1,fp); if(cnt==1) { add_last(rep_list,bk); } }while(cnt==1); fclose(fp); } }*/ void save_report(book_t *bk) { FILE *fp; fp=fopen("d://book/report.db","ab");

fwrite(bk,sizeof(book_t),1,fp); fclose(fp); }

Potrebbero piacerti anche