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
| #include<stdio.h>
#include<string.h>
//INITIALISATION
//initialisation de la fonction dichotomie
short dichotomie(char **dico,char *mot,short nb_mots){
int i,j,k;
i=0;
j= nb_mots;
for(i=0; i<=nb_mots -1; i++) { //execution de la boucle tant que (i,j) supérieur à 1
//puisque j=nb_mots, alors (i,j) sera supérieur à 1 pour
//i< nb_mots -1
int moy; //moyenne de(i,j)
moy=(i+j)/2;
k=moy;
}
if(dico[k] > mot) {
j=k;
}
else {
i = k;
}
//comparaison de mot avec le i ème et le j ème élément du dico
if (mot==dico[i]){return 0;}
if(mot==dico[j]){return 0;}
else{return 1;}
}
//EXECUTION
//PROGRAMME PRINCIPAL
int main(void){
char *mot;
short nb_mots;
//exemple d'initialisation de dico pour notre exemple
char *dico[5]={"arbre","voiture","cheval","malade","besoin"};
nb_mots=5; //initialisation par exemple
//saisie par l'utilisateur du mot à rechercher
printf("Veuillez saisir un mot pour la recherche dichotomique: \n ");
scanf_s("%c",&mot);
//recherche dichotomique
dichotomie(dico, mot, nb_mots);
if(dichotomie(dico, mot, nb_mots)==0 ){
printf("Le mot saisie est bien dans le dictionnaire!\n");
}
else {
printf("Le mot saisie n'est pas dans le dictionnaire!\n");
}
return 0;
} |
Partager