-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.cpp
More file actions
489 lines (320 loc) · 7.83 KB
/
Table.cpp
File metadata and controls
489 lines (320 loc) · 7.83 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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//Felix Jackson
//CS 260
//5-15-2020
//Program 3
//"Table.cpp"
/*
This program allows the client to make a table of websites. It uses
a website struct that can contain the topic, url, summary, review, and rating of
a website. There are functions to add a website to the table, retrieve an array
of websites of a chosen topic, remove all websites with one-star reviews, display
all websites with a chosen topic, and display all websites in the table.
*/
#include "Table.h"
#include <iostream>
#include <cstring>
using namespace std;
//website constructor
website::website()
{
topic_name = NULL;
url = NULL;
summary = NULL;
review = NULL;
rating = 0;
}
//website destructor
website::~website ()
{
if (topic_name)
{
delete topic_name;
}
if (url)
{
delete url;
}
if (summary)
{
delete summary;
}
if (review)
{
delete review;
}
}
//Table constructor
//INPUT: int argument
Table::Table (int array_size)
{
size = array_size;
hash_table = new node * [size];
node * * current = hash_table;
for (int k = 0; k < size; ++k)
{
*current = NULL;
++current;
}
}
//Table destructor
Table::~Table ()
{
if (hash_table)
{
remove_all (hash_table [0], 0);
delete [] hash_table;
hash_table = NULL;
}
}
//removes all the nodes in the hash table. pass hash_table[0] as current and 0 as
//index
//INPUT: arguments
void Table::remove_all (node * & current, int index)
{
if (!current)
{
++index;
if (index == size)
{
return;
}
remove_all (hash_table [index], index);
}
if (current)
{
node * temp = current -> next;
delete current;
current = temp;
}
remove_all (current, index);
}
//calculates the index where the node should go using the keyword
//INPUT: argument
//OUTPUT: int return
int Table::hash_function (char keyword [])
{
int length = strlen (keyword);
int sum = 0;
for (int k = 0; k < length; ++k)
{
sum += keyword[k];
}
sum %= size;
return sum;
}
//Adds a new website to the table with values that match the
//arguments passed to this function
//INPUT: arguments and the returned int from hash_function
int Table::add_website (char topic_name [], char url [], char summary [],
char review [], int rating)
{
if (!hash_table)
{
return 0;
}
int index = hash_function (topic_name);
node * temp = hash_table[index];
website * temp2 = NULL;
hash_table [index] = new node;
hash_table [index] -> next = temp;
temp2 = &hash_table[index]->site;
temp2->topic_name = new char [strlen(topic_name) + 1];
strcpy (temp2->topic_name, topic_name);
temp2->url = new char [strlen (url) + 1];
strcpy (temp2->url, url);
temp2->summary = new char [strlen (summary) + 1];
strcpy (temp2->summary, summary);
temp2->review = new char [strlen (review) + 1];
strcpy (temp2->review, review);
if (rating >= 1 && rating <=5)
{
temp2->rating = rating;
}
if (rating < 1)
{
temp2->rating = 1;
}
if (rating > 5)
{
temp2->rating = 5;
}
return 1;
}
//puts the info of every website with a topic that matches the
//topic_name argument into an array and returns the number of
//matches found, which is also the length of the array
//INPUT: arguments and a returns in from the hash_function
//OUTPUT: argument and an int return
int Table::retrieve (char topic_name [], website * & matches)
{
if (!hash_table)
{
return 0;
}
int index = hash_function (topic_name);
int num = count_matches (hash_table [index], topic_name);
website * matches_current_index = NULL;
if (matches)
{
delete matches;
}
matches = new website [num];
matches_current_index = matches;
retrieve (hash_table [index], topic_name, matches_current_index);
return num;
}
//counts and returns the number of matches found in a chain
//INPUT: arguments
//OUTPUT: int return
int Table::count_matches (node * current, char topic_name [])
{
if (!current)
{
return 0;
}
int sum = 0;
if (strcmp (current->site.topic_name, topic_name) == 0)
{
return sum += 1 + count_matches (current->next, topic_name);
}
return sum += count_matches (current->next, topic_name);
}
//searches a chain for matches and copies each website match into
//the "matches" array
//INPUT: arguments
//OUTPUT: website array argument
void Table::retrieve (node * current, char topic_name [], website * & matches)
{
if (!current)
{
return;
}
if (strcmp (current->site.topic_name, topic_name) == 0)
{
website * temp = ¤t->site;
matches->topic_name = new char [strlen (temp->topic_name)
+ 1];
strcpy (matches->topic_name, temp->topic_name);
matches->url = new char [strlen (temp->url) + 1];
strcpy (matches->url, temp->url);
matches->summary = new char [strlen (temp->summary) + 1];
strcpy (matches->summary, temp->summary);
matches->review = new char [strlen (temp->review) + 1];
strcpy (matches -> review, temp->review);
matches->rating = temp->rating;
retrieve (current->next, topic_name, ++matches);
}
retrieve(current->next, topic_name, matches);
}
//Removes all websites in the table with one-star ratings and
//returns the number of websites removed
//OUTPUT: returns an int
int Table::remove_one_star ()
{
if (!hash_table)
{
return 0;
}
return remove_one_star (hash_table [0], 0);
}
//recursively removes every node in the hash table that contains
//a website with a one star rating
//INPUT: arguments
//OUTPUT: int return
int Table::remove_one_star (node * & current, int index)
{
int count = 0;
if (!current)
{
++index;
if (index == size)
{
return 0;
}
return count += remove_one_star (hash_table[index], index);
}
if (current->site.rating == 1)
{
node * temp = current -> next;
delete current;
current = temp;
return count += 1 + remove_one_star (current, index);
}
return count += remove_one_star (current->next, index);
}
//Displays all websites in the table with a topic that matches
//topic_name and returns the number of matches found
//INPUT: arguments
//OUTPUT: cout, int return
int Table::display_by_topic (char topic_name [])
{
if (!hash_table)
{
return 0;
}
int index = hash_function (topic_name);
int num = 0;
cout << "\n\nWebsites with " << topic_name << " as a topic:\n\n";
num = display_by_topic (hash_table [index], topic_name);
if (!num)
{
cout << "None found.\n\n";
}
return num;
}
//recursively searches a chain for matches and displays each of
//them. returns the number of websites displayed
//INPUT: arguments
//OUTPUT: cout, int return
int Table::display_by_topic (node * current, char topic_name [])
{
if (!current)
{
return 0;
}
int count = 0;
if (strcmp (current->site.topic_name, topic_name) == 0)
{
website * temp = ¤t->site;
cout << "\n\nWebsite URL - " << temp -> url
<< "\nWebsite summary - " << temp -> summary
<< "\nWebsite review - " << temp -> review
<< "\nWebsite rating - " << temp -> rating << " stars\n\n";
return count += 1 + display_by_topic (current->next, topic_name);
}
return count += display_by_topic (current->next, topic_name);
}
//displays all websites in the table and returns how many there are
//OUTPUT: cout, int return
int Table::display_all ()
{
if (!hash_table)
{
return 0;
}
return display_all (hash_table [0], 0);
}
//recursively displays all the websites in the hash table and
//returns the number of websites displayed
//INPUT: arguments
//OUTPUT: cout, int return
int Table::display_all (node * current, int index)
{
int count = 0;
if (!current)
{
++index;
if (index == size)
{
return 0;
}
return count += display_all (hash_table [index], index);
}
website * temp = ¤t->site;
cout << "\n\nWebsite Topic - " << temp->topic_name
<< "\nURL - " << temp -> url
<< "\nSummary - " << temp -> summary
<< "\nReview - " << temp -> review
<< "\nRating - " << temp->rating << " stars out of 5\n\n";
return count += 1 + display_all (current->next, index);
}