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
| #include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <time.h>
#define TAILLE_MAX 100
//les prototypes
int calculNbrDeService();
void chargement();
void affichage();
void chargementSiVide();
//les variables globales
int qtyService=0, i=0, taille=0;
//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 *courant;
services *nouveau;
services *fin=NULL;
//FONCTION principale
int main()
{
int calculNbrDeService();
printf("Le nombre de service est ");
qtyService = calculNbrDeService();
printf("%d", qtyService);
printf("\n\nLes service sont\n");
chargement(qtyService);
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
}
fseek(fichier, 0, SEEK_SET);
fclose(fichier);
}
return nbrService;
}
//CHARGEMENT des services dans la liste chainee
void chargement()
{
//CREATION du pointeur FILE
FILE* fichier = NULL;
//OUVERTURE du fichier service.txt
fichier = fopen("service.txt", "r");
fseek(fichier, 0, SEEK_SET);
//Allocation d. de memoire
nouveau=(services*)malloc(sizeof(services));
if(nouveau!=NULL)
{
//chargement quand il y a deja des elements
if (taille!=0)
{
for ( i=0 ; i<qtyService ; i++ )
{
fscanf(fichier, "%s %d\n", &nouveau->type, &nouveau->tarif);
nouveau->precedent=NULL;
nouveau->suivant=debut;
debut->precedent=nouveau;
debut=nouveau;
taille++;
}
}
else
{
//chargementSiVide
for ( i=0 ; i<qtyService ; i++ )
{
fscanf(fichier, "%s %d\n", &nouveau->type, &nouveau->tarif);
nouveau->precedent=debut;
nouveau->suivant=fin;
debut=nouveau;
fin=nouveau;
taille++;
}
}
}
fclose(fichier);
}
//Affichage de la liste
void affichage()
{
courant=debut;//Point de depart
for ( i=0 ; i<qtyService ; i++)
{
printf("%d.- %s %d\n", i, courant->type, courant->tarif);
courant=courant->suivant;
}
getch();
}
void freeList()
{
services*suivant;
while(debut!=NULL)
{
suivant=debut->suivant;
free(debut);
debut=suivant;
}
printf("It's free!!;)");
} |
Partager