main.c
3.94 KB
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
#include "dico.h"
FILE* getFile(string mode){
if(strcmp(mode,"r"))//mode!"=r", gestion de l'affichage suivant si mode= "r" ou "w"
printf("Tapez un nom du fichier avec son extension pour diriger la sortie des tests vers ce fichier.\n");
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");
FILE*file=NULL;
char fileName[30]={0};
//get the fileName
scanf("%30s",fileName);
if(!strcmp(fileName,"terminal")){
if(!strcmp(mode,"r"))
file=stdin;}
else file=fopen(fileName,mode);
//while there is a problem about it
while(file==NULL){
printf("desole, impossible d'ouvrir ce fichier.\nVeuillez essayer un autre fichier");
scanf("%30s",fileName);
if(!strcmp(fileName,"terminal")){
if(!strcmp(mode,"r"))
file=stdin;}
else file=fopen(fileName,mode);
}
return file;
}
int main(void){
int choice=0;
printf("Bienvenue dans le dictonnaire !\n\n");
dico monDico;
make_empty_dico(monDico);
//initialisation
printf("Pour commencer, veuillez charger un dictionnaire.\n");
FILE*fileIn=getFile("r"),*fileOut=stdout;
if(fileIn==stdin)
printf("Entrez vos mots (ctrl-D pour terminer le chargement):\n");
loadfrom_file(monDico,fileIn);
if(fileIn!=stdin)
fclose(fileIn);
//boucle
while(1){
clear();
//menu
printf("La sortie est branchee sur %s.\n",fileOut==stdout?"le terminal":"un fichier");
printf("Que voulez-vous faire ?\n");
//end programme
printf("0) Fermer le programme.\n\n");
//just about the dictionnary
printf("1) Lire le dictionnaire.\n");
printf("2) Rajouter des mots au dictionnaire\n");
printf("3) Effacer le dictionnaire\n");
printf("4) Remplacer le dictionnaire actuel par un autre dictionnaire\n\n");
//word correction
printf("5) Tester si un mot, un ensemble de mot, une phrase ou un paragraphe appartient au dictionnaire.\n\n");
//out stream
printf("6) Définir un fichier de sortie.\n");
printf("7) Définir la sortie sur le terminal.\n");
//saisie du choix
choice=getchar();
choice=ctoi(choice);
switch(choice){
case 0: {//sortie du programme
if(fileOut!=stdout)
fclose(fileOut);
fileOut=stdout;
delete_dico(monDico);
printf("Le dictionnaire vous dit aurevoir.\n\n\n");
return EXIT_SUCCESS;
}
case 1: {//Lire le dictionnaire
printto_file(monDico,fileOut);
printf("dictionnaire imprimme\n");
break;}
//manip sur le dictionnaire
case 2: {//ajout
fileIn=getFile("r");
if(fileIn==stdin)
printf("Entrez vos mots (ctrl-D pour terminer le chargement):\n");
loadfrom_file(monDico,fileIn);
if(fileIn!=stdin)
fclose(fileIn);
fileIn=stdin;
break;}
case 3: {//effacer
delete_dico(monDico);
printf("Le dictionnaire est maintenant vide.\n");
break;}
case 4: {//remplacer
delete_dico(monDico);
printf("Le dictionnaire est maintenant vide.\n");
fileIn=getFile("r");
loadfrom_file(monDico,fileIn);
if(fileIn!=stdin)
fclose(fileIn);
fileIn=stdin;
break;}
case 5: {//faire des tests
fileIn=getFile("r");
if(fileIn==stdin)
printf("Entrez vos mots (ctrl-D pour terminer les tests):\n");
test_words(monDico,fileIn,fileOut);
if(fileIn!=stdin)
fclose(fileIn);
fileIn=stdin;
break;}
//définir la sortie des tests et de "Lire" le dictionnaire
case 6: {
if(fileOut!=stdout)
fclose(fileOut);
fileOut=getFile("a");
break;}
case 7: {
if(fileOut!=stdout)
fclose(fileOut);
fileOut=stdout;
break;}
//si le choix n'est pas parmi les précédents
default: {break;}
}
//protection, si le scanf pour le choix a un problème
choice=-1;
pause();
}//end of while
}