c micro project
c micro project
h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
struct Book
{
int id;
char title[100];
int price;
int qty;
char category[100];
int isBorrowed;
char borrower[100];
struct Book* next;
};
struct Book* start = NULL;
int createOrInsert(int id, char title[], int price, int qty, char category[])
{
struct Book* temp = (struct Book*)malloc(sizeof(struct Book));
temp->id = id;
strcpy(temp->title, title);
temp->price = price;
temp->qty = qty;
strcpy(temp->category, category);
if (start == NULL)
{
temp->next = temp;
start = temp;
} else {
struct Book* q = start;
while (q->next != start)
{
q = q->next;
}
q->next = temp;
temp->next = start;
}
return 1;
}
void countBooks()
{
int count = 0;
struct Book* q = start;
if (q == NULL) {
printf("The list is empty.\n");
return;
}
do {
count++;
q = q->next;
} while (q != start);
printf("List of Books:\n");
printf("------------------------------------------------------------\n");
printf("| ID | Title | Price | Qty | Category |\n");
printf("------------------------------------------------------------\n");
do {
printf("| %d | %s | %d | %d | %s |\n", q->id, q->title, q-
>price, q->qty, q->category);
q = q->next;
} while (q != start);
printf("------------------------------------------------------------\n");
}
if (q == NULL)
{
printf("The library is empty. No books to search.\n");
return;
}
while (1) {
if (q->id == bookID)
{
printf("Book found:\n");
printf("------------------------------------------------------------\
n");
printf("| %-4d | %-3s | %-6d | %-4d | %-15s |\n", q->id, q->title, q-
>price, q->qty, q->category);
printf("------------------------------------------------------------\
n");
return;
}
q = q->next;
if (q == start)
{
printf("Book with ID %d not found in the library.\n", bookID);
return;
}
}
}
if (!found) {
printf("Book with ID %d not found in the library.\n", bookID);
}
}
float calculateBorrowedBookFee(int bookID, int daysLate) {
if (!found) {
printf("Book with ID %d not found in the library.\n", bookID);
}
}
void sortBooksByID() {
struct Book *q= start;
struct Book *index = NULL;
int temp;
if (start == NULL) {
return;
} else {
do {
index = q->next;
int main()
{
int choice;
int id, price, qty;
char title[100], category[100];
int bookID1, bookID2;
printf("*********Library Management System*********\n");
while (1) {
printf("MENU:\n");
printf("1. Add books\n");
printf("2. Display all books\n");
printf("3. Count added books\n");
printf("4. Search for a book\n");
printf("5. Borrow a book\n");
printf("6. Calculate borrowed book fee\n");
printf("7. Sort books by ID\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
printf("____________________________________\n");
int numBooks, i;
printf("Enter the number of books to add: ");
scanf("%d", &numBooks);
if (result)
{
printf("BOOK ADDED SUCESSFULLY.\n");
printf("____________________________________\n");
} else {
printf("Failed to add the book.\n");
}
}
break;
}
case 2:
printf("____________________________________\n");
displayBooks();
break;
case 3:
printf("____________________________________\n");
countBooks();
printf("____________________________________\n");
break;
case 4:
printf("____________________________________\n");
printf("Enter the ID of the book you want to search: ");
scanf("%d", &id);
searchBook(id);
break;
case 5:
char personName[100];
printf("____________________________________\n");
printf("Enter the ID of the book you want to borrow: ");
scanf("%d", &id);
printf("Enter your name: ");
scanf("%s", &personName);
borrowBook(id, personName);
break;
case 6:
{
int bookID;
printf("Enter the ID of the borrowed book: ");
scanf("%d", &bookID);
int daysLate;
printf("Enter the number of days late: ");
scanf("%d", &daysLate);
calculateBorrowedBookFee(bookID, daysLate);
break;
}
case 7:
default:
printf("Invalid choice. Please select a valid option.\n");
break;
}
}
return 0;
}