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
| #include <stdio.h>
#include <conio.h>
#include <string.h>
#include <malloc.h>
struct composant{ char NOM[30]; //Création d'une structure COMPOSANT comportant 4 éléments
char FABRICANT[30];
char FONCTION[100];
char PRIX[10];
composant *adresse;
};
struct composant *liste_composant;
void CreerListeVide(void);
void CreerListeVide(void)
{
liste_composant=NULL;
}
composant *InsertionEnAd(char Inom[30],char Ifabricant[30],char Ifonction[100],char Iprix[30],struct composant *Iad);
composant *InsertionEnAd(char Inom[30],char Ifabricant[30],char Ifonction[100],char Iprix[30],struct composant *Iad)
{
//on declare et on alloue de la place dans la mémoire
struct composant *nouveau_composant;
nouveau_composant = new struct composant;
//on affecte le contenu de composant dans nouveau_composant
if (nouveau_composant!=NULL)
{
strcpy((*nouveau_composant).NOM , Inom);
strcpy((*nouveau_composant).FABRICANT , Ifabricant);
strcpy((*nouveau_composant).FONCTION , Ifonction);
strcpy((*nouveau_composant).PRIX , Iprix);
//(*nouveau_composant).PRIX = Iprix;
(*nouveau_composant).adresse = NULL;
}
if ( Iad == NULL)
{ //si la liste est vide on renvoie nouveau_composant
return nouveau_composant;
}
else
{ //on declare un pointeur sur liste Iad
struct composant *temp_composant = Iad ;
//qui permet d'assigner l'adresse du chainon suivant
while( temp_composant->adresse != NULL)
{
temp_composant = temp_composant->adresse;
}
temp_composant->adresse = nouveau_composant;
return Iad;
}
}
void Afficher(struct composant *Aad);
void Afficher(struct composant *Aad)
{ //on declare un pointeur sur liste Aad
struct composant *temp_composant = Aad;
//le contenu est vide
if ( temp_composant == NULL)
{
printf(" !Aucun composant(s) present dans la liste chaine! \n");
}
//si le contenu n'est pas vide , on affiche le contenu de chacun des chainons
while(temp_composant != NULL)
{
printf("\nNom : %s\nFabricant : %s\nFonction : %s\nPrix HT : %s Euros",temp_composant->NOM,temp_composant->FABRICANT,temp_composant->FONCTION,temp_composant->PRIX);
//on passe au chainon suivant
temp_composant = temp_composant->adresse;
}
}
void SauvegarderTexte(struct composant *Sad,char Chemin[40]);
void SauvegarderTexte(struct composant *Sad,char Chemin[40])
{
FILE *Fich;
int fermer=0;
//on declare un pointeur sur liste Sad
struct composant *temp_composant = Sad;
/* Ouverture pour ecriture seule*/
if((Fich = fopen(Chemin, "a" )) == NULL ) // "a" permet d'ajouter de nouveau element en fin de fichier
printf("-----------------Le Fichier na pas pu etre ouvert-----------------\n" );
else
{
printf("----------------------Le Fichier est ouvert-----------------------\n" );
/* Ecriture */
if(temp_composant!=NULL)
{ //tant que temp_composant n'est pas le dernier chainon
while(temp_composant != NULL)
{
//fprintf(Fich,"------------------\n");
fprintf(Fich,"%s\n",temp_composant->NOM);
fprintf(Fich,"%s\n",temp_composant->FABRICANT);
fprintf(Fich,"%s\n",temp_composant->FONCTION);
fprintf(Fich,"%s\n",temp_composant->PRIX);
//fprintf(Fich,"-");
//on passe au chainon suivant
temp_composant = temp_composant->adresse;
}
printf("\n SAUVEGARDE EFFECTUE \n" );
}
else
printf("\n !Aucun composant(s) present dans la liste chaine! \n");
/* Fermeture */
}
fermer = _fcloseall();
fclose(Fich);
if(fermer>0)
printf("\n--------------------------Fichier Fermer--------------------------\n");
else
printf("\n------------------------Fichier NON Fermer------------------------\n");
}
void AfficherTexte(char Chemin[40]);
void AfficherTexte(char Chemin[40])
{
FILE *Fich;
char Anom[30];
char Afabricant[30];
char Afonction[100];
char Aprix[10];
int fermer=0,i,c;
CreerListeVide();
/* Ouverture pour lecture seule*/
if((Fich = fopen( Chemin, "r" )) == NULL )
printf("-----------------Le Fichier na pas pu etre ouvert-----------------\n" );
else
{
printf("----------------------Le Fichier est ouvert-----------------------\n" );
/* Lecture */
if((c=fgetc(Fich)) != EOF)
{
printf("\n------------------------CONTENU DU FICHIER------------------------\n" );
while((c=fgetc(Fich)) != EOF)// si on est pas arrivé a la fin
{
fgets(Anom,sizeof(Anom), Fich);
fgets(Afabricant,sizeof(Afabricant), Fich);
fgets(Afonction,sizeof(Afonction), Fich);
fgets(Aprix,sizeof(Aprix), Fich);
liste_composant = InsertionEnAd(Anom,Afabricant,Afonction,Aprix,liste_composant);
}
}
else
{
printf("\n !LE FICHIER EST VIDE! \n");
}
/* Fermeture */
fermer = _fcloseall();
fclose(Fich);
if(fermer>0)
printf("\n--------------------------Fichier Fermer--------------------------\n");
else
printf("\n------------------------Fichier NON Fermer------------------------\n");
}
Afficher(liste_composant);
//CreerListeVide();
}
void main(void)
{
int choix;
char Chemin[40];
char Snom[30];
char Sfabricant[30];
char Sfonction[100];
char Sprix[10];
CreerListeVide();
//permet d'affecter le chemin sur fichier .txt
printf("Saisir la source de votre fichier .txt , ex:(c:\\fichier.txt)\n");
gets(Chemin);
printf("%s",Chemin);
do{
printf("\n-----------------------------COMMANDES----------------------------\n");
printf("- 1 : Entrer un nouveau composant -\n");
printf("- 2 : Afficher le(s) composant(s) -\n");
printf("- 3 : Sauvegarder la liste de composant dans un fichier texte -\n");
printf("- 4 : Afficher tous les composants contenu dans le fichier texte -\n");
printf("- 5 : Clear list e(Utiliser aprés AFFICHER TS COMPOSANTS .txt -\n");
printf("- 6 : QUITTER -\n");
printf("------------------------------------------------------------------\n");
scanf("%d",&choix);
scanf("%*[^\n]"),getchar();
if(choix==1)
{
printf("\nSaisir Nom du composant\n");
gets(Snom);
printf("Saisir Fabricant du composant\n");
gets(Sfabricant);
printf("Saisir Fonction du composant\n");
gets(Sfonction);
printf("Saisir Prix du composant\n");
gets(Sprix);
//scanf("%f",&Sprix);
liste_composant = InsertionEnAd(Snom,Sfabricant,Sfonction,Sprix,liste_composant);
}
if(choix==2)
{
Afficher(liste_composant);
}
if(choix==3)
{
SauvegarderTexte(liste_composant,Chemin);
}
if(choix==4)
{
AfficherTexte(Chemin);
}
if(choix==5)
{
CreerListeVide();
}
}while(choix!=6);
} |