Topic 4 Mini Libra Create a system managing a mini library system. Every book co
ID: 2087186 • Letter: T
Question
Topic 4 Mini Libra Create a system managing a mini library system. Every book corresponds to a record (line) in a text file named "mylibrary.txt". Each record consists of 6 fields (Book ID, Title Author, Possession, Checked out Date, Due Date) separated by comma: 8 points (Group, Implemented in Mylibrary.txt 1,Book1,Author1,Library,null,null 2,Book1,Author1,User1,2014-10-22,2014-11-21 3,Book2,Author2,User1,2014-10-23,2014-11-22 4,Book3,Author2,User2,2014-10-23,2014-11-22 5,Book4,Atuhor3,User2,2014-09-22,2014-10-22 No comma ""in title or author name This mini library keeps the record for each book in the library. Different books can share the book "Title". But the "Book ID for each book is unique. One author may have multiple books. Eg. Both book2 and book3 are written by author1 Possession field is "Library" means this book is kept in library, such as book #001. Otherwise, this book is checked out by the corresponding user. Eg. book #3 is checked out by Userl. A user is allowed to check out up to 3 books. The "checked out Date" field specifies the time of the latest checked out date. The loan period for each hook is 30 days. "The De Data" filed sperifies the time the hook should be returned. This system should provide a menu with following functionalities that the bookkeeper can choose from at the top level menu: ? 1. Enter "a" to add a book into library 1) Ask for title (read) 2) Ask for author 3) Generate a book ID by adding one to the current largest book ID . 4) Add the record of the new book into library with generated book ID, title author, possession-"library", checked out date-"null" and due date "null". After that, print message "book*added successfully!" and go to step 5). 5) Provide options "t" for "Try again" and "b" for "Back to main menu", (If "TryExplanation / Answer
Copy code :
a )
#include <windows.h>
#include<stdio.h>
#include<conio.h> //contains delay(),getch()etc.
#include <stdlib.h>
#include<string.h> //contains strcmp(),strcpy(),strlen(),etc
#include<ctype.h> //contains toupper(), tolower(),etc
#include<dos.h> //contains _dos_getdate
#include<time.h>
#define RETURNTIME 15
FILE *fp,*ft,*fs;
struct l_Date
{
int mm,dd,yy;
};
struct record
{
int bookid;
char title[20];
char Author[20];
char possession[20];
struct l_Date checkedout_date;
struct l_Date due_date;
};
struct record r;
void main()
{
clrscr();
int i;
printf(" a. Add Books ");
printf("b. Delete books");
printf("c. Search Books");
printf("d. Issue Books");
printf("e. View Book list");
printf("f. Edit Book's Record");
printf("g. Close Application");
printf("Enter your choice:");
switch(getch())
{
case 'a':
addbooks();
break;
case 'b':
deletebooks();
break;
case 'c':
searchbooks();
break;
case 'd':
issuebooks();
break;
case 'e':
viewbooks();
break;
case 'f':
editbooks();
break;
case 'g':
{
clrscr();
printf("application is closing.. ");
exit(0);
}
default:
{
printf(" Please re-entered correct option");
}
}
}
void addbooks(void) //funtion to add books
{
int i;
fp=fopen("Bibek.dat","ab+");
fseek(fp,0,SEEK_END);
fwrite(&r,sizeof(r),1,fp);
fclose(fp);
printf("The record is sucessfully saved");
printf("add any more?(t / b):");//t to try again and b to back
if(getch()=='b')
else
addbooks();
}
}
void deletebooks() //function delete record from file fp
{
int d;
char another='y';
while(another=='y') //infinite loop
{
printf("Enter the Book ID to delete:");
scanf("%d",&d);
fp=fopen("Bibek.dat","rb+");
rewind(fp);
while(fread(&r,sizeof(r),1,fp)==1)
{
if(r.bookid==d)
{
printf("The book record is available");
printf("Book name is %s",r.title);
}
}
if(findbook!='t')
{
gotoxy(10,10);
printf("No record is found modify the search");
}
if(findbook=='t' )
{
printf("Do you want to delete it?(Y/N):");
if(getch()=='y')
{
ft=fopen("test.dat","wb+"); //temporary file for delete
rewind(fp);
while(fread(&r,sizeof(r),1,fp)==1)
{
if(r.id!=d)
{
fseek(ft,0,SEEK_CUR);
fwrite(&r,sizeof(r),1,ft); //write all in tempory file except that
} //we want to delete
}
fclose(ft);
fclose(fp);
remove("Bibek.dat");
rename("test.dat","Bibek.dat"); //copy all item from temporary file to fp except that
fp=fopen("Bibek.dat","rb+"); //we want to delete
if(findbook=='t')
{
printf("The record is sucessfully deleted");
printf("Delete another record?(Y/N)");
}
}
else
another=getch();
}
}
}
void searchbooks()
{
int d;
printf("*****************************Search Books*********************************");
printf(" 1. Search By ID");
printf("2. Search By Name");
printf("Enter Your Choice");
fp=fopen("Bibek.dat","rb+"); //open file for reading propose
rewind(fp); //move pointer at the begining of file
switch(getch())
{
case '1':
{
printf("****Search Books By Id****");
printf("Enter the book id:");
scanf("%d",&d);
printf("Searching........");
while(fread(&r,sizeof(r),1,fp)==1)
{
if(r.id==d)
{
printf("The Book is available");
printf(" ID:%d",r.bookid);
printf(" Name:%s",r.title);
printf(" %s ",r.Author);
findbook='t';
}
}
if(findbook!='t') //checks whether conditiion enters inside loop or not
{
printf("No Record Found");
}
printf("Try another search?(Y/N)");
if(getch()=='y')
searchbooks();
else
break;
}
case '2':
{
char s[15];
printf("****Search Books By Name****");
printf("Enter Book Name:");
scanf("%s",s);
int d=0;
while(fread(&r,sizeof(r),1,fp)==1)
{
if(strcmp(r.title,(s))==0)
{
printf("The Book is available");
printf(" ID:%d",r.bookid);
printf(" Name:%s",r.title);gotoxy(47,10);
printf(" Author:%s",r.Author);
printf("possession:%s",r.possession);
d++;
}
}
if(d==0)
{printf("No Record Found");
}
printf("Try another search?(Y/N)");
if(getch()=='y')
searchbooks();
else
break;
}
default :
getch();
searchbooks();
}
fclose(fp);
}
void editbooks(void) //edit information about book
{
int c=0;
int d,e;
printf("****Edit Books Section****");
char another='y';
while(another=='y')
{
printf("Enter Book Id to be edited:");
scanf("%d",&d);
fp=fopen("Bibek.dat","rb+");
while(fread(&r,sizeof(r),1,fp)==1)
{
if(checkid(d)==0)
{
printf("The book is availble");
printf("The Book ID:%d",r.bookid);
printf("Enter new name:");scanf("%s",r.title);
printf("Enter new Author:");scanf("%s",r.Author);
printf("The record is modified");
fseek(fp,ftell(fp)-sizeof(r),0);
fwrite(&r,sizeof(r),1,fp);
fclose(fp);
c=1;
}
if(c==0)
{
printf("No record found");
}
}
printf("Modify another Record?(Y/N)");
fflush(stdin);
another=getch() ;
}
returnfunc();
}
void returnfunc(void)
{
{
printf(" Press ENTER to return to main menu");
}
if(getch()==13) //allow only use of enter
mainmenu();
else
goto r;
}
int getdata()
{
int t;
printf("Enter the Information Below");
printf("Category:");
printf("%s",catagories[s-1]);
printf("Book ID: ");
scanf("%d",&t);
if(checkid(t) == 0)
{
printf("The book id already exists");
getch();
return 0;
}
r.bookid=t;
printf("Book Name:");
scanf("%s",r.title);
printf("Author:");
scanf("%s",r.Author);
printf("Possession:");
scanf("%d",&r.possession);
return 1;
}
int checkid(int t) //check whether the book is exist in library or not
{
rewind(fp);
while(fread(&r,sizeof(r),1,fp)==1)
if(r.id==t)
return 0; //returns 0 if book exits
return 1; //return 1 if it not
}
int t(void) //for time
{
time_t t;
time(&t);
printf("Date and time:%s ",ctime(&t));
return 0 ;
}
void Password(void)
{
char d[25]="Password Protected";
char ch,pass[10];
int i=0,j;
for(j=0;j<20;j++)
{
delay(50);
printf("*");
}
for(j=0;j<20;j++)
{
delay(50);
printf("%c",d[j]);
}
for(j=0;j<20;j++)
{
delay(50);
printf("*");
}
gotoxy(10,10);
gotoxy(15,7);
printf("Enter Password:");
while(ch!=13)
{
ch=getch();
if(ch!=13 && ch!=8){
putch('*');
pass[i] = ch;
i++;
}
}
pass[i] = '';
if(strcmp(pass,password)==0)
{
;
printf("Password match");
printf("Press any key to countinue.....");
mainmenu();
}
else
{
printf("Warning!! Incorrect Password");
getch();
Password();
}
}
void issuerecord() //display issued book's information
{
printf("The Book has taken by Mr. %s",a.stname);
printf("Issued Date:%d-%d-%d",r.issued.dd,r.checkout_date.mm,r.checkout_date.yy);
printf("Returning Date:%d-%d-%d",r.due_date.dd,r.due_date.mm,r.due_date.yy);
}
copy code :
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct library
{
?char bk_name[30];
?char author[30];
?int pages;
?float price;
};
int main()
{
?struct library l[3];
?char ar_nm,bk_nm;
?int i,j;
?printf ("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. Exit");
?printf (" Enter one of the above : ");
?scanf("%d",&j);
?switch (j)
?{
??case 1:
???for (i=0;i<3;i++)
???{
????printf ("Enter book name = ");
????scanf ("%s",&l[i].bk_name);
????printf ("Enter author name = ");
????scanf ("%s",&l[i].author);
????printf ("Enter pages = ");
????scanf ("%d",&l[i].pages);
????printf ("Enter price = ");
????scanf ("%f",&l[i].price);
???}
???break;
??case 2:
???printf("you have entered the following information ");
???for(i=0;i<3;i++)
???{
????printf ("book name = %s",l[i].bk_name);
????printf ("author name = %s",l[i].author);
????printf ("pages = %d",l[i].pages);
????printf ("price = %f",l[i].price);
???}
???break;
??case 3:
???printf ("Enter author name : ");
???scanf ("%s",&ar_nm);
???for (i=0;i<3;i++)
???{
????if (ar_nm == l[i].author)
?????printf ("%s %s %d %f",l[i].bk_name,l[i].author,l[i].pages,l[i].price);
???}
???break;
??case 4:
???printf ("Enter book name : ");
???scanf ("%s",&bk_nm);
???for (i=0;i<3;i++)
???{
????if (bk_nm == l[i].bk_name)
?????printf ("%s %s %d %f",l[i].bk_name,l[i].author,l[i].pages,l[i].price);
???}
???break;
??case 5:
???for (i=0;i<3;i++)
???{
???}
???break;
??case 6:
???exit (0);
?}
?return 0;
}
copy code :
//list of header files
#include<stdio.h> //contains printf,scanf etc
#include<conio.h> //contains delay(),getch(),gotoxy(),etc.
#include <stdlib.h> // contains exit();
#include<string.h> //contains strcmp(),strcpy(),strlen(),etc
#include<ctype.h> //contains toupper(), tolower(),etc
#include<dos.h> //contains _dos_getdate
#include<time.h>
#include<bios.h>
struct Date
{
int mm,dd,yy;
};
struct books
{
int id;
char stname[20];
char name[20];
char Author[20];
int quantity;
float Price;
int count;
struct Date issued;
struct Date duedate;
};
main()
{
clrscr();
int p,c=0;
while(another=='y')
{
printf("Enter Book ID:");
scanf("%d",&p);
checkid(int t);
if(checkid(t) == 0)
{
printf("The book id already exists");
ask current date(int);
if(ask current date(valid date) == 0)
{
printf(" date is valid");
checkcurentpaseed();
}
else
printf(" date is not valid");
returnfunc()
else
printf(" no such book exits");
check currentdate();
int checkid(int t) //check whether the book is exist in library or not
{
rewind(fp);
while(fread(&a,sizeof(a),1,fp)==1)
if(a.id==t)
return 0; //returns 0 if book exits
return 1; //return 1 if it not
}
int t(void) //for time
{
time_t t;
time(&t);
printf("Date and time:%s ",ctime(&t));
return 0 ;
}
ask current date(int valid date);
int dd, mm, yy; /* given date */
int valid date; /* flag to indicate date validity */
clrscr();
printf("Enter date as dd/mm/yyyy: ");
scanf("%d/%d/%d", &dd, &mm, &yy);
/* determine validity of given date */
valid date= 0;
if (yy != 0) /* check year */
{
if (mm >= 1 && mm <= 12) /* check month */
{
/* determine number of days in given month */
int mdays;
if (mm == 2)
mdays = (yy % 4 == 0 && yy % 100 != 0 || yy % 400 == 0) ? 29 : 28;
else if (mm == 4 || mm == 6 || mm == 9 || mm == 11)
mdays = 30;
else mdays = 31;
if (dd >= 1 && dd <= mdays)
valid date= 1;
}
}
}
checkcurentpaseed()
{
struct dosdate_t d; //for current date
_dos_getdate(&d);
a.issued.dd=d.day;
a.issued.mm=d.month;
a.issued.yy=d.year;
printf("Issued date=%d-%d-%d",a.issued.dd,a.issued.mm,a.issued.yy);
printf("The BOOK of ID %d is issued",a.id);
a.duedate.dd=a.issued.dd+RETURNTIME; //for return date
a.duedate.mm=a.issued.mm;
a.duedate.yy=a.issued.yy;
if(a.duedate.dd>30)
{
a.duedate.mm+=a.duedate.dd/30;
a.duedate.dd-=30;
}
if(a.duedate.mm>12)
{
a.duedate.yy+=a.duedate.mm/12;
a.duedate.mm-=12;
}
if(a.duedate.dd<a.issued.dd)
{
printf("due date passed");
printf('the student already paid);
update()
printf('book**** return sucessfully);
returnfunction();
}
if(days>=15)
printf('the fine is:%d',days*1)
void returnfunction(void)
{
printf( "Selections: " );
scanf( "%d", &choice);
switch ( choice ) {
case 't': /* Note the colon, not a semicolon */
printf('tryagain')
break;
case 'b':
printf('back');
break;
default:
printf( "Bad choice, quitting! " );
break;
}
}
void update(void) //edit information about book
{
clrscr();
int c=0;
int d,e;
printf("****update Books Section****");
char another='y';
while(another=='y')
{
clrscr();
printf("Enter Book Id to be edited:");
scanf("%d",&d);
fp=fopen("mylaibrary.txt","rb+");
while(fread(&a,sizeof(a),1,fp)==1)
{
if(checkid(d)==0)
{
printf("The book is availble");
printf("The Book ID:%d",a.id);
printf("Enter new name:");scanf("%s",a.name);
printf("Enter new Author:");scanf("%s",a.Author);
printf("Enter new quantity:");scanf("%d",&a.quantity);
printf("Enter new price:");scanf("%f",&a.Price);
printf("Enter new rackno:");scanf("%d",&a.rackno);
printf("The record is modified");
fseek(fp,ftell(fp)-sizeof(a),0);
fwrite(&a,sizeof(a),1,fp);
fclose(fp);
c=1;
}
if(c==0)
{
printf("No record found");
}
}
printf("Modify another Record?(Y/N)");
fflush(stdin);
another=getch() ;
}
returnfunction();
}
copy code :