luc083.c (3465B)
1 /* Library menu-driven program (Add, Display, List by Author, List by Title, Count, List sorted). 2 */ 3 /* Let Us C, Chap- 17 (Structures), Qn No.: B(f) */ 4 5 /* This file is auto-generated by a bot. */ 6 /* This code is not compiled; it is for reference only. */ 7 8 9 #include <stdio.h> 10 #include <string.h> 11 #include <stdlib.h> 12 13 struct library 14 { 15 int acc_no; 16 char title[50]; 17 char author[50]; 18 float price; 19 int is_issued; // 1 = Yes, 0 = No 20 }; 21 22 void add_book(struct library *lib, int *count); 23 void display_books(struct library *lib, int count); 24 void list_by_author(struct library *lib, int count); 25 void list_title_by_acc(struct library *lib, int count); 26 void sort_by_acc(struct library *lib, int count); 27 28 int main() 29 { 30 struct library books[100]; 31 int count = 0; 32 int choice; 33 34 while (1) 35 { 36 printf("\n--- Library Menu ---\n"); 37 printf("1. Add Book Info\n"); 38 printf("2. Display Book Info\n"); 39 printf("3. List books of given author\n"); 40 printf("4. List title of specified accession number\n"); 41 printf("5. List count of books\n"); 42 printf("6. List books in order of accession number\n"); 43 printf("7. Exit\n"); 44 printf("Enter choice: "); 45 scanf("%d", &choice); 46 47 switch (choice) 48 { 49 case 1: add_book(books, &count); break; 50 case 2: display_books(books, count); break; 51 case 3: list_by_author(books, count); break; 52 case 4: list_title_by_acc(books, count); break; 53 case 5: printf("Total books in library: %d\n", count); break; 54 case 6: sort_by_acc(books, count); display_books(books, count); break; 55 case 7: exit(0); 56 default: printf("Invalid choice!\n"); 57 } 58 } 59 return 0; 60 } 61 62 void add_book(struct library *lib, int *count) 63 { 64 printf("Enter Accession No, Title, Author, Price, Issued(1/0):\n"); 65 scanf("%d", &lib[*count].acc_no); 66 scanf("%s", lib[*count].title); // Using %s for simplicity (no spaces) 67 scanf("%s", lib[*count].author); 68 scanf("%f", &lib[*count].price); 69 scanf("%d", &lib[*count].is_issued); 70 (*count)++; 71 } 72 73 void display_books(struct library *lib, int count) 74 { 75 int i; 76 for (i = 0; i < count; i++) 77 printf("%d: %s by %s ($%.2f) Issued: %d\n", 78 lib[i].acc_no, lib[i].title, lib[i].author, lib[i].price, lib[i].is_issued); 79 } 80 81 void list_by_author(struct library *lib, int count) 82 { 83 char author[50]; 84 int i, found = 0; 85 printf("Enter author name: "); 86 scanf("%s", author); 87 for (i = 0; i < count; i++) 88 { 89 if (strcmp(lib[i].author, author) == 0) 90 { 91 printf("%s\n", lib[i].title); 92 found = 1; 93 } 94 } 95 if (!found) printf("No books found.\n"); 96 } 97 98 void list_title_by_acc(struct library *lib, int count) 99 { 100 int acc, i; 101 printf("Enter Accession No: "); 102 scanf("%d", &acc); 103 for (i = 0; i < count; i++) 104 { 105 if (lib[i].acc_no == acc) 106 { 107 printf("Title: %s\n", lib[i].title); 108 return; 109 } 110 } 111 printf("Book not found.\n"); 112 } 113 114 void sort_by_acc(struct library *lib, int count) 115 { 116 struct library temp; 117 int i, j; 118 for (i = 0; i < count - 1; i++) 119 { 120 for (j = 0; j < count - i - 1; j++) 121 { 122 if (lib[j].acc_no > lib[j + 1].acc_no) 123 { 124 temp = lib[j]; 125 lib[j] = lib[j + 1]; 126 lib[j + 1] = temp; 127 } 128 } 129 } 130 }