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
| #include <stdlib.h>
#include <stdio.h>
struct arc* lectureFichier(FILE* fichier);
struct arc* recherchePlacement (struct arc* arcCourant, int poid);
void insererArc (struct arc* sommets [], int sommetDepart, int sommetArrive, int poid);
void initTableau (struct arc* sommets [], int nbrSommets);
typedef struct arc { //structure servant de liste pour mettre les arcs
int sommetArrive;
int poid;
struct arc* arcSuivant;
};
struct arc* lectureFichier(FILE* fichier){
int nbrSommets, nbrArcs, sommet1, sommet2, poid, i;
struct arc *qdfv;
fscanf (fichier, "%d", &nbrSommets);
struct arc* sommets [nbrSommets]; //créé le tableau de sommets
initTableau (sommets, nbrSommets); //initialise les listes d'arcs à NULL
fseek(fichier, 1, SEEK_CUR);
fscanf (fichier, "%d", &nbrArcs);
for (i=0 ; i<nbrArcs ; i++) {
printf ("Je passe dans la boucle, et i=%d;\n",i);
fseek(fichier, 1, SEEK_CUR); //permet de passer à la ligne suivante
fscanf (fichier, "%d %d %d", &sommet1, &sommet2, &poid); //permet de lire la ligne, et de récupperrer son contenue
insererArc (sommets, sommet1, sommet2, poid);
insererArc (sommets, sommet2, sommet1, poid);
}
//qdfv=sommets;
return qdfv;
}
void insererArc (struct arc* sommets [], int sommetDepart, int sommetArrive, int poid) {
struct arc nouvelArc, tampon;
struct arc* arcADeplacer;
printf ("Je passe dans insererArc, sommetDepart=%d, sommetArrive=%d, poid=%d.\n", sommetDepart, sommetArrive, poid);
if (sommets [sommetDepart-1]==NULL) { //Si c'est le premier arc ayant pour origine sommetDepart
printf ("Je passe dans le if.\n");
nouvelArc.sommetArrive=sommetArrive;
printf ("Le sommet d'arrivé a été mit.\n");
nouvelArc.poid=poid;
printf ("Le poid a été mit.\n");
nouvelArc.arcSuivant=NULL;
printf("L'arc suivant a été initialisé.\n");
sommets [sommetDepart-1]=nouvelArc;
printf("Le pointeur a été bougé.\n");
}
else {
printf ("Je passe dans le else.\n");
arcADeplacer=recherchePlacement (sommets [sommetDepart-1], poid);
nouvelArc.sommetArrive=sommetArrive;
nouvelArc.poid=poid;
nouvelArc.arcSuivant=arcADeplacer->arcSuivant;
*arcADeplacer->arcSuivant=nouvelArc;
}
}
struct arc* recherchePlacement (struct arc* arcCourant, int poid) {
struct arc* arcRetour;
if (arcCourant->poid>=poid) {
arcRetour=arcCourant;
}
else {
if (arcCourant->arcSuivant==NULL) {
arcRetour=NULL;
}
else {
arcRetour=recherchePlacement (arcCourant->arcSuivant, poid);
}
}
return arcRetour;
}
void initTableau (struct arc* sommets [], int nbrSommets) {
int i;
for (i=0; i<nbrSommets; i++) {
sommets [i]=NULL;
}
}
int main (int argc, char *argv[]) {
//Ouverture du fichier
FILE* fichier=NULL;
fichier=fopen (argv[1], "a+");
if (fichier != NULL){
lectureFichier (fichier);
//fermeture du fichier
fclose (fichier);
}
else {
printf ("Un problème est survenue lors de l'ouverture de fichier\n");
}
return 0;
} |
Partager