-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
169 lines (109 loc) · 4 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "graph.h"
#define MAX_BUFFER_SIZE 1000
// Fonction qui va lire les lignes du fichier : metros.txt
void lireLigne(Graph* graph, char* ligne)
{
char* debut;
char* fin;
char* station;
char* StationVu;
char* nomLigne;
char* temps_str;
int temps;
unsigned int taille;
debut = strchr(ligne, '(');
debut++; // 3) L1 :La Defense<->Esplanade de La Defense<->Pont de Neuilly<->Les Sablons<-> ...
fin = strchr(ligne, ')'); // ) L1 :La Defense<->Esplanade de La Defense<->Pont de Neuilly<->Les Sablons<->Porte Maillot ...
taille = fin - debut;// 1
temps_str = (char*)(malloc(sizeof(char) * 10));
temps_str[taille] = '\0';
strncpy(temps_str, debut, taille);
temps = atoi(temps_str);
debut = ++fin; // L1 :La Defense<->Esplanade de La Defense<->Pont de Neuilly<->Les Sablons<->Porte Maillot ...
fin = strchr(ligne, ':'); // :La Defense<->Esplanade de La Defense<->Pont de Neuilly<->Les Sablons<->Porte Maillot ...
taille = fin - debut; // 4
nomLigne = (char*)(malloc(sizeof(char) * 10));
strncpy(nomLigne, debut, taille);
nomLigne[taille] = '\0';
debut = ++fin; // La Defense<->Esplanade de La Defense<->Pont de Neuilly<->Les Sablons<->Porte Maillot ...
fin = strstr(debut, "<->"); // <->Esplanade de La Defense<->Pont de Neuilly<->Les Sablons<->Porte Maillot ...
taille = fin - debut;// 10
StationVu = NULL;
while (fin != NULL)
{
station = (char*)(malloc(sizeof(char) * 50));
strncpy(station, debut, taille);
station[taille] = '\0';
if (StationVu != NULL)
{
ajouterLien(graph, StationVu, station, nomLigne, temps);
}
debut = fin + 3;//Esplanade de La Defense<->Pont de Neuilly<->Les Sablons<->Porte Maillot ...
fin = strstr(debut, "<->"); //<->Pont de Neuilly ..
taille = fin - debut;// 23
if (fin == NULL)
{
taille = strlen(debut);
strncpy(station, debut, taille);
station[taille] = '\0';
ajouterLien(graph, StationVu, station, nomLigne, temps);
}
free(StationVu);
StationVu = (char *)(malloc(sizeof(char) * strlen(station) + 1));
strcpy(StationVu, station);
free(station);
}
free(StationVu);
free(temps_str);
free(nomLigne);
}
//Ouvrir fichier metros dans l'onglet
//Fonction qui permet d'entrer les stations
Station* lireStation(Graph * g)
{
char* tmp = (char*)(malloc(sizeof(char) * 50));
fgets(tmp, 50, stdin);
tmp[strcspn(tmp, "\r\n")] = 0;
int index = rechercherStationParNom(g, tmp);
while (index == -1)
{
printf("La station n'existe pas , veuillez reessayer\n");
fgets(tmp, 50, stdin);
tmp[strcspn(tmp, "\r\n")] = 0;
index = rechercherStationParNom(g, tmp);
}
free(tmp);
return g->stations[index];
}
int main(int argc, char * argv[]){
FILE* fichier;
Station* depart;
Station* arrivee;
fichier = fopen("metros.txt", "r");
if (fichier == NULL) {
printf("Erreur\n");
}
Graph* graph = initGraph();
// Lecture de metros.txt ligne/ligne
while (!feof(fichier))
{
char* buffer = (char*)(malloc(sizeof(char) * MAX_BUFFER_SIZE));
fgets(buffer, MAX_BUFFER_SIZE, fichier);
lireLigne(graph, buffer);
free(buffer);
}
printf("Veuillez entrer la station de depart\n");
depart = lireStation(graph);
printf("Veuillez entrer la station d'arrivee\n");
arrivee = lireStation(graph);
Station ** chemin = dijkstra(graph, depart, arrivee);
dijkstra_chemin(graph, depart, arrivee, chemin);
free(chemin);
nettoie_Graph(graph);
free(graph);
fclose(fichier);
return 0;
}