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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
| #include<stdio.h>
#include<stdlib.h>
struct client
{
int numero;
char nom[21];
float solde;
};
struct noeud
{
struct client cl;
struct noeud*fg;
struct noeud*fd;
};
void recopie(char *metici,char *copieca)
{ int i;
for(i=0;!i || metici[i-1]!='\0';i++) /* !i est identique à i==0: il faut au moins recopier le 1er élément*/
metici[i]=copieca[i];
}
egalise(struct client *a,struct client *b)
{
recopie(a->nom,b->nom);
a->solde=b->solde;
a->numero=b->numero;
}
void affiche_client(struct client Mr)
{
printf("Client Nr: %d",Mr.numero);
printf("\tSolde: %.2f",Mr.solde);
printf("\tNom: %s\n",Mr.nom);
}
struct client new_client(void)
{ struct client Mr;
printf("\nEntrer le num%cro du client: ",130);
scanf("%d",&Mr.numero);
printf("\nEntrer son nom(Max 20 caract%cres SANS ESPACE+Enter): ",138);
fflush(stdin);
gets(Mr.nom);
printf("\nEntrer son solde: ");
scanf("%f",&Mr.solde);
return Mr;
}
struct noeud *new_noeud(struct client c)
{
struct noeud *ptr;
ptr=(struct noeud*) malloc(sizeof(struct noeud));
if(!ptr)
printf("\nPlus assez de m%cmoire",130);
else
{
egalise(&(ptr->cl),&c);
ptr->fg=NULL;
ptr->fd=NULL;
}
return ptr;
}
int est_plus_petit_ou_egal(char *petit,char *grand)
{
int ok=-1,i=0;
while(ok==-1 && grand[i]!='\0' && petit[i]!='\0')
{
if((int)grand[i]==(int)petit[i])
i++;
else
{ if((int)grand[i]>(int)petit[i])
ok=1;
else
ok=0;
}
}
if(ok==-1)
{ if(petit[i]!='\0')
ok=0;
else
ok=1;
}
return ok;
}
void inserer(struct noeud **racine, struct noeud *nouv)
{
if(*racine==NULL)
*racine = nouv;
else
{
if(est_plus_petit_ou_egal(((*nouv).cl).nom,((**racine).cl).nom))
inserer((&(*racine)->fg),nouv);
else
inserer((&(*racine)->fd),nouv);
}
}
int compare(char *MotA,char *MotB)
{ int i=0;
while (MotA[i]==MotB[i] && MotA[i]!='\0')
i++;
if(MotA[i]==MotB[i])
return 1;
else
return 0;
}
void recherche1(struct noeud *racine,char *name)
{
if(racine)
{
if(compare((racine->cl).nom,name))
affiche_client(racine->cl);
recherche1(racine->fg,name);
recherche1(racine->fd,name);
}
}
int recherche2(struct noeud *racine,char *name, int number)
{ int ok=0;
while(racine && !ok)
{ if(compare((racine->cl).nom,name) && ((racine->cl).numero)==number)
ok=1;
else
{
if(est_plus_petit_ou_egal(name,(racine->cl).nom))
racine=racine->fg;
else
racine=racine->fd;
}
}
return ok;
}
int supprimer(struct noeud **racine, char *name,int number)
{
struct noeud *memo;
while(*racine!=NULL && (!compare(((**racine).cl).nom,name) || (((**racine).cl).numero)!=number))
{
if(est_plus_petit_ou_egal(name,((**racine).cl).nom))
racine=&((**racine).fg);
else
racine=&((**racine).fd);
}
if(*racine)/*Si le noeud désigné par **racine est à supprimer*/
{ /*On va le remplacer par le plus petit des plus grands--> voir s'il en existe au moins un*/
memo=*racine;
if((**racine).fd)
{ /*si il existe au moins un plus grand (un fils droit)*/
/*printf("\n Il existe au moins un noeud a droite");*/
racine=&((**racine).fd);
while((**racine).fg!=NULL)
racine=&((**racine).fg);
/*On a trouvé le plus petit des plus grands: le noeud **racine*/
egalise(&(memo->cl),&((**racine).cl));/*remplacer l'info du haut par plus petit des plus grands puis supprimmer le noeud du bas*/
memo=*racine;
*racine=(**racine).fd;
}
else
{ /*si pas de fils droit (pas de plus grand)*/
/*printf("\n Il n'existe pas de noeud a droite");*/
*racine=(**racine).fg;
}
free(memo);
return 1;
}
else
return 0;
}
ecrire_preordre(FILE *fp,struct noeud *racine)
{
if(racine)
{
fprintf(fp,"%s %f %d ",(racine->cl).nom,(racine->cl).solde,(racine->cl).numero);
ecrire_preordre(fp,racine->fg);
ecrire_preordre(fp,racine->fd);
}
}
void ecrit_fichier(char cible[21],struct noeud **racine)
{
FILE *fp;
if((fp=fopen(cible,"w"))==NULL)
printf("ERREUR: impossible d'ouvrir le fichier \"%s\".\n",cible);
else
ecrire_preordre(fp,*racine);
if(!fclose(fp)) /*fclose renvoie la valeur 0 si tout va bien*/
printf("\nLa fermeture du fichier (apr%cs %ccriture) s'est d%croul%ce normalement.\n",138,130,130,130);
else
printf("Erreur de fermeture du fichier.");
}
void charger_fichier(char cible[21],struct noeud **racine)
{
FILE *fp;
struct noeud *monsieur;
char name[21];
float saldo;
int nbr;
monsieur=(struct noeud*) malloc(sizeof(struct noeud));
if((fp=fopen(cible,"r"))==NULL)
printf("ERREUR: impossible d'ouvrir le fichier \"%s\".\n",cible);
else
{ while(fscanf(fp,"%s %f %d ",name,&saldo,&nbr)==3)
{
(monsieur->cl).numero=nbr;
(monsieur->cl).solde=saldo;
recopie((monsieur->cl).nom,name);
monsieur->fd=NULL;
monsieur->fg=NULL;
inserer(racine,monsieur);
monsieur=(struct noeud*) malloc(sizeof(struct noeud));
}
}
if(!fclose(fp)) /*fclose renvoie la valeur 0 si tout va bien*/
printf("\nLa fermeture du fichier s'est d%croul%ce normalement.\n",130,130);
else
printf("Erreur de fermeture du fichier.");
}
void symetrique(struct noeud *racine)
{
if(racine)
{
symetrique(racine->fg);
affiche_client(racine->cl);
symetrique(racine->fd);
}
}
main()
{ struct noeud *debut=NULL;
struct noeud** racine=&debut;
int i,n,number,modifie,choix=1;
char name[21];
while(choix)
{
printf("\n Tapez le num%cro correspondant %c votre choix:",130,133);
printf("\n --------------------------------------------");
printf("\n 1: Charger une liste de clients depuis une liste EXISTANTE");
printf("\n 2: Sauvegarder la liste de clients en cours dans un fichier");
printf("\n 3: Inserer un client dans la liste en cours");
printf("\n 4: Rechercher les clients sur base d'un nom (avec homonymes)");
printf("\n 5: Rechercher si un client existe sur base d'un nom et de son numero client");
printf("\n 6: Supprimer un client sur base d'un nom et de son num%cro",130);
printf("\n 7: Afficher la liste de clients en cours par ordre alphab%ctique",130);
printf("\n 0: Quitter le programme\n",130);
scanf("%d",&choix);
switch(choix)
{
case 1: printf("\nVous voulez ouvrir un fichier EXISTANT, entrez le nom du fichier: ");
fflush(stdin);
gets(name);
*racine=NULL;
charger_fichier(name,racine);
printf("\nEn ordre alphab%ctique on obtient le fichier charg%c:\n",130,130);
printf("--------------------------------------------------\n");
symetrique(*racine);
break;
case 2: printf("\nVous voulez sauvegarder le fichier, entrez le nom du fichier ");
printf("destin%c\n%c m%cmoriser les donn%ces(exple:\"client.dat\")\n",130,133,130,130);
fflush(stdin);
gets(name);
ecrit_fichier(name,racine);
break;
case 3: inserer(&(*racine),new_noeud(new_client()));
printf("\nEn ordre alphab%ctique on obtient maintenant le fichier:\n",130);
symetrique(*racine);
break;
case 4: printf("\nIntroduire le nom de client recherch%c(donne les homonymes):\n",130);
fflush(stdin);
gets(name);
recherche1(*racine,name);
break;
case 5: printf("\nIntroduire le num%cro du client recherch%c:\n",130,130);
scanf("%d",&number);
printf("\nIntroduire le nom du client recherch%c:\n",130);
fflush(stdin);
gets(name);
if(recherche2(*racine,name,number))
printf("Ce client existe");
else
printf("Ce client n'existe pas");
break;
case 6: printf("\nIntroduire le num%cro du client %c supprimer:\n",130,133);
scanf("%d",&number);
printf("\nIntroduire le nom du client %c supprimer:\n",133);
fflush(stdin);
gets(name);
modifie=supprimer(racine,name,number);
if(modifie)
printf("C'est fait");
else
printf("Client non trouv%c",130);
printf("\nEn ordre alphab%ctique on obtient maintenant:\n",130);
symetrique(*racine);
break;
case 7: printf("\nEn alphab%ctique on obtient le fichier:\n",130);
symetrique(*racine);
break;
default:printf("\nTout est bien qui finit bien\n",130,130);
}
}
} |
Partager