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
|
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
FILE *fd;
struct enreg
{
char nom[30];
char tel[10];
};
int question(char*texte)
{
char reponse=' ';
while (reponse!='o' && reponse!='n')
{
printf("%s",texte);
reponse=getchar();
printf("\n");
/*convertion des majuscules en minuscules*/
if (reponse=='O') reponse='o';
if (reponse=='N') reponse='n';
if (reponse!='o' && reponse!='n')
printf("!!!repondre par O ou N!!!\n");
};
if (reponse=='o')
return 0;
else return -1;
}
/*création d'une nouvelle fiche*/
void creation()
{
/*variable locales*/
struct enreg fiche;
int reponse;
/*demande du nom et du numero de téléphone*/
printf("...Creation d'une fiche...\n");
printf("Nom ? ");
scanf("%s",&fiche.nom);
printf("Numero de telephone ? ");
scanf("%s",&fiche.tel);
/*Demande confirmation d'enregistrement*/
reponse=question("Enregistrer la fiche (O/N) ?");
/*si NON, on sort directement*/
if (reponse)
return;
/*se placer à la fin du fichier*/
fseek(fd,0,2);
/*sinon on enregistre à la fin du fichier*/
fprintf(fd,"%s %s\n",fiche.nom,fiche.tel);
}
int rechercher()
{
/*variable locales*/
struct enreg fiche;
char nom[30];
int err;
int reponse;
/*demande le nom à rechercher*/
printf("...rechercher une fiche...\n");
printf("nom a chercher ? ");
scanf("%s",&nom);
/*positionnement au début du fichier*/
fseek(fd,0,0);
/*lecture d'une fiche*/
err=fscanf(fd,"%s%s",&fiche.nom,&fiche.tel);
/*si fin du fichier ou erreur*/
while (err>0)
{
/*si la fiche correspond*/
if (strcmp(nom,fiche.nom)==0)
{
printf("nom:%s telephone:%s\n",fiche.nom,fiche.tel);
reponse=question("continuer la recherche (O/N)?");
if (reponse)
return 0;
}
/*on lit une autre fiche*/
err=fscanf(fd,"%s%s",&fiche.nom,&fiche.tel);
}
/*fin*/
printf("fin du fichier, appuyer sur une touche...\n");
getchar();
}
int menu()
{
int choix=0;
while (choix<1 || choix>3)
{
printf("menu : \n");
printf("1 - creation d'une nouvelle fiche\n");
printf("2 - recherche d'une fiche\n");
printf("3 - quitter\n");
printf("votre choix ? ");
scanf("%i",&choix);
if (choix<1 || choix>3)
printf("!!!entrez 1, 2 ou 3 !!!\n");
}
return choix;
}
/*programme principale*/
void main()
{
int choix=0;
fd=fopen("rep.dat","a+");
printf("...REPERTOIRE TELEPHONIQUE...\n");
while (choix!=3)
{
choix=menu();
switch (choix)/*suivant le choix*/
{
case 1 : creation();/*Cas 1, accéder àla fonction création*/
break;
case 2 : rechercher();/*cas 2, accéder à la fonction rechercher*/
break;
default : break;/*quitter (ne rien faire)*/
}
}
fclose(fd);/*Fermeture du fichier*/
/*Fin*/
printf("Merci, et au revoir\n");
} |
Partager