-
Notifications
You must be signed in to change notification settings - Fork 0
/
v0.c
471 lines (468 loc) · 10.8 KB
/
v0.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
//de testat tot!
//dar de testat pe bucati
typedef struct node //structura
{
struct node *prev;
struct node *next;
double timestamp;
double value;
}node, *DList;
void listinsert1(DList s,double val)
{
//functie ce insereaza intr-o lista valori la inceputul ei
//se presupune ca lista a fost deja initializata
node *newnode;
while(s->next!=NULL)
s=s->next;
newnode=(node*)malloc(sizeof(node));
newnode->value=val;
newnode->prev=s;
s->next=newnode;
newnode->next=NULL;
}
void listinsert2(DList s,double time,double val)
{
//functie ce insereaza intr-o lista valori la inceputul ei
//se presupune ca lista a fost deja initializata
node *newnode;
while(s->next!=NULL)
s=s->next;
newnode=(node*)malloc(sizeof(node));
newnode->timestamp=time;
newnode->value=val;
newnode->prev=s;
s->next=newnode;
newnode->next=NULL;
}
void listdel(DList s,double val)
{
//aici este o functie care doreste sa elimine un nod din lista, cu o anumita valoare
DList todel;
while(s->next!=NULL)
{
//se parcurge lista pana la final
if(s->next->value==val)
{
//daca s-a gasit un nod cu valoarea cautata
todel=s->next; //acesta se sterge cu ajutorul unei variabile ajutatoare
s->next=todel->next;
todel->next->prev=s;
todel->next=NULL;
todel->prev=NULL;
free(todel);
}
s=s->next; //se trece mai departe in cautarea unui nod care ar putea avea valoarea cautata
}
}
void listfree(DList s)
{
//eliberare memorie stergand toate nodurile dintr-o lista
DList todel;
todel=s;
while(todel->next!=NULL)
{
s->next=todel->next; //se sterge de fiecare data primul nod
todel->next->prev=NULL;
free(todel);
todel=s;
}
}
void listprint1(DList s)
{
//prima versiune de afisare a listei, asa cum este prezentata in 2.1
int numarnoduri;
numarnoduri=0;
node *cursor;
cursor=s;
while(cursor!=NULL)
{
cursor=cursor->next; //se parcurge lista cu ajutorul unui cursor
numarnoduri++; //se numara cate noduri sunt in lista si se afiseaza
}
numarnoduri--;
printf("%d\n",numarnoduri);
cursor=s;
cursor=cursor->next;
while(cursor!=NULL)
{
printf("%.2lf\n",cursor->value); //valorile se afiseaza pe ecran (stdout)
cursor=cursor->next;
}
}
void listprint(DList s)
{
//prima versiune de afisare a listei, asa cum este prezentata in 2.1
int numarnoduri;
numarnoduri=0;
node *cursor;
cursor=s;
while(cursor!=NULL)
{
cursor=cursor->next; //se parcurge lista cu ajutorul unui cursor
numarnoduri++; //se numara cate noduri sunt in lista si se afiseaza
}
numarnoduri--;
printf("%d\n",numarnoduri);
cursor=s;
cursor=cursor->next;
while(cursor!=NULL)
{
printf("%.0lf ",cursor->timestamp); //valorile se afiseaza pe ecran (stdout)
printf("%.2lf\n",cursor->value); //valorile se afiseaza pe ecran (stdout)
cursor=cursor->next;
}
}
void listprint2(DList s)
{
//o a doua varianta de afisare a listei, asa cum este aratat in 2.2
node *cursor;
cursor=s;
printf("[");
while(cursor!=NULL)
{
printf("%lf,",cursor->value); //valorile se afiseaza pe ecran (stdout)
cursor=cursor->next; //se parcurge lista cu ajutorul unui cursor
}
printf("]");
}
void listprint3(DList s)
{
//prima versiune de afisare a listei, asa cum este prezentata in 2.3
int numarnoduri;
numarnoduri=0;
node *cursor;
cursor=s;
while(cursor!=NULL)
{
cursor=cursor->next; //se parcurge lista cu ajutorul unui cursor
numarnoduri++; //se numara cate noduri sunt in lista si se afiseaza
}
printf("%d\n",numarnoduri);
cursor=s;
while(cursor!=NULL)
{
printf("%lf %lf\n",cursor->timestamp,cursor->value); //valorile se afiseaza pe ecran (stdout)
cursor=cursor->next;
}
}
void one(DList s)
{
//rezolvarea lui 2.1 -> e1
//mere
node *cursor;
node *elementinsine;
node *finish;
node *q;
DList start;
int contor;
int i,n;
double xm,x;
double num;
double sigma;
double elementinsinev;
double st,dr; //pentru a calcula mai usor intervalul in care sa se verifice apartenenta numarului
s=(DList)malloc(sizeof(node));
s->next=NULL;
s->prev=NULL;
scanf("%d",&n);
for (i=0;i<n;i++){
scanf("%lf",&x);
listinsert1(s,x);
}
q=s->next;
while (q->next->next->next->next!=NULL)
{
xm=q->value+q->next->value+q->next->next->value+q->next->next->next->value+q->next->next->next->next->value;
xm=xm/5;
sigma=(q->value-xm)*(q->value-xm)+(q->next->value-xm)*(q->next->value-xm)
+(q->next->next->value-xm)*(q->next->next->value-xm)+(q->next->next->next->value-xm)*(q->next->next->next->value-xm)
+(q->next->next->next->next->value-xm)*(q->next->next->next->next->value-xm);
sigma=sigma/5;
sigma=sqrt(sigma);
if(q->next->next->value<xm-sigma||q->next->next->value>xm+sigma)
listdel(s,q->next->next->value);
q=q->next;
}
listprint1(s); //afiseaza rezultatul
//listfree(s); //elibereaza memoria
}
void twoone(DList s)
{
node *q;
node *ceva;
DList newnewlist,newlist;
int k,n;
double x;
int ok;
node *i;
double aux;
s=(DList)malloc(sizeof(node));
s->next=NULL;
s->prev=NULL;
scanf("%d",&n);
for (k=0;k<n;k++){
scanf("%lf",&x);
listinsert1(s,x);
}
newlist=(DList)malloc(sizeof(node));
newlist->next=NULL;
newlist->prev=NULL;
double toadd;
//din nou ia cate cinci si sorteaza alea cate cinci si apoi ia mijlocul
q=s->next;
while (q->next->next->next->next!=NULL)
{
newnewlist=(DList)malloc(sizeof(node));
newnewlist->next=NULL;
newnewlist->prev=NULL;
listinsert1(newnewlist,q->value);
listinsert1(newnewlist,q->next->value);
listinsert1(newnewlist,q->next->next->value);
listinsert1(newnewlist,q->next->next->next->value);
listinsert1(newnewlist,q->next->next->next->next->value);
//pana aci stiu ca merge
do
{
ok = 1;
for(i = newnewlist->next ; i->next!=NULL ; i=i->next)
if(i->value > i->next->value)
{
aux = i->value;
i->value= i->next->value;
i->next->value = aux;
ok = 0;
}
}
while(!ok);
toadd=newnewlist->next->next->next->value;
listinsert1(newlist,toadd);
q=q->next;
//listfree(newnewlist);
}
listprint1(newlist);
//listfree(s);
//listfree(newlist);
}
void twotwo(DList s)
{
node *q;
node *ceva;
DList newnewlist,newlist;
int k,n;
double x;
double sum;
node *i;
double aux;
s=(DList)malloc(sizeof(node));
s->next=NULL;
s->prev=NULL;
scanf("%d",&n);
for (k=0;k<n;k++){
scanf("%lf",&x);
listinsert1(s,x);
}
newlist=(DList)malloc(sizeof(node));
newlist->next=NULL;
newlist->prev=NULL;
double toadd;
//din nou ia cate cinci si sorteaza alea cate cinci si apoi ia mijlocul
q=s->next;
while (q->next->next->next->next!=NULL)
{
newnewlist=(DList)malloc(sizeof(node));
newnewlist->next=NULL;
newnewlist->prev=NULL;
listinsert1(newnewlist,q->value);
listinsert1(newnewlist,q->next->value);
listinsert1(newnewlist,q->next->next->value);
listinsert1(newnewlist,q->next->next->next->value);
listinsert1(newnewlist,q->next->next->next->next->value);
//pana aci stiu ca merge
sum=0;
for (i=newnewlist->next;i!=NULL;i=i->next)
sum=sum+i->value;
sum=sum/5;
listinsert1(newlist,sum);
q=q->next;
//listfree(newnewlist);
}
listprint1(newlist);
//listfree(s);
//listfree(newlist);
}
void three(DList s)
{
node *cursor;
int k,n;
double x,y;
double tdiff; //diferenta de timp
s=(DList)malloc(sizeof(node));
s->next=NULL;
s->prev=NULL;
scanf("%d",&n);
for (k=0;k<n;k++){
scanf("%lf",&x);
scanf("%lf",&y);
listinsert2(s,x,y);
}
cursor=s->next->next;
while (cursor!=NULL)
{
printf("ok1\n");
tdiff=cursor->timestamp-cursor->prev->timestamp;
if(tdiff>=100||tdiff<=1000)
{
printf("ok2\n");
cursor->timestamp=(cursor->prev->timestamp+cursor->timestamp)/2;
cursor->value=(cursor->prev->value+cursor->value)/2;
}
cursor=cursor->next;
}
listprint(s);
//listfree(s);
}
/*
void four(DList s,double prag)
{
//de facut
node *cursor;
node *st,*dr;
node *lft,*rght;
double i;
double w;
double C;
double temp;
double sum1,sum2;
int j,l;
listinit(s);
listcreate2(s);
cursor=s;
while(cursor->next!=NULL)
{
if (cursor->next->timestamp-cursor->timestamp>prag*1000)
{
i=cursor->timestamp;
i=i+prag*1000/5;
while (i<=cursor->next->timestamp)
{
listinsert3(s,i-prag*1000/5,i);
st=s;
dr=s;
while(st->next->timestamp!=i)
st=st->next;
dr=st;
for (j=0;j<3;j++)
dr=dr->next;
C=(i-st->timestamp)/(dr->timestamp-st->timestamp);
lft=s;
rght=s;
while(rght!=NULL)
rght=rght->next;
for (j=0;j<3;j++)
rght=rght->prev;
for (j=0;j<3;j++)
{
w=(((j/(3-1))*(j/(3-1)))*0.9+0.1); //w(j,k)
temp=0;
for (l=j;l<=3;l++)
temp=temp+(((j/(3-1))*(j/(3-1)))*0.9+0.1);
w=w/temp;
for (l=j;l<=3;l++)
{
//parcurgere ptr lft si rght in fct de l ul din for
//w(i,k) in fct de l-ul din for
}
}
}
}
}
listprint3(s);
listfree(s);
}
*/
void five(DList s,double delta)
{
node *cursor;
double stop;
double first,last;
double nr;
int k,n;
double x,y;
cursor=(node*)malloc(1*sizeof(node));
s=(DList)malloc(sizeof(node));
s->next=NULL;
s->prev=NULL;
scanf("%d",&n);
for (k=0;k<n;k++){
scanf("%lf",&x);
scanf("%lf",&y);
listinsert2(s,x,y);
}
cursor=s->next;
while(cursor->next!=NULL)
{
cursor=cursor->next;
}
stop=cursor->timestamp;
first=0;
last=delta;
while(last<=stop)
{
cursor=s->next;
nr=0;
while (cursor!=NULL)
{
if (cursor->timestamp>=first&&cursor->timestamp<=last)
nr++;
cursor=cursor->next;
}
printf("[%.0lf,%.0lf] %.0lf\n",first,last,nr);
first=first+delta;
last=last+delta;
}
//listfree(s);
}
int main(int argc,char **argv)
{
int i,n;
//int j,k;
DList s;
//char num[4];
//double delta;
//double x;
// k=0; j=1;
/*for(i=1;i<argc;i++)
if(strcmp(argv[i],"--e1")==0)
one(s);
else
if(strcmp(argv[i],"--e2")==0)
twoone(s);
else
if(strcmp(argv[i],"--e3")==0)
twotwo(s);
else
if(strcmp(argv[i],"--u")==0)
three(s);
else
if(strcmp(argv[i],"--c")==0)
four(s);
else
if(strchr(argv[i],'s')==0)
{
num[k++]=s[j++];
for(j=0;j<strlen(num);j++)
delta=delta*10+num[j];
five(s,delta);
}
*/
five(s,250);
/*
listprint1(s);
*/
return 0;
}