Code:
#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 40
#define MAX_BOOKS 1000
//Make struct for book
struct book {
char title[MAX_LENGTH];
char author[MAX_LENGTH];
char subject[MAX_LENGTH];
};
//Make struct for library
struct library {
struct book collection[MAX_BOOKS];
int num_books;
};
//Create each of the functions
void copybook(struct book *dest, struct book *source);
void addBook(FILE *ifp, struct library *thislib, struct book b);
void removeBook(FILE *ifp, struct library *thislib, struct book *b);
void author_list(FILE *ifp, struct book *l, struct book *b, struct library *thislib);
void genre_list(FILE *ifp, struct book *l, struct book *b, struct library *thislib);
int searcher(struct book *b, struct book *l, struct library *thislib);
void printLib(struct book *l, int length);
int main (void) {
struct book shelf;
struct library myLib;
FILE *ifp;
ifp = fopen("library.txt", "r");
int cases;
fscanf(ifp, "%d", &cases);
int instruct, i;
for (i=0; i<cases; i++) {
fscanf(ifp, "%d", &instruct);
if (instruct == 1) {
addBook(ifp, &myLib, shelf);
} else if (instruct == 2) {
removeBook(ifp, &myLib, &shelf);
} else if (instruct == 3) {
int k = 0;
fscanf(ifp, "%s", &shelf.title[k]);
if (searcher(&shelf, myLib.collection, &myLib) == 1) {
printf("The book %s is currently in the library.\n\n", shelf.title);
} else {
printf("The book %s is NOT currently in the library.\n\n", shelf.title);
}
} else if (instruct == 4) {
author_list(ifp, myLib.collection, &shelf, &myLib);
} else {
genre_list(ifp, myLib.collection, &shelf, &myLib);
}
}
fclose(ifp);
system("PAUSE");
return 0;
}
//Function to copy book
void copybook(struct book *dest, struct book *source) {
strcpy(dest->title, source->title);
strcpy(dest->author, source->author);
strcpy(dest->subject, source->subject);
}
//Function to add book
void addBook(FILE *ifp, struct library *thislib, struct book b) {
fscanf(ifp, "%s%s%s", &b.title[thislib->num_books], &b.author[thislib->num_books], &b.subject[thislib->num_books]);
copybook(thislib->collection, &b);
thislib->num_books++;
printf("The book %s has been added to the library.\n\n", b.title);
}
//Function to remove book
void removeBook(FILE *ifp, struct library *thislib, struct book *b) {
int i;
fscanf(ifp, "%s", &b->title[i]);
for (i=0; i<thislib->num_books; i++) {
if (searcher(thislib->collection, b, thislib) == 1) {
}
}
thislib->num_books--;
printf("The book %s has been removed from the library.\n\n", b->title);
}
//Function to search
int searcher(struct book *b, struct book *l, struct library *thislib) {
int i;
for (i=0; i<thislib->num_books; i++) {
if (strcmp(b->title, l->title) == 0) {
return 1;
}
}
return 0;
}
//Function to scan author
void author_list(FILE *ifp, struct book *l, struct book *b, struct library *thislib) {
int i, count;
fscanf(ifp, "%s", &b->author[i]);
printf("List of all books by %s\n", b->author);
for (i=0; i<thislib->num_books; i++) {
if (strcmp(b->author, l->author) == 0) {
count++;
printf("%s\n", l->title);
}
}
}
//Function to scan genre
void genre_list(FILE *ifp, struct book *l, struct book *b, struct library *thislib) {
int i, count;
fscanf(ifp, "%s", &b->subject[i]);
printf("List of all books on %s\n", b->subject);
for (i=0; i<thislib->num_books; i++) {
if (strcmp(b->subject, l->subject) == 0) {
count++;
printf("%s\n", l->title);
}
}
}
//Funtion to print list
void printLib(struct book *l, int length) {
int i;
for (i=0; i<length; i++) {
printf("%s\n%s\n%s\n", l->title, l->author, l->subject);
}
}