-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtable.c
320 lines (299 loc) · 11.1 KB
/
hashtable.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
#include "hashtable.h"
#define TABLE_SIZE 45523 //prime number larger than 1.3 times the number of keys in CSV file
#define MAX_NAME 256 //Longueur maximale d'un Nom
//Convert string to int
//Return 0 if error, the converted number otherwise
int ConvertToInt(char *s){
char * pEnd;
long sl = strtol(s, &pEnd, 10);
errno = 0;
if (pEnd == s) {
//fprintf(stderr, "%s: not a decimal number\n", s);
return 0;
} else if ('\0' != *pEnd) {
//fprintf(stderr, "%s: extra characters at end of input: %s\n", s, pEnd);
return 0;
} else {
return (int)sl;
}
}
//Returns the index (int) for the hashtable of a specific person based on its name (name is the key)
unsigned int hash(char *name){
const int prime_number = 31;
int length = strlen(name);
unsigned int hash = 0;
for(int i=0; i < length; i++){
hash = ((prime_number^i) * hash + name[i])% TABLE_SIZE; //modulo TABLE_SIZE to have 0 <= hash <= Table of Person, otherwise the index will be out of range
}
return hash;
}
void init_hash_table(Liste *hashtable){
for(int i=0; i < TABLE_SIZE; i++){
hashtable[i] = NULL;
}
}
void free_table(Liste *hash_table){
Liste Lcurrent = NULL, Lnext = NULL;
for(int i=0; i < TABLE_SIZE; i++){
Lcurrent = hash_table[i];
while(!EstVide(Lcurrent)){
Lnext = Lcurrent->next;
free(Lcurrent);
Lcurrent = Lnext;
}
}
free(hash_table);
}
void print_table(Liste *hash_table){
Liste L;
for(int i=0; i < TABLE_SIZE; i++){
L = hash_table[i];
if(L == NULL){
printf("%i\t-------\n",i);
}else{
while(!EstVide(L)){
printf("%i) %i %i %s %i %i %i %i --> ",i, L->sex[0], L->sex[1], L->name, L->birthdate[0],L->birthdate[1],L->birthdate[2],L->birthdate[3]);
L = GetSucc(L);
}
printf("\n");
}
}
}
Liste hash_search(Liste *hash_table, char *s){
int i = hash(s);
Liste tmph = hash_table[i];
while(!EstVide(tmph) && strcmp(tmph->name,s)!=0){
tmph = GetSucc(tmph);
}
return tmph;
}
bool hash_table_insert(Liste *hash_table, Liste L){
if (EstVide(L)) return false;
int i = hash(L->name);
Liste tmpl = NULL;
if (hash_table[i] != NULL) //collision
{
tmpl = hash_search(hash_table,L->name); //Search if Person already exist
if (tmpl != NULL){ //Found
if (L->sex[0] > 0)
{
tmpl->sex[0] = tmpl->sex[0] + L->sex[0];
if (L->birthdate[0] < tmpl->birthdate[0] && L->birthdate[0] != 0) //datebirth check if less than min_date_of_birth for women
{
tmpl->birthdate[0] = L->birthdate[0];
}
if (L->birthdate[1] > tmpl->birthdate[1]) //datebirth check if greater than max_date_of_birth for women
{
tmpl->birthdate[1] = L->birthdate[1];
}
}else if (L->sex[1] > 0)
{
tmpl->sex[1] = tmpl->sex[0] + L->sex[1];
if (L->birthdate[2] < tmpl->birthdate[2] && L->birthdate[2] != 0) //datebirth check if less than min_date_of_birth for men
{
tmpl->birthdate[2] = L->birthdate[2];
}
if (L->birthdate[3] > tmpl->birthdate[3]) //datebirth check if greater than max_date_of_birth for men
{
tmpl->birthdate[3] = L->birthdate[3];
}
}
free(L); // data saved so we don't need this allocated space
return false;
}
}
L->next = hash_table[i];
hash_table[i] = L; //put in front List if not exist
return true;
}
int *countNumberOfPerson(Liste *hash_table){
int *numberOfPerson = calloc(2,sizeof(int));
Liste L;
for(int i=0;i<TABLE_SIZE;i++){
L = hash_table[i];
while(!EstVide(L)){
if (L->sex[0] != 0) //women name
{
numberOfPerson[0]++;
}
if (L->sex[1] != 0) //men name
{
numberOfPerson[1]++;
}
L = GetSucc(L);
}
}
return numberOfPerson;
}
int *readCSV(char *filename,Liste *hash_table,int *nombre){
FILE *fp;
fp = fopen(filename,"r");
if(fp == NULL) {
perror("Error in opening file - Empty file");
exit(1);
}
// Process Data here
nombre[0]=0;
nombre[1]=0;
char *b = NULL;
size_t bufsize = 50;
b = malloc(bufsize * sizeof(char));
char *token;
char delim = ';';
//char *line; //pour stocker une ligne de strsep
int s=0;
char *prenom = NULL;
int annee=0;
int nb=0;
int *numberofNames = calloc(2,sizeof(int));
getline(&b,&bufsize,fp); //1st line: column names - "sexe;preusuel;annais;dpt;nombre"
while (!feof(fp))
{
getline(&b,&bufsize,fp);
if(b[0] != '\0')
{
for (int i = 0; i < 5; i++) { // 5 columns
token = strsep(&b, &delim);
switch (i){
/* for every element we store it in appropriate type */
case 0: //Convert sexe and year to int
s = ConvertToInt(token); // gère le cas ou token est invalide return 0 if error
break;
case 1: //store name as string
prenom = token;
while (*token) {
*token = toupper((unsigned char) *token);
token++;
}
break;
case 2:
annee = ConvertToInt(token);
break;
case 4:
nb = strcspn(token, "\r\n"); //return the index of the first occurence of "\r\n"
token[nb] = '\0'; // "\r\n" =>"\0" (or "\0\n")
nb = ConvertToInt(token);
break;
default:
break;
}
}
Liste ret;
if(s == 1){
nombre[1] = nombre[1] + nb;
ret = create_person(prenom,s,annee,nb);
}else{
nombre[0] = nombre[0] + nb;
ret = create_person(prenom,s,annee,nb);
}
if(hash_table_insert(hash_table,ret)){
if(s == 1) numberofNames[1]++;
if(s == 2) numberofNames[0]++;
}
}
}
fclose(fp);
return numberofNames;
}
void menu(Liste *hashtable, int *nb,int *numberOfNames){
Liste L;
char prenom[MAX_NAME];
int *distinctNumberOfNames;
distinctNumberOfNames = countNumberOfPerson(hashtable);
int choix = -1;
printf("0: Ce menu\n");
printf("1: Le nombre de naissances\n");
printf("2: Le nombre de prénoms\n");
printf("3: Statistiques sur un prénom\n");
printf("4: Quitter\n");
while (choix != 4)
{
printf("Que voulez-vous afficher ? (0 pour le menu) > ");
scanf("%d", &choix);
switch (choix){
case 0:
printf("0: Ce menu\n");
printf("1: Le nombre de naissances\n");
printf("2: Le nombre de prénoms\n");
printf("3: Statistiques sur un prénom\n");
printf("4: Quitter\n");
break;
case 1:
printf("Le fichier recouvre %d naissances.\n",nb[0]+nb[1]);
break;
case 2: ;
//Le nombre de prénoms.
char choix1;
printf("Souhaitez-vous distinger le genre ? (O/N) ");
scanf(" %c", &choix1);
if(choix1 == 'O' || choix1 == 'o'){
printf("Le fichier recouvre %d prénoms masculins et %d prénoms féminins.\n",distinctNumberOfNames[1],distinctNumberOfNames[0]);
}else if(choix1 == 'N' || choix1 == 'n'){
printf("Le fichier recouvre %d prénoms\n",distinctNumberOfNames[1]+distinctNumberOfNames[0]);
}
break;
case 3:
//
printf("Quel prénom ? ");
scanf("%s", prenom);
char *temp=prenom;
while (*temp) {
*temp = toupper((unsigned char) *temp);
temp++;
}
L = hash_search(hashtable,prenom);
if(L == NULL){
printf("Ce prénom n'existe pas\n");
break;
}
if(L->sex[0] != 0 && L->sex[1] != 0){
printf("Souhaitez-vous distinger le genre ? (O/N) ");
scanf(" %c", &choix1);
if(choix1 == 'O' || choix1 == 'o'){
printf("Le prénom %s a été donné à %d garçons et %d filles.\n",prenom,L->sex[1],L->sex[0]);
}else if(choix1 == 'N' || choix1 == 'n'){
printf("Le prénom %s a été donné à %d enfants.\n",prenom,L->sex[1]+L->sex[0]);
}
}else{
printf("Le prénom %s a été donné à %d enfants.\n",prenom,L->sex[1]+L->sex[0]);
}
if (L->birthdate[0] != 0 && L->birthdate[2] != 0)
{
if (L->birthdate[0] < L->birthdate[2])
{
printf("Année de première apparition %d.\n",L->birthdate[0]);
}
printf("Année de première apparition %d.\n",L->birthdate[2]);
}else if (L->birthdate[0] != 0)
{
printf("Année de première apparition %d.\n",L->birthdate[0]);
}else if (L->birthdate[2] != 0)
{
printf("Année de première apparition %d.\n",L->birthdate[2]);
}else{
printf("Année de première apparition inconnu.\n");
}
if (L->birthdate[1] != 0 && L->birthdate[3] != 0)
{
if (L->birthdate[1] > L->birthdate[3])
{
printf("Année de dernière apparition %d.\n",L->birthdate[1]);
}else{
printf("Année de dernière apparition %d.\n",L->birthdate[3]);
}
}else if (L->birthdate[1] != 0)
{
printf("Année de dernière apparition %d.\n",L->birthdate[1]);
}else if (L->birthdate[3] != 0)
{
printf("Année de dernière apparition %d.\n",L->birthdate[3]);
}else{
printf("Année de dernière apparition inconnu.\n");
}
break;
default:
break;
}
}
printf("\nBye\n\n");
}