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
|
// On inclut les bibliotheques indispensables
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include "structures.h"
// On fixe les constantes
#define TAILLE_LIGNE 100
#define TAILLE_CODE_CLIENT 5
clients *nouveau (char *nom_client)
{
clients *p;
p = malloc (sizeof(clients));
p->gauche = NULL;
p->droite = NULL;
p->nom_client = malloc (strlen(nom_client)+1);
strcpy (p->nom_client,nom_client);
return p;
}
void insertion (clients *p, char *nom_client)
{
if (p->nom_client == NULL)
{
p->nom_client = malloc(strlen(nom_client)+1);
strcpy(p->nom_client,nom_client);
}
else if (strcasecmp(nom_client,p->nom_client) <= 0)
{
if (p->gauche != NULL)
insertion (p->gauche,nom_client);
else
p->gauche = nouveau (nom_client);
}
else
{
if (p->droite != NULL)
insertion (p->droite,nom_client);
else
p->droite = nouveau (nom_client);
}
}
void affichage (clients *p)
{
if (p != NULL)
{
affichage (p->gauche);
if (p->nom_client != NULL)
printf ("%s\n",p->nom_client);
affichage (p->droite);
}
}
// Fonction chargeant l'arbre "clients"
int chargement(clients **pointeur_racine)
{
FILE *fichier_clients = NULL;
char ligne_lue[TAILLE_LIGNE];
char code_client_lu[TAILLE_CODE_CLIENT];
char nom_client_lu[TAILLE_LIGNE];
char mdp_client_lu[TAILLE_LIGNE];
int solde_client_lu = 0;
int i = 0;
clients *pointeur_racine_bas = NULL;
// On initialise la racine de l'arbre "clients"
clients racine=
{
NULL,NULL,NULL,NULL,NULL,0
};
//On ouvre le fichier contenant les données clients
fichier_clients = fopen("fournisseurs.csv", "r");
// Et on s'assure que l'operation s'est bien deroulee
if(fichier_clients == NULL)
return 0;
// On saute la premiere ligne du fichier
fgets(ligne_lue, TAILLE_LIGNE, fichier_clients);
i = 0;
// Puis on parcoure le fichier en enregistrant au fur et a mesure
while(fgets(ligne_lue, TAILLE_LIGNE, fichier_clients) != NULL)
{
strcpy(code_client_lu, strtok(ligne_lue,";"));
strcpy(nom_client_lu,strtok(NULL,";"));
strcpy(mdp_client_lu, strtok(NULL,";"));
insertion(&racine, nom_client_lu);
}
*pointeur_racine = &racine;
fclose(fichier_clients);
return 1;
} |
Partager