This repository was archived by the owner on Dec 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataBaseMain.c
More file actions
142 lines (130 loc) · 4.63 KB
/
DataBaseMain.c
File metadata and controls
142 lines (130 loc) · 4.63 KB
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
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Books.h"
#include "Attributes.h"
/*
* @author - Patrick Quinlan
* SID - 575275
* Last Edit - 4/3/24
*
* Main file where the database application for handling the book database
* of a library. Books can be grouped by:
* topic/genre, name, publication year, author, type, availability
*
* Likely best implamented as a linked list of linked lists or an array of linked lists so when searching
* for a specific book its easy to enter a narrowing field in an array and then to traverse the list to find
* book
*
* This would also be benificial for adding new books as adding to a linked list is much easier than adding
* to an array and narrowing topics ect are much more rarely added compared to books. Other Positives is
* searching an array has O(1) as we can go straight to the location as long as we know where it is.
*/
int main(int argc, char* argv)
{
FILE* data;
data = fopen("Books.txt","r");
//book_list_tests(); // hard coded test for the book lists methods
//attribute_table_test(); //hard coded test for hash table
String buffer[99];
String* book_data = malloc(4 * sizeof(buffer));
String token = malloc((4 * strlen(buffer) + 5) * sizeof(char));
int year;
String data_string = malloc((4 * strlen(buffer) + 4) * sizeof(char));
int i = 0;
AttributeTable library_database = create_hashtable(552);
printf("\nFill Database");
fgets(data_string, strlen(buffer) * 4 + 4, data);
clock_t start = clock();
//add books
while (!feof(data))
{
i = 0;
token = strtok(data_string, ":");
while (token != NULL && i < 4) {
book_data[i] = malloc((strlen(buffer) + 1) * sizeof(char));
strcpy(book_data[i], token);
token = strtok(NULL, ":");
i++;
}
BookNodePtr book = create_book(book_data[0], book_data[1], book_data[2], atoi(book_data[3]));
insert_book_to_table(&library_database, book);
free(book);
fgets(data_string, strlen(buffer) * 4 + 4, data);
}
fclose(data);
clock_t diff = clock() - start;
long msec = diff * 1000 / CLOCKS_PER_SEC;
printf("Operation took %d milliseconds\n\n", msec);
//print_table(&library_database); //Test to check how data from text file was being inserted
// Clear the input buffer
printf("\n\nLIBRARY BOOK DATABASE\n\n");
int choice;
int search_by = 0;
String search = malloc(sizeof(buffer));
int quit = 0;
while (quit == 0)
{
String title = malloc((strlen(buffer) + 1) * sizeof(char));
String author = malloc((strlen(buffer) + 1) * sizeof(char));
String genre = malloc((strlen(buffer) + 1) * sizeof(char));
printf("\nMenu\n");
printf("0. Quit\n1. Search for book\n2. Add Book\n3. Delete Book\n");
scanf("%d", &choice);
switch (choice)
{
case(1):
printf("\nSearch by:\n0. Year(1800-2200)\n1. Title\n2. Author\n3. Genre\n");
scanf("%d", &search_by);
printf("SEARCH: ");
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
fgets(search, strlen(buffer),stdin);
search[strcspn(search, "\n")] = '\0';
find_book_in_table(&library_database,search,search_by);
break;
case(2):
printf("\nEnter Title: ");
while (getchar() != '\n'); // Stops the skipping of the fgets line by removing the termination character is it isnt read in
fgets(title, strlen(buffer), stdin);
printf("\nEnter Author: ");
fgets(author, strlen(buffer), stdin);
printf("\nEnter Genre: ");
fgets(genre, strlen(buffer), stdin);
printf("\nEnter Year(1800-2200): ");
scanf("%d", &year);
author[strcspn(author, "\n")] = '\0';
title[strcspn(title, "\n")] = '\0';
genre[strcspn(genre, "\n")] = '\0';
BookNodePtr inserted_book = create_book(title, author, genre, year);
insert_book_to_table(&library_database, inserted_book);
free(inserted_book);
printf("\nYour book; %s by %s %s %d, was added to the database\n\n", title, author, genre, year);
break;
case(3):
printf("\nEnter Title of book to remove: ");
while (getchar() != '\n');
fgets(title, strlen(buffer), stdin);
printf("\nEnter Author of book to remove: ");
fgets(author, strlen(buffer), stdin);
printf("\nEnter Genre of book to remove: ");
fgets(genre, strlen(buffer), stdin);
printf("\nEnter Year of book to remove: ");
scanf("%d", &year);
author[strcspn(author, "\n")] = '\0';
title[strcspn(title, "\n")] = '\0';
genre[strcspn(genre, "\n")] = '\0';
BookNodePtr deleted_book = create_book(title, author, genre, year);
remove_book_from_table(&library_database, deleted_book);
printf("\nThe book; %s by %s, was removed from the database\n\n", title, author);
free(deleted_book);
break;
default:
printf("Quitting LIBRARY BOOK DATABASE");
quit = 1;
break;
}
}
free(&library_database);
}