-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistePoint.c
executable file
·334 lines (311 loc) · 7.18 KB
/
listePoint.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*!
* \file liste.c
* \brief File containing all the function used on list.
* \author Payard - Laurencot
* \version pré-alpha
* \date 8 Octobre 2021
*/
#include "listePoint.h"
/**
* \fn Liste creerListe()
* \brief Function that create a list.
*
*
* \return l - l is a properly initialized list
*/
Liste creerListe(){
Liste l;
l.first = NULL;
return l;
}
/**
* \fn void detruireListe(Liste l)
* \brief Function that free all the part of a list.
* This function is very useful to avoid all memory leak while playing with
* list. After the use of this function, you can reuse or let the pointer on a
* list you sent here.
* \param l - l is the list we work on
* \return void
*/
void detruireListe(Liste l){
Maillon *old;
Maillon *new;
new = l.first;
while (new != NULL) {
old = new;
new = new->suiv;
free(old);
}
}
/**
* \fn void ajouteDebut(Liste *l, point n)
* \brief Function that put a value as the first value of the list
* \param l - l is the list we work on
* \param n - the value we want to add to the list
* \return void
*/
void ajouteDebut(Liste *l, point n){
Maillon *M = (Maillon *)malloc(sizeof(Maillon));
M->val.x = n.x;
M->val.y = n.y;
M->suiv = l->first;
l->first = M;
}
/**
* \fn void ajouteFin(Liste *l, void *n)
* \brief Function that put a value as the last value of the list
*
* \param l - l is the list we work on
* \param n - the value we want to add to the list
* \return void
*/
void ajouteFin(Liste *l, point n){
Maillon *M = (Maillon *)malloc(sizeof(Maillon));
Maillon *new;
M->val.x = n.x;
M->val.y = n.y;
M->suiv = NULL;
if(l->first == NULL){
l->first = M;
}else{
new = l->first;
while (new->suiv != NULL) {
new = new->suiv;
}
new->suiv = M;
}
}
/**
* \fn void afficheListe(Liste l)
* \brief Function that print the pointer on every element on the terminal.
* This function is mostly useful in debugging.
* \param l - l is the list we work on
* \return void
*/
void afficheListePoints(Liste l){
Maillon *new;
int nb = 0;
new = l.first;
if(new == NULL){
printf("La suite est vide.\n");
}else{
printf(" x | y\n");
printf("%d : %9.5f | %9.5f\n",nb, new->val.x, new->val.y);
new = new->suiv;
while(new != NULL) {
nb++;
printf("%d : %9.5f | %9.5f\n",nb, new->val.x, new->val.y);
new = new->suiv;
}
printf("\n");
}
}
/**
* \fn int ListLenght(Liste l)
* \brief Function that get the size of a list.
* Warning : This function as to read every element of the list, for big list it
* can be pretty slow.
* \param l - l is the list we work on
* \return result - result is the list length
*/
int ListLenght(Liste l){
Maillon *new;
int result = 0;
if(l.first != NULL){
new = l.first;
while (new != NULL) {
new = new->suiv;
result++;
}
}
return result;
}
/**
* \fn void supprDebut(Liste *l)
* \brief Function that remove the first element of the list if there is one.
*
*
* \param *l - l is the pointer of the list we work on
* \return void - l is a pointer, no need to return it
*/
void supprDebut(Liste *l){
Maillon *new;
if(l->first != NULL){
new = l->first;
l->first = new->suiv;
free(new);
}
}
/**
* \fn void supprFin(Liste *l)
* \brief Function that remove the last element of the list if there is one.
*
*
* \param *l - l is the pointer of the list we work on
* \return void - l is a pointer, no need to return it
*/
void supprFin(Liste *l){
Maillon *new;
if(l->first != NULL){
new = l->first;
while (new->suiv != NULL) {
new = new->suiv;
}
free(new->suiv);
new->suiv = NULL;
}
}
/**
* \fn void supprValeur(Liste *l, void *val)
* \brief Function that remove one element of the list if there is one with the
* same value as the one passed in parameter.
*
* \param val - val is the value we want to destroy in the list
* \param *l - l is the pointer of the list we work on
* \return void - l is a pointer, no need to return it
*/
void supprValeur(Liste *l, point val){
Maillon *new;
Maillon *old;
if(l->first != NULL){
if((l->first->val.x == val.x) && (l->first->val.y == val.y)){
supprDebut(l);
}else{
new = l->first;
while ((new->suiv != NULL) && ((new->suiv->val.x != val.x) || (new->suiv->val.y != val.y))) {
new = new->suiv;
}
if(new->suiv != NULL){
old = new->suiv->suiv;
free(new->suiv);
new->suiv = old;
}
}
}
}
/**
* \fn void supprMaillon(Liste *l, int nbMaillon)
* \brief Function that remove the element of the list if there is one that as a
* number equal to nbMaillon, with value 0 for the first.
*
*
* \param *l - l is the pointer of the list we work on
* \param nbMaillon - nbMaillon is the number of the maillon we want to destroy
* \return void - l is a pointer, no need to return it
*/
void supprMaillon(Liste *l, int nbMaillon){
Maillon *new;
Maillon *old;
if(nbMaillon == 0){
supprDebut(l);
}else{
new = l->first;
for(int i = 0; i < nbMaillon-1; i++){
new = new->suiv;
if(new == NULL){
break;
}
}
if(new != NULL){
old = new->suiv->suiv;
free(new->suiv);
new->suiv = old;
}
}
}
float **ListeToTabsPoints(Liste l)
{
int n = ListLenght(l);
float **res = (float **)malloc(2*sizeof(float *));
res[0] = (float *)malloc(n*sizeof(float));
res[1] = (float *)malloc(n*sizeof(float));
Maillon *m = l.first;
for(int i = 0; i < n; i++){
res[0][i] = m->val.x;
res[1][i] = m->val.y;
m = m->suiv;
}
return res;
}
void ViderListe(Liste *l)
{
Maillon *old;
Maillon *new;
new = l->first;
while (new != NULL)
{
old = new;
new = new->suiv;
free(old);
}
l->first = NULL;
}
double moyXY(float **Res, int nbPoints)
{
float CA = 0;
for (int i = 0; i < nbPoints; i++)
{
CA += Res[0][i] * Res[1][i];
}
return CA / nbPoints;
}
double moyX(float **Res, int nbPoints)
{
double CA = 0;
for(int i = 0; i < nbPoints; i++){
CA += Res[0][i];
}
return CA/nbPoints;
}
double moyY(float **Res, int nbPoints){
double CA = 0;
for(int i = 0; i < nbPoints; i++){
CA += Res[1][i];
}
return CA/nbPoints;
}
double moyXmoyY(float **Res, int nbPoints){
double CA = 0;
double CI = 0;
for(int i = 0; i < nbPoints; i++){
CA += Res[0][i];
CI += Res[1][i];
}
return CA/nbPoints * CI/nbPoints;
}
double moyXcar(float **Res, int nbPoints){
double CA = 0;
for(int i = 0; i < nbPoints; i++){
CA += Res[0][i] * Res[0][i];
}
return CA/nbPoints;
}
double carmoyX(float **Res, int nbPoints){
double CA = 0;
for(int i = 0; i < nbPoints; i++){
CA += Res[0][i];
}
long double y = CA/nbPoints;
return y*y;
}
float **lnListetotab(float **Res, int nbPoints)
{
float **Sol = (float **)malloc(sizeof(float *) * 2);
Sol[0] = (float *)malloc(sizeof(float) * nbPoints);
Sol[1] = (float *)malloc(sizeof(float) * nbPoints);
for (int i = 0; i < nbPoints; i++)
{
Sol[0][i] = log(Res[0][i]);
Sol[1][i] = log(Res[1][i]);
}
return Sol;
}
double moyXY2tab(float **Res, float **LnRes, int nbPoints)
{
double CA = 0;
for (int i = 0; i < nbPoints; i++)
{
CA += Res[0][i] * LnRes[1][i];
}
return CA / nbPoints;
}