| 12
 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
 
 | #include<stdio.h>
#include<string.h>
#include<stdlib.h>
 
#define MAXMOTS 100
#define LONGMOT 20
 
short dichotomie(char**dico,char*mot,short nb_mots);
int main(void)
{
	/*déclaration et initialisation des variables*/
	short nb_mots=1;
	char*mot;
	char**dico;
	int termine;
	short motrouve;
	termine=0;
	mot=malloc(LONGMOT *sizeof(char));
	dico=malloc(MAXMOTS *sizeof(char*));
	/*Saisi des mots du dictionnaire*/ 
	while((termine!=1)&&(nb_mots<MAXMOTS))
	{
		printf("Entrez un mot (fin pour terminer): ");
		scanf("%s",mot);
		if (strcmp(mot,"fin")==0)
		{
			termine=1;
		}
		else
		{
			*(dico+nb_mots)=malloc(strlen(mot)+1);
			strcpy(*(dico+nb_mots),mot);
			nb_mots++;
		}
	}
	motrouve=dichotomie(dico,mot,nb_mots);
	if (motrouve==1)
	{
		printf("Le mot %s a ete trouve.\n",mot);
		scanf("%s",mot);
	}
	else 
	{
		printf("Le mot %s est introuvable.\n",mot);
		scanf("%s",mot);
	}
	/*libération d'espace mémoire*/
	free(mot);
	free(dico);
}
short dichotomie(char**dico,char*mot,short nb_mots)
{
	/*initialisation des variables*/
	int i,j,k,trouve,inter;
	i=0;
	j=nb_mots;
	k=(nb_mots/2);
	/*Saisi du mot à trouver*/
	printf("Entrez le mot a trouver: ");
	scanf("%s",mot);
	/*recherche du mot saisi dans le dictionnaire*/
	do
	{
		inter=i-j;
		k=(j+i)/2;
		trouve=strcmp(*(dico+k),mot);
		if (trouve>0) j=k;
		if (trouve<0) i=k;
		if (trouve==0) inter=1;
	}
	while(inter>1);
	/*renvoi resultat de la recherche*/
	if (trouve==1)
	{
		return 1;
	}
	else
	{
		return 0;
	}
} | 
Partager