-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliste.c
193 lines (178 loc) · 4.6 KB
/
liste.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <stdio.h>
#include <stdlib.h>
#include "truc.h"
#include "liste.h"
#include "abr.h"
#include <string.h>
// OK
Un_elem *inserer_liste_trie(Un_elem *liste, Un_truc *truc)
{
// In: liste chainée de type Un_elem, pointeur vers un truc
//Out: liste chainée avec truc inséré
// Si on n'insère rien, on renvoie liste
if(truc==NULL) return liste;
// On commence par créer Un_elem qui pourra être inséré dans la liste, et qui
// pointe vers la même adresse que truc
Un_elem *truc_insert = (Un_elem *)malloc(sizeof(Un_elem));
//On vérifie que le malloc a fonctionné
if (truc_insert == NULL) return liste;
truc_insert->truc = truc;
truc_insert->suiv =NULL;
// Si la liste est NULL ou que liste->truc est NULL on renvoie truc_insert :
// C'est le cas lorsque la liste n'est pas initialisée
if((liste==NULL)||(liste->truc == NULL)) {
return truc_insert;
}
// Cas 1 : truc_insert est à insérer au début de la liste
if(truc->user_val < liste->truc->user_val)
{
truc_insert->suiv = liste;
return truc_insert;
}
//Cas 2 : insertion à la fin ou au milieu de la liste
Un_elem *tmp = liste;
int condition = 0;
while(condition == 0) //T que user_val de truc < user_val de lélément courant de la liste
{
if (tmp->suiv !=NULL) {
if((truc->user_val) >= (tmp->suiv->truc->user_val))
{
tmp =tmp->suiv;
}
else
{
condition = 1;
}
}
else
{
condition = 1;
}
}
// Si user_val de truc > à tous les userval de la liste
// on l'insère à la fin de la liste
if(tmp->suiv == NULL)
{
tmp->suiv = truc_insert;
}
else
{
truc_insert->suiv = tmp->suiv;
tmp->suiv = truc_insert;
}
return liste;
}
// OK
Un_elem *lire_stations(char* nom_fichier) {
//In: nom du fichier contenant les stations et leurs coos GPS
//Out: liste contenant les stations du fichier
FILE *fic=fopen(nom_fichier, "r");
if (fic == NULL) {
printf("Erreur ouverture fichier\n");
return NULL;
}
Un_elem *liste = (Un_elem*) malloc(sizeof(Un_elem));
if (liste == NULL) {
printf("Erreur d'allocation mémoire liste");
return NULL;
}
Une_coord coord;
int lastchar;
char *nom= (char*)malloc(sizeof(char)*100);
while (feof(fic) == 0) {
Tdata data;
//Le dernier caractère du nom est \n, il faut donc le supprimer
fscanf(fic, "%f ; %f ; %100[^\n]",&(coord.lon), &(coord.lat), nom);
lastchar = (int) strlen(nom);
if (nom[lastchar] == '\n') {
nom[lastchar] = '\0';
}
data.sta.nb_con = 0;
data.sta.Tab_con = NULL;
data.sta.con_pcc = NULL;
Un_truc *truc=creer_truc(coord, STA, data, 0.0);
truc->data.sta.nom = (char *)malloc(100 * sizeof(char));
strcpy(truc->data.sta.nom, nom);
liste = inserer_liste_trie(liste, truc);
}
free(nom);
fclose(fic);
return liste;
}
void detruire_liste(Un_elem *liste)
{
if(liste==NULL) return;
Un_elem *tmp= liste->suiv;
free(liste);
detruire_liste(tmp);
}
void detruire_liste_et_truc(Un_elem *liste)
{
Un_elem *tmp = liste, *tmp2=NULL;
while (tmp != NULL)
{
detruire_truc(tmp->truc);
tmp2 = tmp;
tmp = tmp->suiv;
free(tmp2);
}
}
void limite_zones(Un_elem* liste, Une_coord* limite_no, Une_coord* limite_se) {
float lonmax, lonmin, latmax, latmin;
if (liste == NULL) {
return;
}
//Coos de la limite no: lonmax, latmin
//Coos de la limite se: lonmin, latmax
lonmax = liste->truc->coord.lon;
lonmin = lonmax;
latmax = liste->truc->coord.lat;
latmin = latmax;
while(liste !=NULL ) {
if(latmin > liste->truc->coord.lat) {
latmin = liste->truc->coord.lat;
}
if(latmax < liste->truc->coord.lat) {
latmax = liste->truc->coord.lat;
}
if (lonmin > liste->truc->coord.lon) {
lonmin = liste->truc->coord.lon;
}
if (lonmax > liste->truc->coord.lon) {
lonmax = liste->truc->coord.lon;
}
liste = liste->suiv;
}
limite_no->lat = latmin;
limite_no->lon = lonmax;
limite_se->lat = latmax;
limite_se->lon = lonmin;
}
// OK
void ecrire_liste(FILE *flux, Un_elem *liste) {
if (flux == NULL) {
printf("Erreur création de fichier\n");
return;
}
while (liste != NULL) {
fprintf(flux, "%f ; %f ; %s\n", liste->truc->coord.lon, liste->truc->coord.lat, liste->truc->data.sta.nom);
liste = liste->suiv;
}
}
// OK
Un_elem *inserer_deb_liste(Un_elem *liste, Un_truc *truc)
{
// In: liste de connexions, connexion (truc) à insérer
// Out: liste de connextions avec truc inséré
if (truc==NULL) return liste;
Un_elem* liste_insert = (Un_elem*) malloc(sizeof(Un_elem));
liste_insert->truc = truc;
liste_insert->suiv = NULL;
if (liste == NULL) return liste_insert;
Un_elem *tmp = liste;
while (tmp->suiv != NULL) {
tmp = tmp->suiv;
}
tmp->suiv = liste_insert;
return liste;
}