/* ------- Booklist Program ----------------------------------- ** Purpose: Illustrates simple file access using standard C. ** Program inserts, updates, deletes, sorts/searches ** book records. (elements: book code, title, author, status) ** ** Copyright Notice: None. This code is intended for instructional ** purposes only. ** ** ditallop@sprynet.com 5/2/93 ** ** ------------------------------------------------------------ */ #include #include #include #define MAX_BOOKS 2000 /* number of books that can be stored in the library */ struct book_rec /* structure tag book_rec with 3 members */ { char book_code[30]; char title[30]; char author[30]; char status[30]; }; typedef struct book_rec BOOK; /* defines a new type name BOOK as a synonym for type struct book_rec */ /* function prototypes */ void initialize_book_database(BOOK []); int menu(void); void add_a_book(BOOK bk_database[]); void delete_a_book( BOOK []); void update_a_book(BOOK []); void display_book_details(BOOK *); void Search_by_code(BOOK []); void Search_by_title(BOOK []); void summary_information(BOOK []); void all_books_sort(BOOK []); void all_on_loan_sort(BOOK []); void all_not_loan_sort(BOOK []); void bubble_sort(BOOK []); int sub_menu (void); void books_search(BOOK [],char*); /* function name : main arguments : none returns : nothing purpose : controls the overall program */ void main() { BOOK book_database[MAX_BOOKS]; int menu_choice; initialize_book_database(book_database); /* clears library database */ while( (menu_choice = menu()) != 0 ){ /* main menu */ bubble_sort(book_database); /* sorts the books alphabetically by title */ switch ( menu_choice ) { case 1 : add_a_book(book_database); /* adds a book to the database */ break; case 2 : delete_a_book(book_database); /* removes a book from the database*/ break; case 3 : update_a_book(book_database); /* changes book details */ break; case 4 : Search_by_code(book_database); /* search the database for a book by code */ break; case 5 : Search_by_title(book_database); /* search the database for a book by title */ break; case 6 : all_on_loan_sort(book_database); /* search the database for all books on loan */ break; case 7 : summary_information(book_database); /* displays a summary information */ break; default : printf("\n INVALID CHOICE ENTERED"); break; } } } /* function name : initialize_book_database arguments : book_dbase[] -pointer to an array of structures which stores all books returns : nothing purpose : clears the library database by storing an empty string for all members of each structure */ void initialize_book_database( BOOK book_dbase[] ) { int i; for ( i = 0; i < MAX_BOOKS; i++ ){ strcpy( book_dbase[i].book_code , ""); /* set all the details of a book to empty strings */ strcpy( book_dbase[i].title , ""); strcpy( book_dbase[i].author , ""); strcpy( book_dbase[i].status , ""); } } /* function name : menu arguments : none returns : choice - an integer value corresponding to the user choice purpose : displays the main menu and gets the choice made */ int menu(void) { int choice = 10; printf("\n ************************************"); printf("\n WELCOME TO THE LIBRARY DATABASE MENU"); printf("\n ************************************"); printf("\n\n 1. Add a Book\n"); printf(" 2. Delete a Book\n"); printf(" 3. update a Book\n"); printf(" 4. Search a book by code\n"); printf(" 5. Search a book by title\n"); printf(" 6. Search a book by status(on loan or not)\n"); printf(" 7. display summary\n"); printf(" 0. Quit\n"); printf("\n Enter choice(integer value 0 - 7):"); scanf("%d",&choice); fflush(stdin); return choice; } /* function name : add_a_book arguments : bk_database[] -pointer to an array of structures which stores all books returns : nothing purpose : adds book details to the library database */ void add_a_book(BOOK bk_database[]) { int i = 0; char book_code[30],storage_string1[30],storage_string2[30]; while ( strcmp( bk_database[i].book_code,"" )!= 0 && i < MAX_BOOKS ) /* check if there are empty spaces in the database */ i++; if ( i == MAX_BOOKS ) printf("\n Sorry, the database is full\n"); else{ printf("\n Please enter Book Code(Maximum 30 characters): "); gets( storage_string1); fflush(stdin); if( strcmp( storage_string1,"" ) == 0 ) printf("\n INVALID ENTRY"); else{ printf( "\n Please enter Book Title(Maximum 30 characters): "); gets(storage_string2); fflush(stdin); if ( strcmp( storage_string2,"" ) == 0 ) printf("\n INVALID ENTRY"); else{ strcpy(bk_database[i].book_code,storage_string1); strcpy(bk_database[i].title,storage_string2); printf( "\n Please enter Book Author(Maximum 30 characters): "); gets(bk_database[i].author); fflush(stdin); strcpy(bk_database[i].status,"available"); fflush(stdin); printf( "\n The book has been added to the library database"); } } } } /* function name : delete_a_book arguments : book_dtb[] -pointer to an array of structures which stores all books returns : nothing purpose : deleates book details from the library database */ void delete_a_book( BOOK book_dtb[]) { int i = 0; char storage_string[30]; printf("\n Please enter book code(Maximum 30 characters): "); gets(storage_string); if( strcmp( storage_string,"" ) == 0 ) printf("\n INVALID ENTRY"); else{ while ( strcmp( book_dtb[i].book_code,storage_string)!= 0 && i < MAX_BOOKS ) /* look for the entered book code */ i++; if ( i == MAX_BOOKS ) printf("\n Sorry, the book is not in the database\n"); else{ strcpy( book_dtb[i].book_code , ""); strcpy( book_dtb[i].title , ""); strcpy( book_dtb[i].author , ""); strcpy( book_dtb[i].status , ""); printf("\n The book has been deleted from the Database"); } } } /* function name : update_a_book arguments : book_dtbase[] -pointer to an array of structures which stores all books returns : nothing purpose : changes the status of a book in the library database */ void update_a_book(BOOK book_dtbase[]) { int i = 0; char storage_string1[30],storage_string2[30]; printf("\n Please enter book code(Maximum 30 characters): "); gets(storage_string1); if( strcmp( storage_string1,"" ) == 0 ) printf("\n INVALID ENTRY"); else{ while ( strcmp( book_dtbase[i].book_code,storage_string1)!= 0 && i < MAX_BOOKS ) i++; if ( i == MAX_BOOKS ) printf("\n Sorry, the the book is not in the database\n"); else{ printf("\n The book you have chosen is:"); display_book_details( &book_dtbase[i]); printf("\n Please enter new Book Code(Maximum 30 characters): "); gets( storage_string1); fflush(stdin); if( strcmp( storage_string1,"" ) == 0 ) printf("\n INVALID ENTRY"); else{ printf( "\n Please enter new Book Title(Maximum 30 characters): "); gets(storage_string2); fflush(stdin); if ( strcmp( storage_string2,"" ) == 0 ) printf("\n INVALID ENTRY"); else{ strcpy(book_dtbase[i].book_code,storage_string1); strcpy(book_dtbase[i].title,storage_string2); printf( "\n Please enter new Book Author(Maximum 30 characters): "); gets(book_dtbase[i].author); fflush(stdin); do{ printf( "\n Enter new Status (available or on loan): "); gets(book_dtbase[i].status); fflush(stdin); } while (strcmp(book_dtbase[i].status,"available") != 0 && strcmp(book_dtbase[i].status,"on loan") != 0); printf( "\n The book has been updated"); } } } } } /* function name : display_book_details arguments : ptr - pointer to a structure which stores the book details returns : nothing purpose : displays details of a specific book */ void display_book_details(BOOK *ptr) { printf("\n book title : "); puts ( ptr->title); printf(" book code : "); puts ( ptr->book_code); printf(" book author: "); puts ( ptr->author); printf(" book status: "); puts ( ptr->status); printf("\n"); } /* function name : Search_by_code arguments : book_base[] -pointer to an array of structures which stores all books returns : nothing purpose : searches the library database for information on a book identified by a unique book code */ void Search_by_code(BOOK book_base[]) { char code[25]; int i = 0; printf("\n Please enter book code: "); gets(code); if( strcmp( code,"" ) == 0 ) printf("\n INVALID ENTRY"); else{ while( strcmp(book_base[i].book_code,code) != 0 && i 0) swap(&(books_dbs[j]),&(books_dbs[j+1])); } /* function name : swap arguments : element1_ptr - pointer to a structure that store details of a book element2_ptr - pointer to a structure that store details of a book returns : nothing purpose : function used by bubble_sort to swap the addresses of two books */ void swap(BOOK *element1_ptr,BOOK *element2_ptr) { BOOK temp; /* temporary structure to hold values being swapped */ temp = *element1_ptr; *element1_ptr = *element2_ptr; *element2_ptr = temp; } /* function name : sub_menu arguments : none returns : choice - an integer value corresponding to the user choice purpose : displays a menu of summary information to the user and reads the choice made */ int sub_menu (void) { int choice = 4; printf("\n **************************"); printf("\n Alphabetical sort by title"); printf("\n **************************"); printf("\n Enter 0 to display all books"); printf("\n Enter 1 to display all books on loan"); printf("\n Enter 2 to display all books not on loan"); printf("\n Enter 3 to return to main menu: "); scanf ("%d",&choice); fflush(stdin); return choice; } /* function name : books_search arguments : books_databs[] -pointer to an array of structures which stores all books status_strng - pointer to a string that stores the book status returns : nothing purpose : utility function used to search for a book identified by its status i.e on loan or available */ void books_search(BOOK books_databs[],char*status_strng) { int i,check = 0 ; for(i = 0; i < MAX_BOOKS; i++) if (strcmp(books_databs[i].status,status_strng) == 0 ) check ++; if (check == 0){ printf("\n The search did not find any book "); puts(status_strng); } else{ printf("\n books sorted alphabetically by title :"); puts(status_strng); for(i = 0; i < MAX_BOOKS; i++) if (strcmp(books_databs[i].status,status_strng) == 0 ){ display_book_details( &books_databs[i]); system("PAUSE"); } } }