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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
| #include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define MAXMOTS 100 //nombre maximum de mots dans le dictionnaire
#define LONGMOT 20 //longueur en carcatere d'un mot
char dico[LONGMOT][MAXMOTS];
char mot[LONGMOT];
char *pmot;
int i=0;
FILE*f;
//initialisation de la fonction de recherche dichotomique
short dichotomie(char dico[LONGMOT][MAXMOTS],char *mot,short nb_mots){
int i,j,k,trouve,indice; //i->debut, j->fin
int strcmp (const char *s1, const char *s2);
i=0;
j=nb_mots;
trouve=0;
while(i<=j && trouve == 0) {
int moy; //moyenne de(i,j)
moy=(i+j)/2;
k=moy;
if(strcmp(dico[k],mot)==0) {
trouve=1;
}
if(strcmp(dico[k],mot)>0) {
j=k-1;}
else{
i=k+1;
}
}
if(trouve==0){indice=0;} //si mot n'a pas été trouvé
if(trouve==1){indice=1;} //si mot a été trouvé
return indice;
}
//initialisation des fonction de supression des majuscules et de la ponctuation
void sup_ponc(char *pmot){
int i=0,j=0;
char c;
//Traitement
while (pmot[i]!= '/0'){
c=pmot[i];
if (isalpha(c)){
pmot[j]=c;
j++;
}
i++;
}
pmot[j]=0;
}
void sup_maj(char *pmot){
int i=0;
char c;
//Traitement
while (pmot[i]!='/0'){
c=pmot[i];
if (isupper(c))
c=tolower(c);
pmot[i]=c;
i++;
}
pmot[i]=0;
}
//fonction ajout
void ajout(char dico[LONGMOT][MAXMOTS],char *mot,short nb_mot)
{
//Copie du nouveau mot dans le tableau
strcpy(dico[nb_mot],mot);
//Tri en mémoire du tableau dico
qsort((char**)dico,nb_mot,LONGMOT,strcmp);
}
//FONCTION PRINCIPALE
int main (int argc, char **argv[]) {
short nb_mots=0;
char *fichier_texte; //nom du fichier à ouvrir et à traiter
fichier_texte="fichierdevoir";
printf("Veuillez entrer le nom du fichier texte a traiter pour la recherche dichotomique svp: ");
fgets (fichier_texte, sizeof fichier_texte, stdin);
f=fopen("dico.dat","r");
//Test que le fichier dico s'ouvre bien
if ((f=fopen("dico.dat","r")) == NULL)
printf("Impossible d'ouvrir le fichier Dico\n");
//**
printf("lecture mot par mot de tout le fichier et stockage dans un tableau en mémoire ....\n");
while(fgetc(f)!=EOF)
{
int i;
i =0;
fgets(dico[i],19,f);
i++;
}
fclose(f);
nb_mots=100;
//**
printf("Tri en mémoire du tableau dico....\n");
qsort((char**)dico,nb_mots,LONGMOT,strcmp);
f=fopen("fichierdevoir","r");
//Test que le fichier s'ouvre bien
if ((f=fopen("fichierdevoir.dat","r")) == NULL)
printf("Impossible d'ouvrir le fichier texte");
pmot=mot;
//**
printf("Traitement pour chaque mot: supression des majuscules et de la ponctuation....");
while (fscanf(f,"%s",mot) != EOF){
sup_ponc(pmot);
sup_maj(pmot);
printf("Recherche du mot dans le tableau dico .....");
dichotomie(dico, mot, nb_mots);
if (dichotomie==0){
char rep[5]; //variable pour la réponse de l'utilisateur
fprintf (f,"Le mot n'existe pas. Voulez vous l'ajouter au dictionnaire? \n");
fscanf(f,"%s",&rep);
if (strcmp(rep,"oui")==0) {
ajout(dico,mot,nb_mots);
}
}
}
fclose(f);
f=fopen("dico.dat","a");
for (i=0; i<nb_mots; i++) {
fprintf(f,"%s\n",dico[i]);
}
fclose(f);
} |