Hello, I was hoping to get some help regarding this series of questions. I have
ID: 3697512 • Letter: H
Question
Hello, I was hoping to get some help regarding this series of questions. I have provided the assignment as well as each of the external files below, thank you very much for any assistance you can offer.
The semester project Library Book Management System will be based on the data type struct book as defined below.
struct book{
int ID;
char Title[50];
char Author[50];
int SubjectCode;
int BorrowingAge;
char BorrowerName[50];
struct book *nextbook;
};
You will need to download 6 files they are prj.h, prj1.h, prj2.h, proj_main.c, prj1.c and library.c. The “main” is located in proj_main.c, it does not need to be modified. All the code you are writing will need to be placed in prj1.c. In library.c, I have written a number of functions. The prototypes for my functions are in prj2.h.
Each book that is in the library list will have a ID, Title, Author Name, Subject Code, Borrowing Age (reflects the number of days passed after it has been borrowed from the library), name of the Borrower and a pointer nextBook which points to a struct book.
Subject code for different subjects as follows:
000 – General works, Computer science and Information, 100 – Philosophy and psychology, 200 – Religion, 300 – Social sciences, 400 – Language, 500 – Pure Science, 600 – Technology, 700 – Arts & recreation, 800 – Literature, 900 – History & geography
The following functions will be available and written by the instructor:
void assignData(struct book *);
struct book * genNewBook(struct book *);
struct book * sortByID (struct book * head);
You are to write the following functions
struct book * processBook (struct book * head);
void displayList (struct book * head);
struct book * findBookbyID (struct book * head, int ID);
struct book * findBookbyKeywords (struct book * head, char * keywords);
struct book * addNewbook (struct book * head);
struct book * deleteBook (struct book * head, int ID_to_go);
void sendReminder (struct book * head);
struct book * addReturnedBook(struct book * head, int return_book_ID);
struct book * sortByAlphabeticalOrder (struct book * head);
void subjectSummary (struct book * head);
void terminateWrite (struct book * head);
struct book * freeList(struct book * head);
Main() will prompt the user to enter a command. The command menu looks like the following:
P Process Book
D Display List
A Add New Book
L Delete Book
R Send Reminder
B Add Returned Book
O Sort list by Alphabetical Order
T Sort list by ID
J Subject Summary
Q Terminate
Functional descriptions of the functions you need to write:
struct book * processBook (struct book * head);
Here you are to process a book when a user wants to borrow it. User can ask for a book either by the ID or by keywords. Ask the user which method he/she wants to use then call your findBookbyID or findBookbyKeywords functions accordingly. If that book is found in the library then you issue that book to that user. Ask for the user name and it should get stored in the Borrower field. Borrowing Age (reflects the number of days it has been with the user) field should be changed from the default 0 value to 1. If the book is not found in the library or it has been already borrowed by someone else tell user that particular reason. Finally display your updated list.
void displayList (struct book * head);
Here you are to print to the screen each book in the library list in an organized and formatted manner. Remember to print a header for each element.
struct book * findBookbyID (struct book * head, int ID);
In this function you are passed an ID and you return the address of the book with this ID. If the book is not in the list tell user about it and you return NULL.
struct book * findBookbyKeywords (struct book * head,char * keywords);
In this function you are passed one or two words from a book title and you return the address of the book which has these words in its title. If no book with a title having the keywords is found tell user about it and you return NULL. (Hint: Check for substring)
struct book * addNewbook (struct book * head);
Here you are to use malloc() to generate a struct book and call assignData that will assign all the info for this new book. You should add the book into the link list. New books should go into the list so that the list is sorted according to ID.
struct book * deleteBook (struct book * head, int ID_to_go);
In this function you are passed an ID, find the node book_to_go from the link list that corresponds to that ID, then delete that node in a manner that reattaches the necessary links to preserve the link list, then you free the memory for book _to_go. If the ID is invalid then display a message to the user that it is an invalid ID and go back to the main menu.
void sendReminder (struct book * head);
A book can remain with the borrower for maximum 14 days. On 10th day the borrower should get a reminder message. In this function you will look for the books with borrowing age 10 and should display a reminder message to its borrower. For example your display window should print a message like this: “Jerry lee, you borrowed the book C programming 10 days ago. You have 4 days left to return it.”
struct book * addReturnedBook(struct book * head, int return_book_ID);
In this function you are passed the ID of a previously borrowed book. Find the node from the link list that corresponds to that ID. You are to erase the borrower name and set the borrowed age back to zero for that book.
struct book * sortByAlphabeticalOrder (struct book * head);
Here you are to sort the book list according to the Title field (in increasing alphabetical order). You should return a pointer to the beginning of the link list.
void subjectSummary (struct book * head);
In this function you are to count and display the number of books you have in the library for different subjects (do this using the subject codes). The books borrowed by the users should not get included in the count.
void terminateWrite (struct book * head);
If the user enter this command you are to write all the books in the list to a file called Booklist.txt. All fields of the structure for each book should be written to an individual line. After writing you should call freeList and then the program will terminate.
struct book * freeList(struct book * head);
Here you should free all the elements of your list and return NULL to main.
proj_main:
prj1.c:
library.c:
prj1.h:
prj2.h
prj.h:
Explanation / Answer
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
//defining the properties of the fields used in the program
#define IN 1
#define OUT 0
void Addbook();
void Searchbook();
void Displaybook();
void Author();
void Titlelist();
void Stock();
void Issue();
void bookret();
void Addmembr();
void Exit();
char info[500];
struct
{
int bid;
char bname[25] ;
char author[25];
int nooftitles;
char titles[500];
int status;
}book;
struct
{
int mid;
char mname[25] ;
char department[25];
int availibcard;
int phno;
}membr;
//initializing the files used in the program
FILE *librecord;
FILE *membrrecord;
FILE *fp1;
FILE *fp2;
FILE *temp1;
FILE *temp2;
int main()
{
int choice=0,i;
printf(" <<LIBRARY MANAGEMENT SYSTEM>>(Beta version ) ");
do{
printf(" ~~MENU~~ 1> Add A New Book 2> Search a book 3> Display Complete Information 4> Display All Books of An Author 5> List Titles of a Book 6> List Count of Books (Issued & On Stock) 7> To Issue a Book 8> To Rreturn a Book 9> Add A New Member 10> Exit the program Enter your choice <1-10>: ");
scanf("%i",&choice);
switch (choice)
{
case 1:
Addbook();
break;
case 2:
Searchbook();
break;
case 3:
Displaybook();
break;
case 4:
Author();
break;
case 5:
Titlelist();
break;
case 6:
Stock();
break;
case 7:
Issue();
break;
case 8:
bookret();
break;
case 9:
Addmembr();
break;
case 10:
Exit();
default:
printf(" ! Invalid Input... ");
}
}while(choice!=10);
return (0);
}
void Addbook()
{
int i;book.status=IN;
//opening the librecord file
librecord = fopen("librecord.txt","a+");
printf("Enter The uniqueid of The Book :(Integer) ");
scanf("%d",&book.bid);
printf("Enter The Name of The Book : ");
scanf("%s",book.bname);
printf("Enter The Name of Author : ");
scanf("%s",book.author);
printf("Enter The Number of Titles Of The Book:(Integer) ");
scanf("%d",&book.nooftitles);
fprintf(librecord," %d %s %s %d %d ",book.bid,book.bname,book.author,book.status,book.nooftitles);
printf("Enter The Titles Of The Book : ");
for(i=0;i<book.nooftitles;i++)
{
scanf("%s",book.titles);
fprintf(librecord,"%s ",book.titles);
}
fclose(librecord);
printf(" (' ' ) A New Book has been Added Successfully... ");
}
void Displaybook()
{
librecord = fopen("librecord.txt","a+");
printf(" Bookid Name Author Status No. Titles ",info);
do
{
fgets(info,500,librecord);
printf("%s ",info);
}while(!feof(librecord));
fclose(librecord);
membrrecord = fopen("membrrecord.txt","a+");
printf(" Mid Name Dept Ph.no Availablecards ");
do
{
fgets(info,500,membrrecord);
printf("%s ",info);
}while(!feof(membrrecord));
fclose(membrrecord);
}
void Searchbook()
{
int i;
char Target[25],stats[3];
int Found=0;
if((librecord=fopen("librecord.txt","r"))==NULL)
printf(" ! The File is Empty... ");
else
{
printf(" Enter The Name Of Book : ");
scanf("%s",Target);
while(!feof(librecord)&& Found==0)
{
fscanf(librecord,"%d %s %s %d %d", &book.bid,book.bname,book.author,&book.status,&book.nooftitles);
if(strcmp(Target,book.bname)==0)
Found=1;
for(i=0;i<book.nooftitles;i++)
fscanf(librecord,"%s",book.titles);
}
if(Found)
{
if(book.status==IN)
strcpy(stats,"IN");
else
strcpy(stats,"OUT");
printf(" The Unique ID of The Book: %d The Name of Book is: %s The Author is: %s The Book Status:%s ",book.bid,book.bname,book.author,stats);
}
else if(!Found)
printf("! There is no such Entry... ");
fclose(librecord);
}
}
void Author()
{
int i;
char Target[500];
int Found=0;
if((librecord=fopen("librecord.txt","r"))==NULL)
printf(" ! The file is empty... ");
else
{
printf(" Enter The Name Of Author : ");
scanf("%s",Target);
printf(" Books:");
while(!feof(librecord))
{
fscanf(librecord,"%d %s %s %d %d",&book.bid,book.bname,book.author,&book.status,&book.nooftitles);
if(strcmp(Target,book.author)==0)
{
Found=1;
printf(" %s",book.bname);
}
for(i=0;i<book.nooftitles;i++)
fscanf(librecord,"%s",book.titles);
}
if(!Found)
printf(" ! There is no such Entry... ");
fclose(librecord);
}
}
void Titlelist()
{
int i;
char Target[500];
int Found=0;
if((librecord=fopen("librecord.txt","r"))==NULL)
printf(" ! The file is empty... ");
else
{
printf(" Enter The Book Name :");
scanf("%s",Target);
while(!feof(librecord)&& Found==0)
{
fscanf(librecord,"%d %s %s %d %d",&book.bid,book.bname,book.author,&book.status,&book.nooftitles);
if(strcmp(Target,book.bname)==0)
{
Found=1;
break;
}
for(i=0;i<book.nooftitles;i++)
fscanf(librecord,"%s",book.titles);
}
if(Found)
{
//printf("The Name of book is: %s ",book.bname);
printf(" The Titles: ");
for(i=0;i<book.nooftitles;i++)
{
fscanf(librecord,"%s",book.titles);
printf("%d.%s...... ",i+1,book.titles);
}
}
else if(!Found)
printf(" ! There is no such Entry... ");
fclose(librecord);
}
}
void Stock()
{
int i,issuecount=0,stockcount=0;
char Issued[100][20];
int Found=0;
if((librecord=fopen("librecord.txt","r"))==NULL)
printf(" ! The file is empty... ");
else
{
while(!feof(librecord))
{
fscanf(librecord,"%d %s %s %d %d",&book.bid,book.bname,book.author,&book.status,&book.nooftitles);
if(book.status==IN)
{
stockcount++;
}
else
{
issuecount++;
}
for(i=0;i<book.nooftitles;i++)
fscanf(librecord,"%s",book.titles);
}
fclose(librecord);
printf(" Count of issued Books:%d Count of Books in Stock:%d ",issuecount,stockcount-1);
}
}
void Addmembr()
{
int i;
membrrecord = fopen("membrrecord.txt","a+");
printf("Enter The userid of the Member(Integer) : ");
scanf("%d",&membr.mid);
printf("Enter The Name of the Member : ");
scanf("%s",membr.mname);
printf("Enter The Department ");
scanf("%s",membr.department);
printf("Enter The phone number of the member: ");
scanf("%d",&membr.phno);
membr.availibcard=5;
fprintf(membrrecord," %d %s %s %d %d ",membr.mid,membr.mname,membr.department,membr.phno, membr.availibcard);
fclose(membrrecord);
printf(" (' ') Added A New member Successfully... ");
}
void Issue()
{
int mid,i,Found1=0,Found2=0;char issubookname[20];
//temp1=librecord;temp2=membrrecord;
printf(" Enter The userid of the Member : ");
scanf("%d",&mid);
if((membrrecord=fopen("membrrecord.txt","r"))==NULL)
printf(" ! The file is empty... ");
else
{
while(!feof(membrrecord)&& Found1==0)
{
fscanf(membrrecord,"%d %s %s %d %d ",&membr.mid,membr.mname,membr.department,&membr.phno,&membr.availibcard);
if(mid==membr.mid)
{
Found1=1;
}
}
if(Found1)
{
if(membr.availibcard<1)
{
printf(" ! Library card not available... ");
}
else
{ printf(" Enter The Name of book :");
scanf("%s",issubookname);
if((librecord=fopen("librecord.txt","r"))==NULL)
printf(" ! The file is empty... ");
else
{
while(!feof(librecord)&& Found2==0)
{
fscanf(librecord,"%d %s %s %d %d", &book.bid,book.bname,book.author,&book.status,&book.nooftitles);
if(strcmp(issubookname,book.bname)==0)
Found2=1;
for(i=0;i<book.nooftitles;i++)
fscanf(librecord,"%s",book.titles);
}
if(Found2)
{
if(book.status==0)
{
printf(" ! Book already issued... ");
}
else
{
fp2=fopen("fp2.txt","w");
if((temp2=fopen("membrrecord.txt","r"))==NULL)
printf(" ! The file is empty... ");
else
{
while(!feof(temp2))
{
fscanf(temp2,"%d %s %s %d %d ",&membr.mid,membr.mname,membr.department,&membr.phno,&membr.availibcard);
if(mid==membr.mid)
{
membr.availibcard--;
fprintf(fp2," %d %s %s %d %d ",membr.mid,membr.mname,membr.department,membr.phno, membr.availibcard);
}
else{
fprintf(fp2," %d %s %s %d %d ",membr.mid,membr.mname,membr.department,membr.phno,membr.availibcard);}
if(feof(temp2))
break;
}
}
fclose(temp2);
fclose(fp2);
fp1=fopen("fp1.txt","w");
if((temp1=fopen("librecord.txt","r"))==NULL)
printf(" ! The file is empty... ");
else
{
while(!feof(temp1))
{
fscanf(temp1,"%d %s %s %d %d", &book.bid,book.bname,book.author,&book.status,&book.nooftitles);
if(feof(temp1))
break;
if(strcmp(issubookname,book.bname)!=0)
{
fprintf(fp1," %d %s %s %d %d ",book.bid,book.bname,book.author,book.status,book.nooftitles);
}
else
{
fprintf(fp1," %d %s %s %d %d ",book.bid,book.bname,book.author,0,book.nooftitles);
}
for(i=0;i<book.nooftitles;i++)
{
fscanf(temp1,"%s",book.titles);
fprintf(fp1,"%s ",book.titles);
}
}
}
fclose(temp1);
fclose(fp1);
fclose(librecord);
fclose(membrrecord);
remove("librecord.txt");
rename("fp1.txt","librecord.txt");
remove("membrrecord.txt");
rename("fp2.txt","membrrecord.txt");
printf(" (' ') Book Successfully issued... ");
}
}
else if(!Found2)
printf(" ! There is no such Book... ");
}
}
}
else if(!Found1)
printf(" ! Invalid User id... ");
}
}
void bookret()
{
int mid,i,Found1=0,Found2=0,flag=0;char retbookname[20];
temp1=librecord;temp2=membrrecord;
printf(" Enter The userid of the Member : ");
scanf("%d",&mid);
if((membrrecord=fopen("membrrecord.txt","r"))==NULL)
printf(" ! The file is empty... ");
else
{
while(!feof(membrrecord)&& Found1==0)
{
fscanf(membrrecord,"%d %s %s %d %d ",&membr.mid,membr.mname,membr.department,&membr.phno,&membr.availibcard);
if(mid==membr.mid)
{
Found1=1;
}
}
if(Found1)
{
if(membr.availibcard>=5)
{
printf(" ! Error... ");
}
else
{ printf(" Enter The Name of book :");
scanf("%s",retbookname);
if((librecord=fopen("librecord.txt","r"))==NULL)
printf(" ! The file is empty ");
else
{
while(!feof(librecord)&& Found2==0)
{
fscanf(librecord,"%d %s %s %d %d", &book.bid,book.bname,book.author,&book.status,&book.nooftitles);
if(strcmp(retbookname,book.bname)==0)
Found2=1;
for(i=0;i<book.nooftitles;i++)
fscanf(librecord,"%s",book.titles);
}
if(Found2)
{
if(book.status==1)
{
printf(" ! Error:Book already in stock... ");
}
else
{
fp2=fopen("fp2.txt","w");
if((temp2=fopen("membrrecord.txt","a+"))==NULL)
printf(" ! The file is empty... ");
else
{
while(!feof(temp2))
{
fscanf(temp2,"%d %s %s %d %d ",&membr.mid,membr.mname,membr.department,&membr.phno,&membr.availibcard);
if(mid==membr.mid)
{
membr.availibcard++;
fprintf(fp2," %d %s %s %d %d ",membr.mid,membr.mname,membr.department,membr.phno, membr.availibcard);
}
else
{
fprintf(fp2," %d %s %s %d %d ",membr.mid,membr.mname,membr.department,membr.phno,membr.availibcard);
}
if(feof(temp2))
break;
}
}
fclose(temp2);
fclose(fp2);
fp1=fopen("fp1.txt","w");
if((temp1=fopen("librecord.txt","r"))==NULL)
printf(" ! The file is empty... ");
else
{
while(!feof(temp1))
{
fscanf(temp1,"%d %s %s %d %d", &book.bid,book.bname,book.author,&book.status,&book.nooftitles);
if(feof(temp1))
break;
if(strcmp(retbookname,book.bname)!=0)
{
fprintf(fp1," %d %s %s %d %d ",book.bid,book.bname,book.author,book.status,book.nooftitles);
}
else
{
fprintf(fp1," %d %s %s %d %d ",book.bid,book.bname,book.author,1,book.nooftitles);
}
for(i=0;i<book.nooftitles;i++)
{
fscanf(temp1,"%s",book.titles);
fprintf(fp1,"%s ",book.titles);
}
}
}
fclose(temp1);
fclose(fp1);
fclose(librecord);
fclose(membrrecord);
printf("('') Book Successfully Returned... ");
remove("librecord.txt");
rename("fp1.txt","librecord.txt");
remove("membrrecord.txt");
rename("fp2.txt","membrrecord.txt");
}
}
else if(!Found2)
printf("! There is no such Book... ");
}
}
}
else if(!Found1)
printf("! Invalid User id... ");
}
}
void Exit()
{
exit(0);
}
note- the above code logic can help to answer the given question.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.