Browse Source

TP9

master
parent
commit
1a5e7af515
5 changed files with 16166 additions and 0 deletions
  1. 5
    0
      TP9/.gitignore
  2. 30
    0
      TP9/Makefile
  3. 16020
    0
      TP9/dico.english
  4. 110
    0
      TP9/hash.c
  5. 1
    0
      TP9/listechaines.h

+ 5
- 0
TP9/.gitignore View File

@@ -0,0 +1,5 @@
1
+*.o
2
+*.a
3
+tp7
4
+*.o
5
+hash

+ 30
- 0
TP9/Makefile View File

@@ -0,0 +1,30 @@
1
+#Makefile pour le tp8
2
+#1 seul .c ici
3
+EXEC = hash
4
+
5
+#nom du compilo et options de compil
6
+CC = clang
7
+CFLAGS = -Wall -Werror
8
+
9
+#chemins et nom de la lib
10
+LIBNAME = liblistechaines.a
11
+PATHLIBA = .
12
+PATHLIBH = .
13
+
14
+#flags de compil : le .h à la compil, la lib à l'édition de lien
15
+LDFLAGS = -L$(PATHLIBA) -llistechaines
16
+INCLUDES= -I$(PATHLIBH)
17
+
18
+CFILES = $(EXEC).c
19
+OBJS  = $(patsubst %.c,%.o,$(CFILES)) 
20
+
21
+all : $(EXEC)
22
+
23
+$(EXEC) : $(OBJS) 
24
+	$(CC) -o $@ $(OBJS) $(LDFLAGS)
25
+
26
+%.o: %.c
27
+	$(CC) $(CFLAGS) $(INCLUDES) -c  $<
28
+
29
+clean:
30
+	rm *.o *~ $(EXEC)

+ 16020
- 0
TP9/dico.english
File diff suppressed because it is too large
View File


+ 110
- 0
TP9/hash.c View File

@@ -0,0 +1,110 @@
1
+/*Source Code From Laure Gonnord, 2012*/
2
+/*Adapted from Bernard Carre, 2011*/
3
+
4
+#include <stdio.h>
5
+#include <stdlib.h>
6
+#include <stdbool.h>
7
+
8
+/*List library : reminder, the word have maximal size  MAXSIZE*/
9
+#include "listechaines.h"
10
+
11
+/*Size of the hashtables */
12
+#define TABLE_SIZE 308
13
+
14
+/**Hash functions**/
15
+int asciis(char *word)
16
+{
17
+    int i=0;
18
+    int h=0;
19
+    while(word[i]!='\0')
20
+    {
21
+        h=h+(word[i]-96);
22
+        i++;
23
+    }
24
+    return h;
25
+}
26
+
27
+int hash(char *word)
28
+{
29
+    return (asciis(word) % TABLE_SIZE);
30
+}
31
+
32
+//TODO : write the following functions
33
+
34
+/*Declaration of type Hashtable */
35
+typedef Liste Hashtable[TABLE_SIZE];
36
+
37
+
38
+/*Initialisation of a given Hashtable*/
39
+void init_ht(Hashtable ht) {
40
+    int i;
41
+    for (i = 0; i < TABLE_SIZE; i++) {
42
+        ht[i] = NULL;
43
+    }
44
+}
45
+
46
+/*Update of the hashtable : add the given word in the table!*/
47
+void update_ht(char *word, Hashtable ht) {
48
+    ajout_alphab(&ht[hash(word)], word);
49
+}
50
+
51
+/*Load the file in the internal structure ht */
52
+void load_ht(FILE *fp, Hashtable ht) {
53
+    char mot[MAXSIZE];
54
+    while (fscanf(fp, "%s", mot) != EOF) {
55
+        update_ht(mot, ht);
56
+    }
57
+}
58
+
59
+/*Count the collisions*/
60
+void collisions(Hashtable ht) {
61
+    int i;
62
+    for (i = 0; i < TABLE_SIZE; i++) {
63
+        /* printf("Indice %3d : %3d élément(s).\n", i, taille(ht[i])); */
64
+        printf("%3d %3d\n", i, taille(ht[i]));
65
+    }
66
+}
67
+
68
+/*Maximal hash of the words of the given file*/
69
+/*returns max_word such that hash(max_word)=hmax*/
70
+void max_hash(FILE *fp, char *max_word, int *hmax) {
71
+    char mot[MAXSIZE];
72
+    int h;
73
+    *hmax = 0;
74
+    while (fscanf(fp, "%s", mot) != EOF) {
75
+        h = hash(mot);
76
+        if (h > *hmax) {
77
+            *hmax = h;
78
+            strncpy(max_word, mot, MAXSIZE);
79
+        }
80
+    }
81
+}
82
+
83
+/*Main function*/
84
+int main (int argc, char *argv[]) {
85
+    if (argc < 2) { // text file is missing ?
86
+        fprintf(stderr, "usage: hash <file_name>\n");
87
+    } else {
88
+        FILE *fp;
89
+        fp=fopen(argv[1], "r");
90
+        if (fp==NULL) {
91
+            fprintf(stderr, "no such file, or unreachable: %s\n", argv[1]);
92
+        } else {
93
+            Hashtable myHt;
94
+            init_ht(myHt);
95
+            load_ht(fp, myHt);
96
+
97
+            // Affichage des collisions
98
+            collisions(myHt);
99
+
100
+            // Affichage du hash maximum
101
+            int hmax;
102
+            char max_word[MAXSIZE];
103
+            rewind(fp);
104
+            max_hash(fp, max_word, &hmax);
105
+            printf("Mot maximum : %s de somme %d\n", max_word, hmax);
106
+        }
107
+    }
108
+
109
+    return 0;
110
+}

+ 1
- 0
TP9/listechaines.h View File

@@ -0,0 +1 @@
1
+../TP7/listechaines.h