-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcatalogue.cpp
More file actions
executable file
·194 lines (151 loc) · 4.4 KB
/
Copy pathcatalogue.cpp
File metadata and controls
executable file
·194 lines (151 loc) · 4.4 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
#include <iostream>
#include "catalogue.h"
using namespace std;
Catalogue::Catalogue(void)
{
head = NULL;
size = 0;
total_people = 0;
// cout << "A new Catalogue has been created!\n";
}
Catalogue::~Catalogue(void)
{
// cout << "A Catalogue is being destructed!\n";
delete head;
head = NULL;
tail = NULL;
size = 0;
}
/* function that inserts the id of a voter that has voted to the Catalogue. We need the postal code of
the voter in order to place him in the correct list. */
void Catalogue::insert(char* id, const int postal_code)
{
if (total_people == 0) {
head = new catalogue_node(postal_code, NULL, NULL);
head->list->insert(id);
tail = head;
} else {
catalogue_nodePtr temp = head;
while (temp != NULL) {
if (temp->list->postal_code != postal_code) {
temp = temp->next;
} else {
break;
}
}
if (temp != NULL) {
temp->list->insert(id);
} else {
tail->next = new catalogue_node(postal_code, tail, NULL);
tail->next->list->insert(id);
tail = tail->next;
}
}
size++;
}
/* function that removes a voter from the list with a specific postcode. Returns true if the delete was successful. Else returns false. */
bool Catalogue::remove(char* id, const int postcode)
{
if (size == 0) {
cout << "\n\nError in Catalogue::remove(). Catalogue is already empty.\n\n";
return false;
}
catalogue_nodePtr temp = head;
while (temp != NULL) {
if (temp->list->postal_code != postcode) {
temp = temp->next;
} else {
break;
}
}
if (temp == NULL) {
cout << "\n\nError in Catalogue::remove(). Postal code List was not found.\n\n";
return false;
}
if (temp->list->remove(id) != true) {
cout << "\n\nError in Catalogue::remove(). Voter with id " << id << " was not found.\n\n";
return false;
}
size--;
total_people--;
return true;
}
long Catalogue::get_size(void)
{
return size;
}
long Catalogue::get_voters_in_postcode(int postal_code)
{
catalogue_nodePtr temp = head;
while (temp != NULL) {
if (temp->list->postal_code == postal_code) {
return temp->list->size;
}
temp = temp->next;
}
return 0;
}
void Catalogue::print_percentages(void)
{
catalogue_nodePtr temp = head;
while (temp != NULL) {
if (temp->list->total_people_in_postal_code != 0) {
double percentage = temp->list->size / ((double) temp->list->total_people_in_postal_code);
cout << "# IN " << temp->list->postal_code << " PERCENTAGE-OF-VOTERS-IS " << percentage << endl;
}
temp = temp->next;
}
}
/* function that increases by 1 the counter of the people in a specific postcode. If the postcode is new,
and has no people in it, then a new list is created which corresponds to the new postcode. */
void Catalogue::add_person_to_postal_code(const int postcode)
{
if (total_people == 0) {
head = new catalogue_node(postcode, NULL, NULL);
head->list->add_person_to_postal_code();
tail = head;
} else {
catalogue_nodePtr temp = head;
while (temp != NULL) {
if (temp->list->postal_code != postcode) {
temp = temp->next;
} else {
break;
}
}
if (temp != NULL) {
temp->list->add_person_to_postal_code();
} else {
tail->next = new catalogue_node(postcode, tail, NULL);
tail->next->list->add_person_to_postal_code();
tail = tail->next;
}
}
total_people++;
}
/* decreases the counter of people in a specific postcode. */
bool Catalogue::remove_person_from_postal_code(const int postcode)
{
if (total_people == 0) {
cout << "\n\nError in Catalogue::remove_person_from_postal_code(). Total people is zero.\n\n";
return false;
}
catalogue_nodePtr temp = head;
while (temp != NULL) {
if (temp->list->postal_code != postcode) {
temp = temp->next;
} else {
break;
}
}
if (temp == NULL) {
cout << "\n\nError in Catalogue::remove_person_from_postal_code(). Postal code List was not found.\n\n";
return false;
}
if (temp->list->remove_person_from_postal_code() == false) {
cout << "Error in Catalogue::remove_person_from_postal_code(). Total people in List is zero.\n";
return false;
}
total_people--;
return true;
}