Commit 8af1f2429503e7afb5adcccb10b4548b1dc0e101
1 parent
2934aea8
V2
Showing
2 changed files
with
155 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,155 @@ | @@ -0,0 +1,155 @@ | ||
1 | +#include <stdio.h> | ||
2 | +#include <string.h> | ||
3 | +#include <stdlib.h> | ||
4 | +#include <stdbool.h> | ||
5 | + | ||
6 | +struct cell { | ||
7 | + int stop; | ||
8 | + struct cell * liste[26]; | ||
9 | +}; | ||
10 | + | ||
11 | +int num(char c){ | ||
12 | + int x; | ||
13 | + x=c; | ||
14 | + if (x>96 && x<123){ | ||
15 | + x=x-97; | ||
16 | + return x; | ||
17 | + } | ||
18 | + if (x>64 && x<91) { | ||
19 | + x=x-65; | ||
20 | + return x; | ||
21 | + } | ||
22 | + printf("erreur num \n"); | ||
23 | + return -1; | ||
24 | +} | ||
25 | + | ||
26 | +int ajout_mot(struct cell ** d,char * m){ | ||
27 | + char c; | ||
28 | + struct cell **tmp1 , *tmp2 ; | ||
29 | + int x=0; | ||
30 | + if (*d==NULL){return EXIT_FAILURE;} | ||
31 | + tmp1=d; | ||
32 | + c=m[x]; | ||
33 | + while (c != '\0'){ | ||
34 | + //printf("%c",c); | ||
35 | + if ((*tmp1)->liste[num(c)]==NULL){ | ||
36 | + tmp2=malloc(sizeof(struct cell)); | ||
37 | + (*tmp1)->liste[num(c)]=tmp2; | ||
38 | + } | ||
39 | + tmp1=&((*tmp1)->liste[num(c)]); | ||
40 | + x++; | ||
41 | + c=m[x]; | ||
42 | + } | ||
43 | + (*tmp1)->stop=1; | ||
44 | + //printf("\n"); | ||
45 | + return 1; | ||
46 | +} | ||
47 | + | ||
48 | +void creation_dico(FILE *fd, struct cell **d){ | ||
49 | + char s[20]; | ||
50 | + while (fscanf(fd,"%s",s)==1){ | ||
51 | + ajout_mot(d,s); | ||
52 | + } | ||
53 | +} | ||
54 | + | ||
55 | +int fin_mot(char c){ | ||
56 | + if (c == '\0' || c == ' ' || c== '.' || c == ':' || c == ',' || c == '?' || c == ';' || c == '\'' || c == '\"' || c == '!' || c == '`' || c == '-' || c == '_'){ | ||
57 | + return 1;} | ||
58 | + return 0; | ||
59 | +} | ||
60 | + | ||
61 | +int comparaison(char *m , int x , int conjug){ | ||
62 | + if (fin_mot(m[x])==1){ | ||
63 | + return 1; | ||
64 | + } | ||
65 | + if (conjug==1){ | ||
66 | + if (fin_mot(m[x+1])==1 && m[x]=='s'){ | ||
67 | + return 1; | ||
68 | + } | ||
69 | + if (fin_mot(m[x+2])==1 && m[x+1]=='d' && m[x]=='e'){ | ||
70 | + return 1; | ||
71 | + } | ||
72 | + if (fin_mot(m[x+3])==1 && m[x+2]=='g' && m[x+1]=='n' && m[x]=='i'){ | ||
73 | + return 1; | ||
74 | + } | ||
75 | + } | ||
76 | + return 0; | ||
77 | +} | ||
78 | + | ||
79 | +int reconaissance(struct cell * d,char * m){ | ||
80 | + char c; | ||
81 | + struct cell *tmp1; | ||
82 | + int x=0; | ||
83 | + tmp1=d; | ||
84 | + if (d==NULL){return 1;} | ||
85 | + c=m[x]; | ||
86 | + while (comparaison (m,x,1) == 0 ){ | ||
87 | + if (tmp1->liste[num(c)]==NULL){return 1;} | ||
88 | + tmp1=(tmp1->liste[num(c)]); | ||
89 | + x++; | ||
90 | + c=m[x]; | ||
91 | + } | ||
92 | + if (tmp1->stop==1) {return 0;} | ||
93 | + return 1; | ||
94 | +} | ||
95 | + | ||
96 | +int lecture(FILE *fd, struct cell *d){ | ||
97 | + char s[20]; | ||
98 | + int cmpt=0; | ||
99 | + int x; | ||
100 | + while (fscanf(fd,"%s",s)==1){ | ||
101 | + x=reconaissance(d,s); | ||
102 | + cmpt+=x; | ||
103 | + if(x==1){printf("%s \n",s);} | ||
104 | + } | ||
105 | + return cmpt; | ||
106 | +} | ||
107 | + | ||
108 | +void suprime_dico(struct cell **d){ | ||
109 | + int i=0; | ||
110 | + for (i=0;i<26;i++){ | ||
111 | + if ((*d)->liste[i]!=NULL){ | ||
112 | + suprime_dico(&((*d)->liste[i])); | ||
113 | + } | ||
114 | + } | ||
115 | + free(*d); | ||
116 | +} | ||
117 | + | ||
118 | + | ||
119 | +int main(int argc, char *argv[]) | ||
120 | +{ | ||
121 | + if (argc < 3) | ||
122 | + { | ||
123 | + fprintf(stderr, "usage: hash <file_name>\n"); | ||
124 | + return EXIT_FAILURE; | ||
125 | + } | ||
126 | + | ||
127 | + FILE *fp; | ||
128 | + printf("%s\n",argv[1]); | ||
129 | + fp=fopen(argv[1], "r"); | ||
130 | + if (fp==NULL) | ||
131 | + { | ||
132 | + fprintf(stderr, "no such file, or unreachable: %s\n", argv[1]); | ||
133 | + return EXIT_FAILURE; | ||
134 | + } | ||
135 | + | ||
136 | + FILE *fd; | ||
137 | + printf("%s\n",argv[2]); | ||
138 | + fd=fopen(argv[2], "r"); | ||
139 | + if (fd==NULL) | ||
140 | + { | ||
141 | + fprintf(stderr, "no such file, or unreachable: %s\n", argv[2]); | ||
142 | + return EXIT_FAILURE; | ||
143 | + } | ||
144 | + | ||
145 | + | ||
146 | + struct cell *d; | ||
147 | + d=malloc(sizeof(struct cell)); | ||
148 | + creation_dico(fp,&d); | ||
149 | + int inc; | ||
150 | + inc=lecture(fd,d); | ||
151 | + printf("%d mots non reconnus \n", inc); | ||
152 | + suprime_dico(&d); | ||
153 | + free(fd); | ||
154 | + free(fp); | ||
155 | +} |
No preview for this file type