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
| #include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define TAILLE_MAX 100
//les prototypes
int calculNbrDeService();
void chargement();
void affichage();
//les variables globales
int qtyService=0, i;
//services service[qtyService];
//Declaration de la structure SERVICE
typedef struct services services;
struct services
{
char type[50];
int tarif;
struct services *precedent;
struct services *suivant;
};
//Declation et initialisation de variable structuree
services *debut=NULL;
services *fin=NULL;
services *nouveau;
services *courant;
//FONCTION principale
int main()
{
int calculNbrDeService();
printf("Le nombre de service est ");
qtyService = calculNbrDeService();
printf("%d", qtyService);
printf("\nLes service sont\n");
chargement();
affichage();
return 0;
}
//Calcul du nombre de services disponible
int calculNbrDeService()
{
int nbrService=0;
FILE* fichier = NULL;
char chaine[TAILLE_MAX] = "";
fichier = fopen("service.txt", "r");
if (fichier != NULL)
{
while (fgets(chaine, TAILLE_MAX, fichier) != NULL) // On lit le fichier au complet
{
nbrService++; //augmentation du nombre de service
}
fclose(fichier);
}
return nbrService;
}
//CHARGEMENT des services dans la liste chainee
void chargement()
{
//CREATION du pointeur FILE
FILE* fichier = NULL;
char ttype[50];
int ttarif;
//OUVERTURE du fichier service.txt
fichier = fopen("service.txt", "r");
//Allocation d. de memoire
nouveau=(services*)malloc(sizeof(services));
if(nouveau!=NULL)
{
for ( i=0 ; i<qtyService ; i++ )
{
fscanf(fichier, "%s %d\n", ttype, &ttarif);
nouveau->type[0]=&ttype[0];
nouveau->tarif=ttarif;
nouveau->precedent=NULL;
nouveau->suivant=debut;
debut->precedent=nouveau;
debut=nouveau;
}
}
fclose(fichier);
}
//Affichage de la liste
void affichage()
{
courant=debut;//Point de depart
while(courant!=NULL)
{
printf("%s %d", courant->type, courant->tarif);
courant=courant->suivant;
}
getch();
} |
Partager