-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
80 lines (68 loc) · 2.18 KB
/
main.c
File metadata and controls
80 lines (68 loc) · 2.18 KB
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
#include <stdio.h>
#include <stdlib.h>
#include "library.h"
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
return 1;
}
FILE* file = fopen(argv[1], "r");
if (!file) {
perror("Erreur d'ouverture du fichier");
return FILE_NOT_FOUND;
}
if (nodes_quantity(file) == 0 || nodes_links_quantity(file) == 0) {
fclose(file);
fprintf(stderr, "Erreur : Format de fichier incorrect.\n");
return BAD_FILE_FORMAT;
}
printf("Nombre de noeud(s): %d\n", nodes_quantity(file));
printf("Nombre de lien(s): %d\n", nodes_links_quantity(file));
int start_node_id = node_start(file);
int end_node_id = node_end(file);
printf("Node de départ: %d\n", start_node_id);
printf("Node de fin: %d\n", end_node_id);
if (start_node_id == -1) {
fclose(file);
fprintf(stderr, "Erreur : Aucun nœud de départ trouvé.\n");
return NO_START_NODE;
}
if (end_node_id == -1) {
fclose(file);
fprintf(stderr, "Erreur : Aucun nœud de fin trouvé.\n");
return NO_END_NODE;
}
rewind(file);
Node** nodes = init_nodes(argv[1]);
if (!nodes) {
fclose(file);
return 1;
}
for (int i = 0; nodes[i] != NULL; i++) {
printf("Node id: %d\n", nodes[i]->id);
}
init_graph(nodes);
Node* start_node = get_node_by_id(nodes, start_node_id);
Node* end_node = get_node_by_id(nodes, end_node_id);
if (start_node && end_node) {
Node* visited[100] = { NULL };
int visited_count = 0;
printf("Chemins possibles : ");
display_nodes(start_node, visited, &visited_count);
printf("\n");
if (!is_visited(end_node, visited, visited_count)) {
fclose(file);
fprintf(stderr, "Erreur : Le nœud de fin n'a pas été trouvé dans les chemins possibles.\n");
return NO_VALID_PATH;
}
} else {
printf("Le nœud de départ n'a pas été trouvé.\n");
}
for (int i = 0; nodes[i] != NULL; i++) {
free(nodes[i]->links);
free(nodes[i]);
}
free(nodes);
fclose(file);
return 0;
}