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
|
#include<stdio.h>
#include<conio.h>
#include<string.h>
/*ce script permet de éffecter une recherche dichatomique */
typedef struct
{
int num;
char nom[10];
}etud;
int main()
{
FILE *f;
int sup,inf,trouve=0,milieu;
char nom_chercher[20];
etud e;
printf("Taper l'element à chercher\n");
gets(nom_chercher);
f=fopen("etudiant.dat","r+");
fseek(f,0,0);
sup=ftell(f)/sizeof(etud);
do
{
milieu=sup-inf/2;
/*positioner le cursur au milieu*/
fseek(f,(milieu-1)*sizeof(etud),0);
fread(&e,sizeof(etud),1,f);
/*tester cet element */
if(strcmp(e.nom,nom_chercher)==0)
trouve=1;
else
{
if(strcmp(e.nom,nom_chercher)>0)
sup=milieu-1;
else
inf=milieu+1;
}
}while(!trouve&&sup>inf);
if(trouve==0)
printf("L'element n'existe pass ");
else
printf("L'element existe ");
getch();
return 0;
} |
Partager