-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
410 lines (391 loc) · 10.7 KB
/
main.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
#include<stdio.h>
#include<string.h>
#include<time.h>
#include<stdlib.h>
#define MAX_B_ISSUE_F 4
#define MAX_B_ISSUE_S 2
#define NAME_SIZE 50
#define DEPT_SIZE 4
#define MAX_ARRAY_SIZE 50
#define MAX_FAC_DAYS 30
#define MAX_STUD_DAYS 14
enum designation_type {Student, Faculty};
struct date{
int day;
int month;
int year;
};
struct member{
char name[NAME_SIZE];
char dept[DEPT_SIZE]; //three letter short form for departments
enum designation_type designation;
int member_id;
int no_books; //no of books issued
int iss_book_id[MAX_B_ISSUE_F]; //Accession number of books issued
struct date ret_date[MAX_B_ISSUE_F]; //return date of books issued
} students[MAX_ARRAY_SIZE], faculty[MAX_ARRAY_SIZE];
int stud_count = 0, fac_count = 0;
struct book{
char name[NAME_SIZE];
char subject[NAME_SIZE];
char authors[NAME_SIZE];
int acc_no;
int year_pub;
int hall_no;
int rack_no;
int row_no;
int no_issued; //number of copies of books issued
int no_unissued; //number of copies of books available
} books[MAX_ARRAY_SIZE];
int book_count = 0;
struct mem_sort{
char *name;
int no_books;
enum designation_type designation;
} member_sort[2*MAX_ARRAY_SIZE]; // useful when sorting to not disturb original array
void new_member(){
int i;
printf("Enter details:\n");
printf("Enter 0 for faculty, any other number for student: ");
scanf("%d", &i);
if(i == 0){
printf("Enter name: ");
fgets(faculty[fac_count].name, NAME_SIZE, stdin); // reads full name
// we used fgets as scanf stops reading at space
printf("Enter Department's 3 letter name: ");
fgets(faculty[fac_count].dept, DEPT_SIZE, stdin);
faculty[fac_count].designation = Faculty;
printf("Enter member ID: ");
scanf("%d", &faculty[fac_count].member_id);
faculty[fac_count].no_books = 0; //initialisation necessary for this element
fac_count++;
printf ("New Faculty member added in the database!!\n");
}
else{
printf("Enter name: ");
fgets(students[stud_count].name, NAME_SIZE, stdin); // reads full name
// we used fgets as scanf stops reading at space
printf("Enter Department's 3 letter name: ");
fgets(students[stud_count].dept, DEPT_SIZE, stdin);
students[stud_count].designation = Student;
printf("Enter member ID: ");
scanf("%d", &students[stud_count].member_id);
stud_count++;
printf ("New Student member added in the database!!\n");
}
}
void max_issue(){
int max_val = 0, max_index = 0;
int i;
for (i=0;i<fac_count;i++){
if (faculty[i].no_books > max_val){
max_index = i;
max_val = faculty[i].no_books;
}
}
printf("The faculty who borrowed maximum books is ");
fputs(faculty[max_index].name, stdout); //prints string with spaces
printf(" and they issued %d books.\n", max_val);
//printing for faculty done
max_val = 0;
max_index = 0;
for (i=0;i<stud_count;i++){
if (students[i].no_books > max_val){
max_index = i;
max_val = students[i].no_books;
}
}
printf("The student who borrowed maximum books is ");
fputs(students[max_index].name, stdout); //prints string with spaces
printf(" and they issued %d books.\n", max_val);
}
void merge(struct mem_sort arr[], int l, int m, int r){
int n1 = m-l+1;
int n2 = r-m;
struct mem_sort L[n1], R[n2];
// Copy data to temp arrays L[] and R[]
int i, j, k;
for (i=0;i<n1;i++){
L[i] = arr[l+i];
}
for (j=0;j<n2;j++){
R[j] = arr[m+1+j];
}
i= 0;
j = 0;
k = l;
// k is Initial index of merged subarray
while (i<n1 && j<n2){
if(L[i].no_books > R[j].no_books){ // sorts on no of books
arr[k] = L[i];
i++;
}
else if (L[i].no_books == R[j].no_books){
if(L[i].designation > R[j].designation){ //sorts on basis of designation
arr[k] = L[i];
i++;
}
else if(L[i].designation < R[j].designation){
arr[k] = R[j];
j++;
}
else{ //sorts on basis of alphabetical order
if(strcmp(L[i].name, R[j].name)<0){
arr[k] = L[i];
i++;
}
else{
arr[k] = R[j];
j++;
}
}
}
else{
arr[k] = R[j];
j++;
}
k++;
}
// copy remaining L
while (i<n1) {
arr[k] = L[i];
i++;
k++;
}
// Copy remaining R
while (j<n2) {
arr[k] = R[j];
j++;
k++;
}
}
void merge_sort(struct mem_sort arr[],int l,int r){
if(l>=r){
return;//returns recursively
}
int m = (l+r-1)/2;
merge_sort(arr,l,m);
merge_sort(arr,m+1,r);
merge(arr,l,m,r);
}
void borrow_sort(){
int i, j;
for(i=0;i<fac_count;i++){ // initialising member_sort array
member_sort[i].name = &faculty[i].name[0];
member_sort[i].no_books = faculty[i].no_books;
member_sort[i].designation = Faculty;
}
int total = fac_count + stud_count;
for(i=fac_count;i<total;i++){
member_sort[i].name = &students[i-fac_count].name[0];
member_sort[i].no_books = students[i-fac_count].no_books;
member_sort[i].designation = Student;
}
// initialising done
merge_sort(member_sort, 0, total - 1);
printf("The sorted list of borrowers is:\n");
for(i=0;i<total;i++){
printf("%d. %d books borrowed by ", i+1, member_sort[i].no_books);
if (member_sort[i].designation==Faculty){
printf("Faculty member ");
}
else{
printf("Student member ");
}
fputs(member_sort[i].name, stdout);
printf("\n");
}
}
void book_shift(int start, int size){
int i; // this function moves books forward to make room for a new one
for(i=size-1; i>=start; i--){
books[i+1] = books[i];
}
}
void insert_book(){
// book details are first stored in a temporary structure
// then inserted in the correct place in the sorted database
struct book temp;
printf("Enter book details:");
printf("Enter book name: ");
fgets(temp.name, NAME_SIZE, stdin);
printf("Enter subject name: ");
fgets(temp.subject, NAME_SIZE, stdin);
printf("Enter author(s) names: ");
fgets(temp.authors, NAME_SIZE, stdin);
temp.acc_no = book_count+1;
printf("Accession number assigned is: %d\n", book_count+1);
printf("Enter Year Of Publication: ");
scanf("%d", &temp.year_pub);
printf("Enter Hall No, Rack No, Row No: ");
scanf("%d %d %d", &temp.hall_no, &temp.rack_no, &temp.row_no);
temp.no_issued = 0;
printf("Enter number of copies acquired: ");
scanf("%d", &temp.no_unissued);
int i=0, flag = 0;
for(i=0; i<book_count&&flag==0; i++){
if(strcmp(temp.subject, books[i].subject)<0){
book_shift(i, book_count); //shifts books to make room for new
books[i] = temp;
flag = 1;
}
else if(strcmp(temp.subject, books[i].subject)==0){
if(strcmp(temp.name, books[i].name)<0){
book_shift(i, book_count); //shifts books to make room for new
books[i] = temp;
flag = 1;
}
else if(strcmp(temp.name, books[i].name)==0){
if(strcmp(temp.authors, books[i].authors)<0){
book_shift(i, book_count); //shifts books to make room for new
books[i] = temp;
flag = 1;
}
else if(strcmp(temp.authors, books[i].authors)==0){
if(temp.year_pub<books[i].year_pub){
book_shift(i, book_count); //shifts books to make room for new
books[i] = temp;
flag = 1;
}
}
}
}
}
if(flag==0){ // if book is to be inserted at last
books[book_count]= temp;
}
book_count++;
printf("Book Inserted in the database!!\n\n");
}
int date_diff(struct date a, struct date b){
int month_days[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
int a_day, b_day; //stores days elapsed since 31 Dec 2000
a_day = 365*(a.year-2000);
a_day += (a.year-2001)/4; // for leap year, it ignores current year for now
if(a.month>2&&(a.year%4==0)){ //if current year is leap
a_day++;
}
a_day += month_days[a.month - 1];
a_day += a.day;
// we calculate the same for b
b_day = 365*(b.year-2000);
b_day += (b.year-2001)/4; // for leap year, it ignores current year for now
if(b.month>2&&(b.year%4==0)){ //if current year is leap
b_day++;
}
b_day += month_days[b.month - 1];
b_day += b.day;
return a_day - b_day;
}
struct date get_cur_date(){
struct date ans;
time_t now;
time(&now);
struct tm *local = localtime(&now);
ans.day = local->tm_mday; // get day of month (1 to 31)
ans.month = local->tm_mon + 1; // get month of year (0 to 11)
ans.year = local->tm_year + 1900; // get year since 1900
return ans;
}
float calc_fine(int mem_id, int print_flag){
if (mem_id == 0){ // if mem_id is passed zero, it asks the user for id
printf("Enter Member ID whose fine you want to calculate: ");
scanf("%d", &mem_id);
}
int i, j, flag = 0;
float fine = 0;
struct date today = get_cur_date(); //gets today's date
for(i=0;i<fac_count&&flag==0;i++){
if(mem_id==faculty[i].member_id){
for(j=0;j<faculty[i].no_books;j++){
if(date_diff(faculty[i].ret_date[i],today)>0){
fine += 0.5*date_diff(faculty[i].ret_date[i],today);
}
}
flag = 1;
}
}
for(i=0;i<stud_count&&flag==0;i++){
if(mem_id==students[i].member_id){
for(j=0;j<students[i].no_books;j++){
if(date_diff(students[i].ret_date[i],today)>0){
fine += 0.5*date_diff(students[i].ret_date[i],today);
}
}
flag = 1;
}
}
if (flag==1&&print_flag==1){
printf("Member ID %d has fine of %f rupees.\n", mem_id,fine);
}
else if(print_flag==1){
printf("Member ID %d not found!!\n\n", mem_id);
}
return fine;
}
void max_fine(){
float max_fine = 0.0;
int i = 0, mem_no = 0;
float cur_fine;
for(i=0;i<fac_count;i++){
cur_fine = calc_fine(faculty[i].member_id, 0);
if(cur_fine>max_fine){
max_fine = cur_fine;
}
}
for(i=0;i<stud_count;i++){
cur_fine = calc_fine(students[i].member_id, 0);
if(cur_fine>max_fine){
max_fine = cur_fine;
}
}
if (mem_no!=0){ //if checks if anyone has been fined at all
printf("Max fine was received by Member No %d of %f rupees\n\n", mem_no, max_fine);
}
else{
printf("No one has any fines!!\n\n");
}
}
void print_menu(){
printf("=========================================\n\n");
printf(" Welcome to VNIT Library Program\n\n");
printf("Select the operation you want to perform:\n");
printf("1. Add New Student/Faculty Member\n");
printf("2. See who has issued maximum books\n");
printf("3. List of all borrowers in descending order\n");
printf("4. Insert a new book in the database\n");
printf("5. Calculate fine of a Library Member\n");
printf("6. Get info of member with Maximum fine for late return\n");
printf("0. Exit program\n");
printf("Enter your choice: ");
}
int main(){
int flag = 1, temp;
while (flag!=0){
print_menu();
scanf("%d", &flag);
printf("\n");
switch(flag){
case 1:
new_member();
break;
case 2:
max_issue();
break;
case 3:
borrow_sort();
break;
case 4:
insert_book();
break;
case 5:
calc_fine(0, 1);
break;
case 6:
max_fine();
break;
}
}
printf("\n\nExiting program!!!");
return 0;
}