280074a8
Corentin Danjou
ok
|
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
|
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
/*Size of the hashtable*/
#define TABLE_SIZE 250000
#define MAXSIZE 30
//Declaration de liste chainee de chaines de caracteres
typedef struct cell {
char val[MAXSIZE];
struct cell * suiv;
} Cellule, *Ptcellule, *Liste;
typedef struct salle{
char* region;
int affiliate_id;
char* affiliate_name;
}Salle;
typedef struct athlete{
int id_athlete;
char* lastname;
char* firstname;
char gender;
int age;
char* weight;
char* height;
int overallscore;
int overallrank;
int score;
char* scoredisplay;
int rank;
Salle* salle;
}Athlete;
typedef Athlete* AllAthletes;
//déclaration de la table hashage
typedef Liste Hashtable[TABLE_SIZE];
//initialisation de la table de hachage
void init_ht(Hashtable ht)
{
for(int i=0;i<TABLE_SIZE;i++)
{
ht[i]=NULL;
}
}
//Hash fonction
int asciis(char *word)
{
int i=0;
int h=0;
while(word[i]!='\0')
{
h=h+(word[i]-96);
i++;
}
return h;
}
int hash(char *word)
{
return (asciis(word) % TABLE_SIZE);
}
/*Update of the hashtable : add the given word in the table!*/
void update_ht(char *word, Hashtable ht)
{
ajout_alphab(&ht[hash(word)],word);
}
/*Load the file in the internal structure ht */
void load_ht(FILE *fp, Hashtable ht)
{
char word[MAXSIZE];
fscanf(fp,"%s",word);
while(!feof(fp))
{
ajout_alphab(&ht[hash(word)],word);
fscanf(fp,"%s",word);
}
}
void ajout_alphab(Liste *pl, char mot[MAXSIZE])
{
if ( (*pl==NULL) || (strcmp(mot, (*pl)->val) < 0) ) { // empty list or mo\
t < head => new head
ajout_tete(pl,mot);
}
else
{
if (strcmp(mot, (*pl)->val) > 0) // mot > head => add in next's
{
ajout_alphab(&(*pl)->suiv,mot);
} // else mot already in list
}
}
int main (int argc, char *argv[])
{
if (argc < 2) { // text file is missing ?
fprintf(stderr, "usage: hash <file_name>\n");
}
else {
FILE *fp;
fp=fopen(argv[1], "r");
if (fp==NULL) {
fprintf(stderr, "no such file, or unreachable: %s\n", argv[1]);
}
else {
Hashtable mylist;
init_ht(mylist);
load_ht(fp,mylist);
}
return 0;
}
|