fork download
  1. #include <stdio.h>
  2. #include <string.h> #define MAX 5 // Maximum number of books in the
  3. library (stack size)
  4. // Structure to represent a book
  5. struct Book {
  6. int book_id;
  7. char title[50];
  8. char author[50];
  9. };
  10. // Stack to hold books
  11. struct Book stack[MAX];
  12. int top = -1; // Stack initialization
  13. // Function to add a book to the stack
  14. void add_book(struct Book book) {
  15. if (top >= MAX - 1) {
  16. printf("Stack Overflow! Cannot add more books.\n");
  17. } else {
  18. top++;
  19. stack[top] = book;
  20. printf("Book '%s' added to the library.\n", book.title);
  21. }
  22. }
  23. // Function to issue a book (pop from stack)
  24. void issue_book() {
  25. if (top == -1) {
  26. printf("No books available to issue.\n");
  27. } else {
  28. printf("Book '%s' has been issued.\n", stack[top].title);
  29. top--; // Remove the book from the stack
  30. }
  31. }
  32. // Function to return a book (push to stack)
  33. void return_book(struct Book book) {
  34. if (top >= MAX - 1) {
  35. printf("Stack Overflow! Cannot return more books.\n");
  36. } else {
  37. top++;
  38. stack[top] = book;
  39. printf("Book '%s' returned successfully.\n", book.title);
  40. }
  41. }
  42. // Function to display all books in the library (stack)
  43. void view_books() {
  44. if (top == -1) {
  45. printf("No books available in the library.\n");
  46. } else {
  47. printf("Books available in the library:\n");
  48. for (int i = top; i >= 0; i--) {
  49. printf("ID: %d, Title: %s, Author: %s\n", stack[i].book_id, stack[i].title,
  50. stack[i].author);
  51. }
  52. }
  53. }
  54. // Main function to drive the program
  55. int main() {
  56. struct Book book1 = {1, "C Programming", "Dennis Ritchie"};
  57. struct Book book2 = {2, "Data Structures", "Robert Lafore"};
  58. struct Book book3 = {3, "Algorithms", "Thomas H. Cormen"};
  59. int choice;
  60. do {
  61. printf("\nLibrary Management System\n");
  62. printf("1. Add Book\n");
  63. printf("2. Issue Book\n");
  64. printf("3. Return Book\n");
  65. printf("4. View Books\n");
  66. printf("5. Exit\n");
  67. printf("Enter your choice: ");
  68. scanf("%d", &choice);
  69. switch(choice) {
  70. case 1:
  71. add_book(book1);
  72. break;
  73. case 2:
  74. issue_book();
  75. break;
  76. case 3:
  77. return_book(book2);
  78. break;
  79. case 4:
  80. view_books();
  81. break;
  82. case 5:
  83. printf("Exiting program.\n");
  84. break;
  85. default:
  86. printf("Invalid choice!
  87. Please try again.\n");
  88. != 5);
  89. return 0;
  90. }
Success #stdin #stdout 0.02s 26156KB
stdin
Standard input is empty
stdout
#include <stdio.h> 
#include <string.h> #define MAX 5  // Maximum number of books in the 
library (stack size) 
// Structure to represent a book 
struct Book { 
int book_id; 
char title[50]; 
char author[50]; 
}; 
// Stack to hold books 
struct Book stack[MAX]; 
int top = -1;  // Stack initialization 
// Function to add a book to the stack 
void add_book(struct Book book) { 
if (top >= MAX - 1) { 
printf("Stack Overflow! Cannot add more books.\n");     
} else { 
top++; 
stack[top] = book; 
printf("Book '%s' added to the library.\n", book.title);     
} 
} 
// Function to issue a book (pop from stack) 
void issue_book() { 
if (top == -1) { 
printf("No books available to issue.\n");     
} else { 
printf("Book '%s' has been issued.\n", stack[top].title); 
top--;  // Remove the book from the stack     
} 
} 
// Function to return a book (push to stack) 
void return_book(struct Book book) { 
if (top >= MAX - 1) { 
printf("Stack Overflow! Cannot return more books.\n");     
} else { 
top++; 
stack[top] = book; 
printf("Book '%s' returned successfully.\n", book.title);     
} 
} 
// Function to display all books in the library (stack) 
void view_books() { 
if (top == -1) { 
printf("No books available in the library.\n");     
} else { 
printf("Books available in the library:\n"); 
for (int i = top; i >= 0; i--) { 
printf("ID: %d, Title: %s, Author: %s\n", stack[i].book_id, stack[i].title, 
stack[i].author);         
} 
} 
} 
// Main function to drive the program 
int main() { 
struct Book book1 = {1, "C Programming", "Dennis Ritchie"}; 
struct Book book2 = {2, "Data Structures", "Robert Lafore"}; 
struct Book book3 = {3, "Algorithms", "Thomas H. Cormen"}; 
int choice; 
do { 
printf("\nLibrary Management System\n"); 
printf("1. Add Book\n"); 
printf("2. Issue Book\n"); 
printf("3. Return Book\n"); 
printf("4. View Books\n"); 
printf("5. Exit\n"); 
printf("Enter your choice: "); 
scanf("%d", &choice); 
switch(choice) { 
case 1:                 
add_book(book1); 
break; 
case 2:                 
issue_book(); 
break; 
case 3:                 
return_book(book2); 
break; 
case 4:                 
view_books(); 
break; 
case 5: 
printf("Exiting program.\n"); 
break; 
default: 
printf("Invalid choice! 
Please try again.\n");         
!= 5); 
return 0; 
}