bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

luc083.c (3656B)


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