Problem: Personal Library You are quite an avid reader and would like to write a
ID: 3820836 • Letter: P
Question
Problem: Personal Library
You are quite an avid reader and would like to write a program that manages your collection of books. In particular, you want the following functionality:
1) Search for a book from the collection a) If the book is not found your program should return an appropriate message b) otherwise, this function returns the number of copies of the book in the collection
2) List all of the books by a particular author from the collection
Your program will create the library as an array of structs, from entries for each book read from an input file called “library.txt”. Your library will never exceed 100 books.
Input Specification (for library.txt)
The first line of the file will contain a single positive integer representing the number of books in the library. (Note: We assume that at the beginning of the program, the library is empty.) Each following line will have data for a single book in the library. Book data is provided in the following format:
TITLE AUTHOR_fname AUTHOR_lname Number_of_copies
The title, author_firstname and author_lastname will be strings that contain alphabetic letters and underscores. None of these strings will exceed 39 characters. The title will be unique to the collection. The library may contain multiple books by the same author, and multiple copies of the same book, but no two book entries (book structs in your program) will ever have the same title. You may assume that the Number_of_copies will indicate how many copies of that book are in the library.
Output Specification
All output should go to the screen. Separate the output for each processed request with one blank line. 2 For searching for a book, output one of two statements, depending on whether or not the designated book was found in the library: There are n copies of book X currently in the library. The book X is NOT currently in the library. For the book to be found, the title must match exactly, including capitalization. For searching for all the books by a particular author, output a single line of the form: List of all books by X X represents the author requested. Each following line should list the title of one book by that author in the library. List each book exactly once.
Implementation Restrictions
You must use the following constants: #define MAX_LENGTH 40 #define MAX_BOOKS 100 You must use the following struct to store information about one book: struct book { char title[MAX_LENGTH]; char authorfname[MAX_LENGTH]; char authorlname[MAX_LENGTH]; int numcopies; }; When run, your program will: 1. Create the library 2. Prompt the user to select one of 2 choices: a. title of a book to search for b. an author name to search for 3. Your program should then process the users selected choice. (it is up to you to plan how you will implement this part) 4. Your program should repeat steps 2-3 until the user is done.
Hint: Remember to use strcpy whenever you want to copy the contents of one string into another, instead of the equals sign. Whenever you list all of the books by a particular author, list them in the order in which they appear in the library, by index into the array in the struct library
ANSWER SHOULD BE IN CODE LANGUAGE C
Explanation / Answer
#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 40
#define MAX_BOOKS 100
#define FILE_NAME "Library.txt"
int total_record = 0;
struct book {
char title[MAX_LENGTH];
char authorfname[MAX_LENGTH];
char authorlname[MAX_LENGTH];
int numcopies;
}books[MAX_BOOKS];
void createLibrary() {
FILE *f = fopen(FILE_NAME, "w");
if (f == NULL)
{
printf("Error opening file! ");
}
fprintf(f, "10 ");
fprintf(f, "Narendra_Modi:_A_Political_Biography Andy Marino 150 ");
fprintf(f, "Unaccustomed_Earth Jhumpa Lahiri 40 ");
fprintf(f, "Interpreter_of_Maladies Jhumpa Lahiri 60 ");
fprintf(f, "Lowland Jhumpa Lahiri 20 ");
fprintf(f, "India_at_Risk Jaswant Singh 50 ");
fprintf(f, "Half_Girlfriend Chetan Bhagat 120 ");
fprintf(f, "Black_Tornado Sandeep Unnithan 170 ");
fprintf(f, "Making_India_Awesome Chetan Bhagat 60 ");
fprintf(f, "Modi Tarun Vijay 80 ");
fprintf(f, "Food_for_All Uma Lele 100 ");
fclose(f);
}
void readLibrary() {
FILE *f = fopen(FILE_NAME, "r");
if (f == NULL)
{
printf("Error opening file! ");
} else {
total_record = -1;
int record = 0;
char line[256];
while (fgets(line, sizeof(line), f)) {
if(total_record == -1) {
total_record = atoi(line);
} else {
int i=0;
char *p = strtok (line, " ");
while (p != NULL)
{
if(i == 0) {
strcpy(books[record].title, p);
} else if(i == 1) {
strcpy(books[record].authorfname, p);
} else if(i == 2){
strcpy(books[record].authorlname, p);
} else if(i == 3){
books[record].numcopies = atoi(p);
}
i++;
p = strtok (NULL, " ");
}
}
record ++;
}
}
fclose(f);
}
void searchBook(char book_name[MAX_LENGTH]) {
int i=0;
for(i=0; i<=total_record; i++) {
if (strcmp(books[i].title, book_name) == 0) {
printf(" There are %d copies of book %s currently in the library", books[i].numcopies, books[i].title);
return;
}
}
printf(" The book %s not currently in the library", book_name);
return;
}
void listBooks(char auth_name[MAX_LENGTH]) {
int i=0;
printf(" List of all books by %s represents the author requested", auth_name);
for(i=0; i<=total_record; i++) {
char *nm = strcat(books[i].authorfname, " ");
nm = strcat(nm, books[i].authorlname);
printf(" %s", auth_name);
if (strcmp(nm, auth_name) == 0) {
printf(" %s", books[i].title);
}
}
}
int main()
{
// Create Library.Write data to file
createLibrary();
// Read data from library
readLibrary();
int option = 0;
char book_name[MAX_LENGTH];
while(option != 3) {
// Prompt user to make choice
printf (" -------------------------------------------- ");
printf (" 1. Search book from all book by title ");
printf (" 2. List all books by auther name ");
printf (" 3. Exit ");
printf (" -------------------------------------------- ");
printf (" Choose option from above : ");
scanf ("%d", &option);
switch(option) {
case 1 : // Search book from all book by title
printf (" Enter book name : ");
scanf ("%s", &book_name);
searchBook(book_name);
break;
case 2 : // List all books by auther name
printf (" Enter author name : ");
scanf ("%s", &book_name);
listBooks(book_name);
break;
case 3 :
return 0;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.