-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLIBRARY.c
444 lines (415 loc) · 13.2 KB
/
LIBRARY.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h> // For getch()
#define MAX_BOOKS 100
#define MAX_USERS 10
#define USER_FILE "users.txt"
#define BOOK_FILE "books.txt"
// Structure to store book information
typedef struct {
char title[100];
char author[100];
char ISBN[20];
} Book;
// Structure to store user information
typedef struct {
char username[50];
char hashedPassword[100];
int isAdmin;
} User;
Book library[MAX_BOOKS];
User users[MAX_USERS];
int bookCount = 0;
int userCount = 0;
// Simple hash function for demonstration (not secure for real applications)
void hashPassword(const char *password, char *hashedPassword) {
int i;
for (i = 0; password[i] != '\0'; i++) {
hashedPassword[i] = password[i] + 1; // Simple Caesar cipher
}
hashedPassword[i] = '\0';
}
// Function to hide password input
void getPassword(char *password) {
int i = 0;
char ch;
while ((ch = getch()) != '\r') { // Enter key is pressed
if (ch == 8) { // Backspace key is pressed
if (i > 0) {
i--;
printf("\b \b");
}
} else {
password[i++] = ch;
printf("*");
}
}
password[i] = '\0';
printf("\n");
}
// Function to load users from file
void loadUsers() {
FILE *file = fopen(USER_FILE, "r");
if (file == NULL) {
printf("No user file found. Starting with default admin user.\n");
strcpy(users[userCount].username, "admin");
hashPassword("admin123", users[userCount].hashedPassword);
users[userCount].isAdmin = 1;
userCount++;
return;
}
while (fscanf(file, "%s %s %d", users[userCount].username, users[userCount].hashedPassword, &users[userCount].isAdmin) != EOF) {
userCount++;
}
fclose(file);
}
// Function to save users to file
void saveUsers() {
FILE *file = fopen(USER_FILE, "w");
for (int i = 0; i < userCount; i++) {
fprintf(file, "%s %s %d\n", users[i].username, users[i].hashedPassword, users[i].isAdmin);
}
fclose(file);
}
// Function to load books from file
void loadBooks() {
FILE *file = fopen(BOOK_FILE, "r");
if (file == NULL) {
printf("No book file found. Starting with an empty library.\n");
return;
}
while (fscanf(file, "%[^\n]%*c %[^\n]%*c %[^\n]%*c", library[bookCount].title, library[bookCount].author, library[bookCount].ISBN) != EOF) {
bookCount++;
}
fclose(file);
}
// Function to save books to file
void saveBooks() {
FILE *file = fopen(BOOK_FILE, "w");
for (int i = 0; i < bookCount; i++) {
fprintf(file, "%s\n%s\n%s\n", library[i].title, library[i].author, library[i].ISBN);
}
fclose(file);
}
// Function to authenticate user
int loginUser(char *username, char *password) {
char hashedPassword[100];
hashPassword(password, hashedPassword);
for (int i = 0; i < userCount; i++) {
if (strcmp(users[i].username, username) == 0 && strcmp(users[i].hashedPassword, hashedPassword) == 0) {
return users[i].isAdmin;
}
}
return -1; // Login failed
}
// Function to add a new user (Admin only)
void addUser() {
if (userCount < MAX_USERS) {
printf("Enter username: ");
scanf("%s", users[userCount].username);
char password[100];
printf("Enter password: ");
getPassword(password);
hashPassword(password, users[userCount].hashedPassword);
printf("Is admin (1 for yes, 0 for no): ");
scanf("%d", &users[userCount].isAdmin);
userCount++;
saveUsers();
printf("User added successfully!\n");
system("pause");
} else {
printf("User limit reached!\n");
system("pause");
}
}
// Function to add a new book
void addBook() {
if (bookCount < MAX_BOOKS) {
printf("Enter title: ");
scanf(" %[^\n]%*c", library[bookCount].title);
printf("Enter author: ");
scanf(" %[^\n]%*c", library[bookCount].author);
printf("Enter ISBN: ");
scanf(" %[^\n]%*c", library[bookCount].ISBN);
bookCount++;
saveBooks();
printf("Book added successfully!\n");
system("pause");
} else {
printf("Library is full!\n");
system("pause");
}
}
// Function to delete a book by number
void deleteBook() {
int bookNumber;
printf("Enter the book number to delete: ");
scanf("%d", &bookNumber);
if (bookNumber < 1 || bookNumber > bookCount) {
printf("Invalid book number!\n");
system("pause");
return;
}
for (int i = bookNumber - 1; i < bookCount - 1; i++) {
library[i] = library[i + 1];
}
bookCount--;
saveBooks();
printf("Book deleted successfully!\n");
system("pause");
}
// Function to search for a book by title
void searchBookByTitle() {
char title[100];
printf("Enter title to search: ");
scanf(" %[^\n]%*c", title);
for (int i = 0; i < bookCount; i++) {
if (strcmp(library[i].title, title) == 0) {
printf("Book found:\nTitle: %s\nAuthor: %s\nISBN: %s\n", library[i].title, library[i].author, library[i].ISBN);
system("pause");
return;
}
}
printf("Book not found!\n");
system("pause");
}
// Function to search for a book by author
void searchBookByAuthor() {
char author[100];
int found = 0;
printf("Enter author to search: ");
scanf(" %[^\n]%*c", author);
for (int i = 0; i < bookCount; i++) {
if (strcmp(library[i].author, author) == 0) {
if (!found) {
printf("Books found:\n");
found = 1;
}
printf("Title: %s\nAuthor: %s\nISBN: %s\n", library[i].title, library[i].author, library[i].ISBN);
}
}
if (!found) {
printf("No books found by author %s.\n", author);
}
system("pause");
}
// Function to search for a book by ISBN using binary search
void searchBookByISBN() {
char ISBN[20];
printf("Enter ISBN to search: ");
scanf(" %[^\n]%*c", ISBN);
// Sort books by ISBN first
for (int i = 0; i < bookCount - 1; i++) {
for (int j = i + 1; j < bookCount; j++) {
if (strcmp(library[i].ISBN, library[j].ISBN) > 0) {
Book temp = library[i];
library[i] = library[j];
library[j] = temp;
}
}
}
// Perform binary search
int left = 0, right = bookCount - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
int cmp = strcmp(library[mid].ISBN, ISBN);
if (cmp == 0) {
printf("Book found:\nTitle: %s\nAuthor: %s\nISBN: %s\n", library[mid].title, library[mid].author, library[mid].ISBN);
system("pause");
return;
} else if (cmp < 0) {
left = mid + 1;
} else {
right = mid - 1;
}
}
printf("Book not found!\n");
system("pause");
}
// Function to update book information
void updateBook() {
int bookNumber;
printf("Enter the book number to update: ");
scanf("%d", &bookNumber);
if (bookNumber < 1 || bookNumber > bookCount) {
printf("Invalid book number!\n");
system("pause");
return;
}
printf("Enter new title: ");
scanf(" %[^\n]%*c", library[bookNumber - 1].title);
printf("Enter new author: ");
scanf(" %[^\n]%*c", library[bookNumber - 1].author);
printf("Enter new ISBN: ");
scanf(" %[^\n]%*c", library[bookNumber - 1].ISBN);
saveBooks();
printf("Book updated successfully!\n");
system("pause");
}
// Function to sort books by title
void sortBooksByTitle() {
if (bookCount == 0) {
printf("No books to sort.\n");
system("pause");
return;
}
for (int i = 0; i < bookCount - 1; i++) {
for (int j = i + 1; j < bookCount; j++) {
// Compare titles case-insensitively
if (strcasecmp(library[i].title, library[j].title) > 0) {
Book temp = library[i];
library[i] = library[j];
library[j] = temp;
}
}
}
printf("Books sorted by title.\n");
printf("Displaying sorted books:\n");
for (int i = 0; i < bookCount; i++) {
printf("Book %d:\nTitle: %s\nAuthor: %s\nISBN: %s\n", i + 1, library[i].title, library[i].author, library[i].ISBN);
}
system("pause");
}
// Function to display all books
void displayBooks() {
if (bookCount == 0) {
system("cls");
printf("No books in the library.\n");
system("pause");
} else {
for (int i = 0; i < bookCount; i++) {
printf("Book %d:\nTitle: %s\nAuthor: %s\nISBN: %s\n", i + 1, library[i].title, library[i].author, library[i].ISBN);
}
system("pause");
}
}
// Main function
int main() {
loadUsers();
loadBooks();
char username[50], password[50];
int isAdmin;
while (1) {
system("COLOR 03");
printf(
" __________________ __________________\n"
".-/| \\ / |\\-.\n"
"|||| | ||||\n"
"|||| | ||||\n"
"|||| CSE246 | Library ||||\n"
"|||| Algorithm | Management ||||\n"
"|||| | System ||||\n"
"|||| | ||||\n"
"|||| | ||||\n"
"|||| | ||||\n"
"|||| | ||||\n"
"||||__________________ | __________________||||\n"
"||/===================\\|/===================\\||\n"
"`--------------------~___~-------------------''\n"
);
int loginChoice;
while (1) {
printf("Login:\n1. Enter Username and Password\n2. Exit\nEnter your choice: ");
scanf("%d", &loginChoice);
if (loginChoice == 1 || loginChoice == 2) {
break;
} else {
fflush(stdin);
system("cls");
system("color 04");
printf("Invalid choice! Please try again.\n");
}
}
if (loginChoice == 2) {
return 0;
}
printf("Username: ");
scanf("%s", username);
printf("Password: ");
getPassword(password);
isAdmin = loginUser(username, password);
if (isAdmin == -1) {
fflush(stdin);
system("cls");
system("color 04");
printf("Login failed! Try again.\n");
} else {
break;
}
}
int choice;
while (1) {
system("cls");
printf("\t|===============================================================|\n");
printf("\t| Main Menu |\n");
printf("\t|===============================================================|\n\n");
printf("\n1. Search Book by Title\n2. Search Book by Author\n3. Search Book by ISBN\n4. Sort Books by Title\n5. Display Books\n");
if (isAdmin) {
printf("6. Add Book\n");
printf("7. Add User\n");
printf("8. Delete Book\n");
printf("9. Update Book Information\n");
}
printf("10. Logout\n11. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
searchBookByTitle();
break;
case 2:
searchBookByAuthor();
break;
case 3:
searchBookByISBN();
break;
case 4:
sortBooksByTitle();
break;
case 5:
system("cls");
displayBooks();
break;
case 6:
if (isAdmin) {
addBook();
} else {
printf("Access denied! Only admins can add books.\n");
system("pause");
}
break;
case 7:
if (isAdmin) {
addUser();
} else {
printf("Access denied! Only admins can add users.\n");
system("pause");
}
break;
case 8:
if (isAdmin) {
deleteBook();
} else {
printf("Access denied! Only admins can delete books.\n");
system("pause");
}
break;
case 9:
if (isAdmin) {
updateBook();
} else {
printf("Access denied! Only admins can update book information.\n");
system("pause");
}
break;
case 10:
main(); // Logout and re-login
return 0;
case 11:
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}