Skip to content

Instantly share code, notes, and snippets.

@pavi2410
Created May 3, 2020 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pavi2410/51fe30bef1a406f2f4ff87d7dc5320a6 to your computer and use it in GitHub Desktop.
Save pavi2410/51fe30bef1a406f2f4ff87d7dc5320a6 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <time.h>
struct Book {
int BookNumber;
char BookTitle[100];
char Author[100];
char Publisher[100];
int YearOfPublication;
char Subject[100];
};
struct Date {
int day, month, year;
};
struct Checkout {
char Name[100];
char Address[100];
int Phone;
int BookNumber;
struct Date CheckoutDate;
struct Date ReturnDate;
};
struct Book library[2700];
int availablebookssize = 0;
struct Checkout checkoutregistry[2700];
int currentcheckoutsize = 0;
void addnewbooks();
void searchbooks();
void removeoutdatedbooks();
void checkoutbooks();
void returnbooks();
int main() {
int choice;
do {
printf("Ansprin Library\n");
printf("---------------\n");
printf("1) Add new books\n");
printf("2) Search the library by book number, author, title, subject, keywords\n");
printf("3) Remove outdated books (all books older than 15 years)\n");
printf("4) Checkout books\n");
printf("5) Return books\n");
printf("0) Exit\n");
printf("\nEnter choice: ");
scanf("%d", &choice);
getc(stdin);
switch (choice) {
case 1:
addnewbooks();
break;
case 2:
searchbooks();
break;
case 3:
removeoutdatedbooks();
break;
case 4:
checkoutbooks();
break;
case 5:
returnbooks();
break;
case 0:
goto exit;
default:
printf("Wrong choice... Exiting\n");
break;
}
printf("\n\n");
} while (choice != 0);
exit:
return 0;
}
void addnewbooks() {
struct Book newbook;
printf("Enter Book Number: ");
scanf("%d", &newbook.BookNumber);
getc(stdin);
printf("Enter Book Title: ");
gets(newbook.BookTitle);
printf("Enter Author Name: ");
gets(newbook.Author);
printf("Enter Publisher Name: ");
gets(newbook.Publisher);
printf("Enter Year Of Publication: ");
scanf("%d", &newbook.YearOfPublication);
getc(stdin);
printf("Enter Subject: ");
gets(newbook.Subject);
library[++availablebookssize] = newbook;
printf("\nBook \"%s\" is added.\n", newbook.BookTitle);
}
struct Book *findbookbybooknumber();
struct Book *findbookbyauthor();
struct Book *findbookbytitle();
struct Book *findbookbysubject();
void searchbooks() {
printf("Find book by\n");
printf("a. book number\n");
printf("b. author\n");
printf("c. title\n");
printf("d. subject\n");
printf("\nEnter choice: ");
char choice;
scanf("%c", &choice);
struct Book *book = NULL;
switch (choice) {
case 'a':
book = findbookbybooknumber();
break;
case 'b':
book = findbookbyauthor();
break;
case 'c':
book = findbookbytitle();
break;
case 'd':
book = findbookbysubject();
break;
default:
printf("Wrong choice...");
return;
}
if (book != NULL) {
printf("Found book: (%d) %s\n", book->BookNumber, book->BookTitle);
} else {
printf("No book found\n");
}
}
struct Book *findbookbybooknumber() {
int booknumber;
printf("Enter Book Number: ");
scanf("%d", &booknumber);
getc(stdin);
for (int i = 0; i < availablebookssize; ++i) {
struct Book *book = &library[i];
if (book->BookNumber == booknumber) {
return book;
}
}
return NULL;
}
struct Book *findbookbyauthor() {
char author[100];
printf("Enter Author Name: ");
gets(author);
for (int i = 0; i < availablebookssize; ++i) {
struct Book *book = &library[i];
if (strcmp(book->Author, author) == 0) {
return book;
}
}
return NULL;
}
struct Book *findbookbytitle() {
char title[100];
printf("Enter Book Title: ");
gets(title);
for (int i = 0; i < availablebookssize; ++i) {
struct Book *book = &library[i];
if (strcmp(book->BookTitle, title) == 0) {
return book;
}
}
return NULL;
}
struct Book *findbookbysubject() {
char subject[100];
printf("Enter Subject: ");
gets(subject);
for (int i = 0; i < availablebookssize; ++i) {
struct Book *book = &library[i];
if (strcmp(book->Subject, subject) == 0) {
return book;
}
}
return NULL;
}
void removebook(int index, int length);
void removeoutdatedbooks() {
time_t currenttime;
time(&currenttime);
struct tm *timeinfo = localtime(&currenttime);
int currentyear = timeinfo->tm_year;
for (int i = 0; i < availablebookssize; ++i) {
struct Book *book = &library[i];
if (book->YearOfPublication < currentyear - 15) {
removebook(i, availablebookssize);
availablebookssize--;
}
}
}
void removebook(int index, int length) {
for (int i = index; i < length - 1; i++) {
library[i] = library[i + 1];
}
}
void checkoutbooks() {
struct Checkout newco;
printf("Enter Name: \n");
gets(newco.Name);
printf("Enter Address: \n");
gets(newco.Address);
printf("Enter Phone: \n");
scanf("%d", &newco.Phone);
getc(stdin);
printf("Enter Book Number: \n");
scanf("%d", &newco.BookNumber);
getc(stdin);
printf("Enter Checkout Date (dd/mm/yyyy): \n");
int dd, mm, yyyy;
scanf("%d/%d/%d", &dd, &mm, &yyyy);
getc(stdin);
struct Date cd;
cd.day = dd;
cd.month = mm;
cd.year = yyyy;
newco.CheckoutDate = cd;
printf("\nBook %d is checked out by %s.\n", newco.BookNumber, newco.Name);
currentcheckoutsize++;
}
void returnbooks() {
int booknumber;
printf("Enter Book Number: \n");
scanf("%d", &booknumber);
getc(stdin);
for (int i = 0; i < currentcheckoutsize; ++i) {
struct Checkout *cd = &checkoutregistry[i];
if (cd->BookNumber == booknumber) {
time_t currenttime;
time(&currenttime);
struct tm *timeinfo = localtime(&currenttime);
struct Date rd;
rd.day = timeinfo->tm_mday;
rd.month = timeinfo->tm_mon;
rd.year = timeinfo->tm_year;
cd->ReturnDate = rd;
printf("\nReturn date for Book %d is recorded.\n", booknumber);
break;
}
}
printf("\nNot found\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment