-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample5.c
82 lines (68 loc) · 2.52 KB
/
sample5.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdio.h>
#include <string.h>
#define MAX_BOOKS 10
struct Book {
int accessionNumber;
char author[50];
char title[100];
int isIssued;
};
void displayBookInfo(struct Book book) {
printf("Accession Number: %d\n", book.accessionNumber);
printf("Author: %s\n", book.author);
printf("Title: %s\n", book.title);
printf("Is Issued: %s\n", book.isIssued ? "Yes" : "No");
printf("\n");
}
void addNewBook(struct Book library[], int *totalBooks, int accessionNumber, const char *author, const char *title) {
if (*totalBooks < MAX_BOOKS) {
library[*totalBooks].accessionNumber = accessionNumber;
strcpy(library[*totalBooks].author, author);
strcpy(library[*totalBooks].title, title);
library[*totalBooks].isIssued = 0;
(*totalBooks)++;
} else {
printf("Library is full. Cannot add more books.\n");
}
}
void displayBooksByAuthor(struct Book library[], int totalBooks, const char *author) {
printf("Books by Author '%s':\n", author);
for (int i = 0; i < totalBooks; i++) {
if (strcmp(library[i].author, author) == 0) {
displayBookInfo(library[i]);
}
}
}
int countBooksByTitle(struct Book library[], int totalBooks, const char *title) {
int count = 0;
for (int i = 0; i < totalBooks; i++) {
if (strcmp(library[i].title, title) == 0) {
count++;
}
}
return count;
}
void displayAllBooks(struct Book library[], int totalBooks) {
printf("Library Book Information:\n");
for (int i = 0; i < totalBooks; i++) {
displayBookInfo(library[i]);
}
}
int main() {
struct Book library[MAX_BOOKS];
int totalBooks = 0;
// Add new books
addNewBook(library, &totalBooks, 1, "John Doe", "Introduction to Programming");
addNewBook(library, &totalBooks, 2, "Jane Smith", "Data Structures and Algorithms");
addNewBook(library, &totalBooks, 3, "Alice Johnson", "C Programming for Beginners");
// Display all books
displayAllBooks(library, totalBooks);
// Display books by author
displayBooksByAuthor(library, totalBooks, "Jane Smith");
// Display number of books by title
int count = countBooksByTitle(library, totalBooks, "Introduction to Programming");
printf("Number of books with title 'Introduction to Programming': %d\n", count);
// Display total number of books
printf("Total number of books in the library: %d\n", totalBooks);
return 0;
}