Commit ccb47d03ed117b4a117a9f148808f291c7a69785
1 parent
2c6f0266
menu quit ok
Showing
8 changed files
with
266 additions
and
32 deletions
Show diff stats
@@ -6,13 +6,13 @@ void make_empty_dico(dico d){ | @@ -6,13 +6,13 @@ void make_empty_dico(dico d){ | ||
6 | } | 6 | } |
7 | void delete_dico(dico d){ | 7 | void delete_dico(dico d){ |
8 | for(int i=0;i<NBCHAR;i++) | 8 | for(int i=0;i<NBCHAR;i++) |
9 | - delete_tree(d[i]); | 9 | + delete_tree(d+i); |
10 | } | 10 | } |
11 | 11 | ||
12 | 12 | ||
13 | 13 | ||
14 | byte addto_dico(dico d,const string word){ | 14 | byte addto_dico(dico d,const string word){ |
15 | - //-1 if word can't enter, 0if allready in dico, 1 if added | 15 | + //return values : -1 if word can't enter, 0if allready in dico, 1 if added |
16 | int i; | 16 | int i; |
17 | byte isIn=0, endKind=0; | 17 | byte isIn=0, endKind=0; |
18 | endKind=end_kind(word); | 18 | endKind=end_kind(word); |
@@ -76,11 +76,6 @@ void loadfrom_file(dico d,FILE*stream){ | @@ -76,11 +76,6 @@ void loadfrom_file(dico d,FILE*stream){ | ||
76 | 76 | ||
77 | 77 | ||
78 | 78 | ||
79 | - | ||
80 | - | ||
81 | - | ||
82 | - | ||
83 | - | ||
84 | void printto_terminal(dico d){printto_file(d,stdout);} | 79 | void printto_terminal(dico d){printto_file(d,stdout);} |
85 | void printto_file(dico d,FILE*stream){ | 80 | void printto_file(dico d,FILE*stream){ |
86 | if(stream==NULL){ | 81 | if(stream==NULL){ |
@@ -141,16 +136,18 @@ void print(tree t,FILE*stream,string prefix){ | @@ -141,16 +136,18 @@ void print(tree t,FILE*stream,string prefix){ | ||
141 | free(word); | 136 | free(word); |
142 | } | 137 | } |
143 | 138 | ||
139 | +//uppers all letters in str | ||
144 | void strupper(string str){ | 140 | void strupper(string str){ |
145 | for(int i=0;str[i]!='\0';i++) | 141 | for(int i=0;str[i]!='\0';i++) |
146 | str[i]=toupper(str[i]); | 142 | str[i]=toupper(str[i]); |
147 | } | 143 | } |
148 | 144 | ||
149 | 145 | ||
150 | -bool is_in(dico d,string word){ | 146 | +byte is_in(dico d,string word){ |
147 | + //return : -1 if incorrect word, 1 if is in, else 0 | ||
151 | byte endKind=end_kind(word); | 148 | byte endKind=end_kind(word); |
152 | if(!is_word(endKind)){ | 149 | if(!is_word(endKind)){ |
153 | - return false; | 150 | + return -1; |
154 | } | 151 | } |
155 | tree tmp=d[hash(word[0])]; | 152 | tree tmp=d[hash(word[0])]; |
156 | for(int i=1; word[i]!='\0' && word[i]!='\'' && !is_empty(tmp) ;i++){ | 153 | for(int i=1; word[i]!='\0' && word[i]!='\'' && !is_empty(tmp) ;i++){ |
@@ -158,3 +155,44 @@ bool is_in(dico d,string word){ | @@ -158,3 +155,44 @@ bool is_in(dico d,string word){ | ||
158 | } | 155 | } |
159 | return !is_empty(tmp) && (endKind & is_end(tmp)); | 156 | return !is_empty(tmp) && (endKind & is_end(tmp)); |
160 | } | 157 | } |
158 | + | ||
159 | +void test_words(dico d,FILE*inStream,FILE*outStream){ | ||
160 | + stringLIFO absWords=init_stringLIFO(); | ||
161 | + char word[30]={0}; | ||
162 | + byte isIn=0,someWrong=0; | ||
163 | + while(fscanf(inStream,"%30s",word)!=EOF){ | ||
164 | + isIn=is_in(d,word); | ||
165 | + if(isIn<1){ | ||
166 | + if(someWrong==0){ | ||
167 | + someWrong=1; | ||
168 | + fprintf(outStream,"Voici les mots absent du dictionnaire"); | ||
169 | + } | ||
170 | + fprintf(outStream,"%s est absent dans le dictionnaire",word); | ||
171 | + if(isIn) | ||
172 | + fprintf(outStream,", car il est incorrect.\n"); | ||
173 | + else{ | ||
174 | + push(&absWords,word); | ||
175 | + fprintf(outStream,".\n"); | ||
176 | + } | ||
177 | + } | ||
178 | + } | ||
179 | + //ajout des mots absent? | ||
180 | + if(is_emptyLIFO(absWords))return; | ||
181 | + printf("Voulez-vous ajouter certains de ces mots au dictionnaire?\n(1 for yes,0 for no)\n"); | ||
182 | + someWrong=saisie(0,1); | ||
183 | + | ||
184 | + //ajout de certains mots au cas par cas | ||
185 | + if(someWrong){ | ||
186 | + string aword; | ||
187 | + while(!is_emptyLIFO(absWords)){ | ||
188 | + aword=pop(&absWords); | ||
189 | + printf("Voulez-vous ajouter %s au dictionnaire? (1 for yes,0 for no)\n",aword); | ||
190 | + isIn=saisie(0,1); | ||
191 | + if(isIn){ | ||
192 | + addto_dico(d,aword); | ||
193 | + } | ||
194 | + free(aword); | ||
195 | + } | ||
196 | + } | ||
197 | + delete_LIFO(&absWords); | ||
198 | +} |
@@ -2,6 +2,7 @@ | @@ -2,6 +2,7 @@ | ||
2 | #define DICO_H | 2 | #define DICO_H |
3 | 3 | ||
4 | #include "treeh.h" | 4 | #include "treeh.h" |
5 | +#include "functions.h" | ||
5 | 6 | ||
6 | typedef tree dico[NBCHAR]; | 7 | typedef tree dico[NBCHAR]; |
7 | 8 | ||
@@ -19,6 +20,7 @@ void print(tree,FILE*,string); | @@ -19,6 +20,7 @@ void print(tree,FILE*,string); | ||
19 | 20 | ||
20 | void strupper(string str); | 21 | void strupper(string str); |
21 | 22 | ||
22 | -bool is_in(dico,string); | 23 | +byte is_in(dico,string); |
24 | +void test_words(dico,FILE*inStream,FILE*outStream); | ||
23 | 25 | ||
24 | #endif | 26 | #endif |
@@ -0,0 +1,67 @@ | @@ -0,0 +1,67 @@ | ||
1 | +#include <stdio.h> | ||
2 | +#include "functions.h" | ||
3 | + | ||
4 | +int saisie(int min,int max){ | ||
5 | + int n; | ||
6 | + do{scanf("%d",&n); | ||
7 | + }while(n<min || n>max); | ||
8 | + return n; | ||
9 | +} | ||
10 | + | ||
11 | +int ctoi(char c){ | ||
12 | + if(c>'9' || c<'0') | ||
13 | + return -1; | ||
14 | + return c-'0'; | ||
15 | +} | ||
16 | + | ||
17 | +void clear(){ | ||
18 | +#ifdef WIN32 | ||
19 | + system("cls"); | ||
20 | +#else | ||
21 | + #ifdef UNIX | ||
22 | + system("clear"); | ||
23 | + #else | ||
24 | + for(int i=0;i<30;i++)printf("\n"); | ||
25 | + #endif | ||
26 | +#endif | ||
27 | +} | ||
28 | + | ||
29 | +void pause(){ | ||
30 | + getchar(); | ||
31 | + printf("tapez la touche entree"); | ||
32 | + fflush(stdout); | ||
33 | + while(getchar()!='\n'); | ||
34 | +} | ||
35 | + | ||
36 | + | ||
37 | + | ||
38 | +//stringLIFO | ||
39 | +stringLIFO init_stringLIFO(){return NULL;} | ||
40 | + | ||
41 | +int is_emptyLIFO(stringLIFO list){return list==NULL;} | ||
42 | + | ||
43 | +void delete_LIFO(stringLIFO*list){ | ||
44 | + while(!is_emptyLIFO(*list)) | ||
45 | + free(pop(list)); | ||
46 | +} | ||
47 | + | ||
48 | +string top(stringLIFO list){return is_emptyLIFO(list)?NULL:list->val;} | ||
49 | + | ||
50 | +void push(stringLIFO *list,string s){ | ||
51 | + cell*c=malloc(sizeof(cell)); | ||
52 | + c->val=calloc((strlen(s)+1),sizeof(char)); | ||
53 | + strcpy(c->val,s); | ||
54 | + c->next=*list; | ||
55 | + *list=c; | ||
56 | +} | ||
57 | + | ||
58 | +string pop(stringLIFO*list){ | ||
59 | + string ret=NULL; | ||
60 | + if(!is_emptyLIFO(*list)){ | ||
61 | + cell*c=(*list)->next; | ||
62 | + ret=(*list)->val; | ||
63 | + free(*list); | ||
64 | + *list=c; | ||
65 | + } | ||
66 | + return ret; | ||
67 | +} |
@@ -0,0 +1,23 @@ | @@ -0,0 +1,23 @@ | ||
1 | +#ifndef FUNCTIONS_H | ||
2 | +#define FUNCTIONS_H | ||
3 | + | ||
4 | +#include <stdlib.h> | ||
5 | +#include <string.h> | ||
6 | + | ||
7 | +int saisie(int min,int max); | ||
8 | +int ctoi(char); | ||
9 | +void pause(); | ||
10 | +void clear(); | ||
11 | + | ||
12 | +typedef char* string; | ||
13 | +typedef struct _cell cell,*stringLIFO; | ||
14 | +struct _cell{string val;cell* next;}; | ||
15 | + | ||
16 | +stringLIFO init_stringLIFO(); | ||
17 | +int is_emptyLIFO(stringLIFO); | ||
18 | +void push(stringLIFO*,string); | ||
19 | +string pop(stringLIFO*); | ||
20 | +string top(stringLIFO); | ||
21 | +void delete_LIFO(stringLIFO*); | ||
22 | + | ||
23 | +#endif |
@@ -3,26 +3,130 @@ | @@ -3,26 +3,130 @@ | ||
3 | 3 | ||
4 | 4 | ||
5 | 5 | ||
6 | -int main(int argc, char* argv[]){ | ||
7 | - if(argc<2){ | ||
8 | - printf("not enough arguments\n"); | ||
9 | - return EXIT_FAILURE; | ||
10 | - } | ||
11 | - FILE*fileIn=fopen(argv[1],"r"); | ||
12 | - if(fileIn==NULL){ | ||
13 | - printf("wrong arguments\n"); | ||
14 | - return EXIT_FAILURE; | 6 | + |
7 | + | ||
8 | +FILE* getFile(string mode){ | ||
9 | + if(strcmp(mode,"r"))//mode!"=r", gestion de l'affichage suivant si mode= "r" ou "w" | ||
10 | + printf("Tapez un nom du fichier avec son extension pour diriger la sortie des tests vers ce fichier.\n"); | ||
11 | + else printf("Tapez \"terminal\" pour entrer des mots depuis le terminal.\nTapez le nom du fichier avec son extension pour charger des mots depuis ce fichier\n"); | ||
12 | + FILE*file=NULL; | ||
13 | + char fileName[30]={0}; | ||
14 | + | ||
15 | + //get the fileName | ||
16 | + scanf("%30s",fileName); | ||
17 | + if(!strcmp(fileName,"terminal")){ | ||
18 | + if(!strcmp(mode,"r")) | ||
19 | + file=stdin;} | ||
20 | + else file=fopen(fileName,mode); | ||
21 | + | ||
22 | + //while there is a problem about it | ||
23 | + while(file==NULL){ | ||
24 | + printf("desole, impossible d'ouvrir ce fichier.\nVeuillez essayer un autre fichier"); | ||
25 | + scanf("%30s",fileName); | ||
26 | + if(!strcmp(fileName,"terminal")){ | ||
27 | + if(!strcmp(mode,"r")) | ||
28 | + file=stdin;} | ||
29 | + else file=fopen(fileName,mode); | ||
15 | } | 30 | } |
16 | - FILE*fileOut=fopen("out.txt","w"); | ||
17 | 31 | ||
18 | - printf("bienvenue dans le dictonnaire\n\n"); | ||
19 | - dico monDico; | ||
20 | - make_empty_dico(monDico); | 32 | + return file; |
33 | +} | ||
34 | + | ||
35 | + | ||
36 | + | ||
37 | + | ||
38 | + | ||
39 | +void menu(dico monDico){ | ||
40 | + int choice=0; | ||
41 | + char word[2]={0}; | ||
42 | + //initialisation | ||
43 | + printf("Pour commencer, veuillez charger un dictionnaire.\n"); | ||
44 | + FILE*fileIn=getFile("r"),*fileOut=stdout; | ||
21 | loadfrom_file(monDico,fileIn); | 45 | loadfrom_file(monDico,fileIn); |
22 | - printto_file(monDico,stdout); | ||
23 | - delete_dico(monDico); | ||
24 | fclose(fileIn); | 46 | fclose(fileIn); |
25 | - fclose(fileOut); | ||
26 | 47 | ||
48 | + //boucle | ||
49 | + while(1){ | ||
50 | + clear(); | ||
51 | + //menu | ||
52 | + printf("La sortie est branchee sur %s.\n",fileOut==stdout?"le terminal":"un fichier"); | ||
53 | + printf("Que voulez-vous faire ?\n"); | ||
54 | + //end programme | ||
55 | + printf("0) Fermer le programme.\n\n"); | ||
56 | + //just about the dictionnary | ||
57 | + printf("1) Lire le dictionnaire.\n"); | ||
58 | + printf("2) Rajouter des mots au dictionnaire\n"); | ||
59 | + printf("3) Effacer le dictionnaire\n"); | ||
60 | + printf("4) Remplacer le dictionnaire actuel par un autre dictionnaire\n\n"); | ||
61 | + //word correction | ||
62 | + printf("5) Tester si un mot, un ensemble de mot, une phrase ou un paragraphe appartient au dictionnaire.\n\n"); | ||
63 | + //out stream | ||
64 | + printf("6) Définir un fichier de sortie.\n"); | ||
65 | + printf("7) Définir la sortie sur le terminal.\n"); | ||
66 | + | ||
67 | + scanf("%1s",word); | ||
68 | + choice=ctoi(*word); | ||
69 | + | ||
70 | + switch(choice){ | ||
71 | + case 0: {//sortie du programme | ||
72 | + if(fileOut!=stdout) | ||
73 | + fclose(fileOut); | ||
74 | + return;} | ||
75 | + case 1: {//Lire le dictionnaire | ||
76 | + printto_file(monDico,fileOut); | ||
77 | + printf("dictionnaire imprimme\n"); | ||
78 | + break;} | ||
79 | + | ||
80 | + //manip sur le dictionnaire | ||
81 | + case 2: {//ajout | ||
82 | + fileIn=getFile("r"); | ||
83 | + loadfrom_file(monDico,fileIn); | ||
84 | + fclose(fileIn); | ||
85 | + break;} | ||
86 | + case 3: {//effacer | ||
87 | + delete_dico(monDico); | ||
88 | + printf("Le dictionnaire est maintenant vide.\n"); | ||
89 | + break;} | ||
90 | + case 4: {//remplacer | ||
91 | + delete_dico(monDico); | ||
92 | + fileIn=getFile("r"); | ||
93 | + loadfrom_file(monDico,fileIn); | ||
94 | + fclose(fileIn); | ||
95 | + break;} | ||
96 | + | ||
97 | + | ||
98 | + case 5: {//faire des tests | ||
99 | + fileIn=getFile("r"); | ||
100 | + test_words(monDico,fileIn,fileOut); | ||
101 | + fclose(fileIn); | ||
102 | + break;} | ||
103 | + | ||
104 | + //définir la sortie des tests et de "Lire" le dictionnaire | ||
105 | + case 6: { | ||
106 | + if(fileOut!=stdout) | ||
107 | + fclose(fileOut); | ||
108 | + fileOut=getFile("w"); | ||
109 | + break;} | ||
110 | + case 7: { | ||
111 | + if(fileOut!=stdout) | ||
112 | + fclose(fileOut); | ||
113 | + fileOut=stdout; | ||
114 | + break;} | ||
115 | + //si le choix n'est pas parmi les précédents | ||
116 | + default: {break;} | ||
117 | + } | ||
118 | + choice=0; | ||
119 | + pause(); | ||
120 | + }//end of while | ||
121 | +} | ||
122 | + | ||
123 | + | ||
124 | +int main(void){ | ||
125 | + printf("Bienvenue dans le dictonnaire !\n\n"); | ||
126 | + dico monDico; | ||
127 | + make_empty_dico(monDico); | ||
128 | + menu(monDico); | ||
129 | + delete_dico(monDico); | ||
130 | + printf("Le dictionnaire vous dit aurevoir.\n\n\n"); | ||
27 | return EXIT_SUCCESS; | 131 | return EXIT_SUCCESS; |
28 | } | 132 | } |
makefile
treeh.c
@@ -21,11 +21,11 @@ node* make_node(char l,byte end){ | @@ -21,11 +21,11 @@ node* make_node(char l,byte end){ | ||
21 | n->next[i]=make_empty_tree(); | 21 | n->next[i]=make_empty_tree(); |
22 | return n; | 22 | return n; |
23 | } | 23 | } |
24 | -void delete_tree(tree t){ | ||
25 | - if(is_empty(t))return; | 24 | +void delete_tree(tree *t){ |
25 | + if(is_empty(*t))return; | ||
26 | for(int i=0;i<NBCHAR;i++) | 26 | for(int i=0;i<NBCHAR;i++) |
27 | - delete_tree(t->next[i]); | ||
28 | - free(t); | 27 | + delete_tree(&((*t)->next[i])); |
28 | + free(*t); | ||
29 | } | 29 | } |
30 | 30 | ||
31 | 31 |
treeh.h
@@ -31,7 +31,7 @@ struct _node{ | @@ -31,7 +31,7 @@ struct _node{ | ||
31 | tree make_empty_tree();//create a null node* | 31 | tree make_empty_tree();//create a null node* |
32 | node* make_empty_node();//malloc a node and initialize it | 32 | node* make_empty_node();//malloc a node and initialize it |
33 | node* make_node(char,byte);//id | 33 | node* make_node(char,byte);//id |
34 | -void delete_tree(tree);//free(tree) and delete tree on all t->next | 34 | +void delete_tree(tree*);//free(tree) and delete tree on all t->next |
35 | 35 | ||
36 | bool is_empty(const tree);//==NULL | 36 | bool is_empty(const tree);//==NULL |
37 | bool is_followed(const tree);//if true tree has following letters | 37 | bool is_followed(const tree);//if true tree has following letters |