-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmostPopularBooks.c
98 lines (69 loc) · 1.99 KB
/
mostPopularBooks.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
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
#include <time.h>
#include <windows.h>
#include "book.h"
#include "member.h"
#include "admin.h"
#include "menu.h"
#include "dataTypes.h"
#include "utilities.h"
void mostPopularBooks() {
SetConsoleTitle("Library System > Admin Menu > Most Popular Books");
system("cls");
FILE * books = fopen("books.bin", "r");
// failure check
if(books==NULL) {
perror("\a\nError");
getche();
return;
}
bookDataType bookBuffer;
bookDataType * book = NULL;
int i=0;
// read from original file
while( fread(&bookBuffer,sizeof(bookDataType),1,books) ) {
book = realloc(book, (i+1)*sizeof(bookDataType) );
book[i] = bookBuffer;
i++;
}
fclose(books);
bookDataType temp;
// bubble sort
int j,k;
for(j=0 ; j<(i-1) ; j++)
for(k=0 ; k<(i-1) ;k++)
if( book[k].borrowed < book[k+1].borrowed ) {
temp = book[k];
book[k] = book[k+1];
book[k+1] = temp;
}
// table header
printf("\n # ");
printf("Title\t\t\t");
printf("Author\t\t\t");
printf("ISBN\t\t");
printf("Borrowed\n\n\n");
int top=5;
int rank=1;
// table printing
for(j=1 ; j<=top , j<=i ; j++) {
// to avoid not taking duplication
// i.e. 5th and 6th are same at value
if( (j>=5) && (book[j-1].borrowed == book[j].borrowed) )
top++;
printf("%2d ", (j==1) ? 1 : (book[j-1].borrowed == book[j].borrowed) ? rank : ++rank );
printf("%.50s\t", book[j-1].title );
printf("%.30s\t\t", book[j-1].author );
printf("%.20s\t", book[j-1].ISBN );
printf("%8d\n", book[j-1].borrowed );
}
free(book);
getche();
fflush(stdin);
adminMenu();
}