-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojekt_1.c
627 lines (571 loc) · 18 KB
/
projekt_1.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void print_data(long long id, int m_mod2, char m_mod1, char m_type1, char m_type2, double value, int time, int date)
{
printf("ID cislo mer. osoby: %lld\n", id);
printf("Mer. modul: %c%d\n", m_mod1, m_mod2);
printf("Typ mer. veliciny: %c%c\n", m_type1, m_type2);
printf("Hodnota: %f\n", value);
printf("Cas merania: %04d\n", time);
printf("Datum: %d\n\n", date);
}
void scan_data(FILE **fr)
{
long long id;
int time, date, m_mod2;
double value;
char m_mod1, m_type[2];
while (fscanf(*fr, "%lld", &id) != EOF)
{
fscanf(*fr, "%lld\n", &id);
fscanf(*fr, "%c%d\n", &m_mod1, &m_mod2);
fscanf(*fr, "%c%c\n", &m_type[0], &m_type[1]);
fscanf(*fr, "%lf\n", &value);
fscanf(*fr, "%d\n", &time);
fscanf(*fr, "%d\n\n", &date);
print_data(id, m_mod2, m_mod1, m_type[0], m_type[1], value, time, date);
}
rewind(*fr);
}
void v_command(FILE **fr, int *v, int n, long long *id, int *m_mod2, char *m_mod1, char *m_type, double *value, int *time, int *date, int record_count)
{
if (*v)
{
if (n)
{
for (int i = 0; i < record_count; i++)
{
print_data(*(id + i), *(m_mod2 + i), *(m_mod1 + i), *(m_type + i * 2), *(m_type + i * 2 + 1), *(value + i), *(time + i), *(date + i));
}
}
else
{
scan_data(fr);
}
}
else
{
if ((*fr = fopen("C:\\Users\\frede\\Desktop\\STU - FIIT\\Proceduralne programovanie\\Visual Studio Code\\Projekt 1\\dataloger.txt", "r")) == NULL)
{
printf("Neotvoreny subor.\n");
}
else
{
*v = 1;
scan_data(fr);
}
}
}
void sort_value_by_date(double *sort_value, int *sort_date, int *sort_time, int counter)
{
for (int i = 0; i < counter - 1; i++)
{
for (int j = 0; j < counter - i - 1; j++)
{
if (*(sort_date + j) > *(sort_date + j + 1))
{
double help2 = *(sort_value + j);
*(sort_value + j) = *(sort_value + j + 1);
*(sort_value + j + 1) = help2;
int help3 = *(sort_date + j);
*(sort_date + j) = *(sort_date + j + 1);
*(sort_date + j + 1) = help3;
int help4 = *(sort_time + j);
*(sort_time + j) = *(sort_time + j + 1);
*(sort_time + j + 1) = help4;
}
}
}
}
void s_command(int n, int *m_mod2, char *m_mod1, char *m_type, double *value, int *time, int *date, int record_count)
{
if (!n)
{
printf("Polia nie su vytvorene.\n");
}
else
{
int i_m_mod2;
char i_m_mod1, i_m_type[2];
scanf("%c%d %c%c", &i_m_mod1, &i_m_mod2, &i_m_type[0], &i_m_type[1]);
getchar();
double sort_value[record_count];
int sort_date[record_count], sort_time[record_count], counter = 0, exist = 0;
for (int i = 0; i < record_count; i++)
{
if (*(m_mod1 + i) == i_m_mod1 && *(m_mod2 + i) == i_m_mod2 && *(m_type + i * 2) == i_m_type[0] && *(m_type + i * 2 + 1) == i_m_type[1])
{
exist = 1;
sort_value[counter] = *(value + i);
sort_date[counter] = *(date + i);
sort_time[counter] = *(time + i);
counter++;
}
}
sort_value_by_date(sort_value, sort_date, sort_time, counter);
FILE *fw;
fw = fopen("C:\\Users\\frede\\Desktop\\STU - FIIT\\Proceduralne programovanie\\Visual Studio Code\\Projekt 1\\vystup_S.txt", "w");
for (int i = 0; i < counter; i++)
{
fprintf(fw, "%d%04d\t%f\n", *(sort_date + i), *(sort_time + i), *(sort_value + i));
}
fclose(fw);
if (exist)
{
printf("Pre dany vstup je vytvoreny txt subor.\n");
}
else
{
printf("Pre dany vstup neexistuju zaznamy.\n");
}
}
}
void sort_values(double *value, int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (*(value + j) > *(value + j + 1))
{
int help = *(value + j);
*(value + j) = *(value + j + 1);
*(value + j + 1) = help;
}
}
}
}
void h_command(int n, char *m_type, double *value, int record_count)
{
if (!n)
{
printf("Polia nie su vytvorene.\n");
}
else
{
double sorted_value[record_count];
char i_m_type[2];
scanf("%c%c", &i_m_type[0], &i_m_type[1]);
getchar();
int j = 0;
for (int i = 0; i < record_count; i++)
{
if (*(m_type + i * 2) == i_m_type[0] && *(m_type + i * 2 + 1) == i_m_type[1])
{
sorted_value[j] = *(value + i);
j++;
}
}
sort_values(sorted_value, j);
int counter = 0, int_start = 0, int_end = 5;
printf("\tAx\t pocetnost\n");
int i = 0;
while (i < j)
{
if (*(sorted_value + i) > int_start && *(sorted_value + i) <= int_end)
{
counter++;
i++;
}
else
{
if (counter != 0)
{
printf("(%3d.0 - %3d.0>\t\t%d\n", int_start, int_end, counter);
}
counter = 0;
int_start += 5;
int_end += 5;
}
}
printf("(%3d.0 - %3d.0>\t\t%d\n", int_start, int_end, counter);
}
}
void r_command(int n, int *time, int record_count)
{
if (!n)
{
printf("Polia nie su vytvorene.\n");
}
else
{
int time_table[24][60] = {0};
for (int i = 0; i < record_count; i++)
{
int s = *(time + i) % 100;
int h = *(time + i) /= 100;
time_table[h][s] = 1;
}
for (int i = 0; i < 24; i++)
{
int next = 0;
for (int j = 0; j < 60; j++)
{
if (time_table[i][j] != 0)
{
if (!next)
{
printf("%02d:", i);
printf("%02d", j);
next = 1;
}
else
{
printf(", %02d", j);
}
}
}
if (next)
{
printf("\n");
}
}
}
}
void n_command(FILE **fr, int *v, int *n, long long **id, int **m_mod2, char **m_mod1, char **m_type, double **value, int **time, int **date, int *record_count)
{
if (!*v)
{
printf("Neotvoreny subor.\n");
}
else
{
if (*n)
{
for (int i = 0; i < *record_count; i++)
{
*(*id + i) = 0;
}
free(*id);
*id = NULL;
free(*m_mod2);
*m_mod2 = NULL;
free(*m_mod1);
*m_mod1 = NULL;
free(*m_type);
*m_type = NULL;
free(*value);
*value = NULL;
free(*time);
*time = NULL;
free(*date);
*date = NULL;
*record_count = 0;
}
*n = 1;
char z;
while ((z = getc(*fr)) != EOF)
{
if (z == '\n')
(*record_count)++;
}
*record_count = ++(*record_count) / 7;
rewind(*fr);
*id = (long long *)malloc(*record_count * sizeof(long long));
*time = (int *)malloc(*record_count * sizeof(int));
*date = (int *)malloc(*record_count * sizeof(int));
*m_mod2 = (int *)malloc(*record_count * sizeof(int));
*value = (double *)malloc(*record_count * sizeof(double));
*m_mod1 = (char *)malloc(*record_count * sizeof(char));
*m_type = (char *)malloc(*record_count * 2 * sizeof(char));
if (*id == NULL || *time == NULL || *date == NULL || *m_mod2 == NULL || *value == NULL || *m_mod1 == NULL || *m_type == NULL)
{
printf("Pole sa nepodarilo vytvorit.\n");
exit(EXIT_FAILURE);
}
for (int i = 0; i < *record_count; i++)
{
fscanf(*fr, "%lld\n", (*id + i));
fscanf(*fr, "%c%d\n", (*m_mod1 + i), (*m_mod2 + i));
fscanf(*fr, "%c%c\n", (*m_type + i * 2), (*m_type + i * 2 + 1));
fscanf(*fr, "%lf\n", (*value + i));
fscanf(*fr, "%d\n", (*time + i));
fscanf(*fr, "%d\n\n", (*date + i));
}
rewind(*fr);
}
}
int check_type(char type1, char type2)
{
char type[9][2] = {"R1", "U1", "A1", "R2", "U2", "A2", "R4", "U4", "A4"};
for (int i = 0; i < 9; i++)
{
if (type1 == type[i][0] && type2 == type[i][1])
{
return 1;
}
}
return 0;
}
int check_time(int time)
{
int min = time % 100;
time /= 100;
if (min > 59)
{
return 0;
}
if (time > 23)
{
return 0;
}
return 1;
}
int check_date(int date)
{
if (date > 99999999 || date < 10000000)
{
return 0;
}
int day = date % 100;
if (day > 31)
{
return 0;
}
date /= 100;
int month = date % 100;
if (month > 12)
{
return 0;
}
return 1;
}
void c_command(FILE **fr, int v)
{
if (!v)
{
printf("Neotvoreny subor.\n");
}
else
{
long long id;
int time, date, m_mod2;
double value;
char m_mod1, m_type[2];
int correct = 1;
while (fscanf(*fr, "%lld", &id) != EOF)
{
fscanf(*fr, "%lld\n", &id);
fscanf(*fr, "%c%d\n", &m_mod1, &m_mod2);
fscanf(*fr, "%c%c\n", &m_type[0], &m_type[1]);
fscanf(*fr, "%lf\n", &value);
fscanf(*fr, "%d\n", &time);
fscanf(*fr, "%d\n\n", &date);
if (id % 11 != 0)
{
printf("Nekorektne zadany vstup: ID cislo mer. osoby.\n");
correct = 0;
}
else if (m_mod1 < 'A' || m_mod1 > 'Z' || m_mod2 < 0 || m_mod2 > 99)
{
printf("Nekorektne zadany vstup: Mer. modul.\n");
correct = 0;
}
else if (check_type(m_type[0], m_type[1]) == 0)
{
printf("Nekorektne zadany vstup: Typ mer. veliciny.\n");
correct = 0;
}
else if (check_time(time) == 0)
{
printf("Nekorektne zadany vstup: Cas merania.\n");
correct = 0;
}
else if (check_date(date) == 0)
{
printf("Nekorektne zadany vstup: Datum.\n");
correct = 0;
}
}
rewind(*fr);
if (correct)
{
printf("Data su korektne.\n");
}
}
}
void z_command(int n, long long **id, int **m_mod2, char **m_mod1, char **m_type, double **value, int **time, int **date, int *record_count)
{
if (n)
{
long long i_id;
scanf("%lld", &i_id);
getchar();
int new_count = 0;
for (int i = 0; i < *record_count; i++)
{
if (*(*id + i) != i_id)
{
*(*id + new_count) = *(*id + i);
*(*m_mod1 + new_count) = *(*m_mod1 + i);
*(*m_mod2 + new_count) = *(*m_mod2 + i);
*(*m_type + new_count * 2) = *(*m_type + i * 2);
*(*m_type + new_count * 2 + 1) = *(*m_type + i * 2 + 1);
*(*value + new_count) = *(*value + i);
*(*time + new_count) = *(*time + i);
*(*date + new_count) = *(*date + i);
new_count++;
}
}
*id = (long long *)realloc(*id, new_count * sizeof(long long));
*time = (int *)realloc(*time, new_count * sizeof(int));
*date = (int *)realloc(*date, new_count * sizeof(int));
*m_mod2 = (int *)realloc(*m_mod2, new_count * sizeof(int));
*value = (double *)realloc(*value, new_count * sizeof(double));
*m_mod1 = (char *)realloc(*m_mod1, new_count * sizeof(char));
*m_type = (char *)realloc(*m_type, new_count * 2 * sizeof(char));
printf("Vymazalo sa: %d zaznamov!\n", *record_count - new_count);
*record_count = new_count;
}
else
{
printf("Polia nie su vytvorene.\n");
}
}
void o_command(FILE **fr, int v, int n, int *m_mod2, char *m_mod1, char *m_type, double *value, int *time, int *date, int *record_count)
{
if (!v)
{
printf("Neotvoreny subor.\n");
}
else
{
int i_m_mod2;
char i_m_mod1, i_m_type[2];
scanf("%c%d %c%c", &i_m_mod1, &i_m_mod2, &i_m_type[0], &i_m_type[1]);
getchar();
if (n)
{
double sort_value[*record_count];
int sort_date[*record_count], sort_time[*record_count], counter = 0;
for (int i = 0; i < *record_count; i++)
{
if (*(m_mod1 + i) == i_m_mod1 && *(m_mod2 + i) == i_m_mod2 && *(m_type + i * 2) == i_m_type[0] && *(m_type + i * 2 + 1) == i_m_type[1])
{
sort_value[counter] = *(value + i);
sort_date[counter] = *(date + i);
sort_time[counter] = *(time + i);
counter++;
}
}
sort_value_by_date(sort_value, sort_date, sort_time, counter);
for (int i = 0; i < counter; i++)
{
printf("%c%d\t%c%c\t%d\t%04d\t%f\n", i_m_mod1, i_m_mod2, i_m_type[0], i_m_type[1], *(sort_date + i), *(sort_time + i), *(sort_value + i));
}
}
else
{
char z;
while ((z = getc(*fr)) != EOF)
{
if (z == '\n')
(*record_count)++;
}
*record_count = ++(*record_count) / 7;
rewind(*fr);
double sort_value[*record_count];
int sort_date[*record_count], sort_time[*record_count], counter = 0;
long long h_id;
int h_time, h_date, h_m_mod2;
double h_value;
char h_m_mod1, h_m_type[2];
while (fscanf(*fr, "%lld", &h_id) != EOF)
{
fscanf(*fr, "%lld\n", &h_id);
fscanf(*fr, "%c%d\n", &h_m_mod1, &h_m_mod2);
fscanf(*fr, "%c%c\n", &h_m_type[0], &h_m_type[1]);
fscanf(*fr, "%lf\n", &h_value);
fscanf(*fr, "%d\n", &h_time);
fscanf(*fr, "%d\n\n", &h_date);
if (h_m_mod1 == i_m_mod1 && h_m_mod2 == i_m_mod2 && h_m_type[0] == i_m_type[0] && h_m_type[1] == i_m_type[1])
{
sort_value[counter] = h_value;
sort_date[counter] = h_date;
sort_time[counter] = h_time;
counter++;
}
}
rewind(*fr);
sort_value_by_date(sort_value, sort_date, sort_time, counter);
for (int i = 0; i < counter; i++)
{
printf("%c%d\t%c%c\t%d\t%04d\t%f\n", i_m_mod1, i_m_mod2, i_m_type[0], i_m_type[1], *(sort_date + i), *(sort_time + i), *(sort_value + i));
}
}
}
}
int main()
{
char command;
int v = 0, n = 0, record_count = 0;
FILE *fr;
long long *id = NULL;
int *time = NULL, *date = NULL, *m_mod2 = NULL;
double *value = NULL;
char *m_mod1 = NULL, *m_type = NULL;
while (command != 'k')
{
printf("Input command: ");
command = getchar();
getchar();
switch (command)
{
case 'v':
v_command(&fr, &v, n, id, m_mod2, m_mod1, m_type, value, time, date, record_count);
break;
case 'o':
o_command(&fr, v, n, m_mod2, m_mod1, m_type, value, time, date, &record_count);
break;
case 'n':
n_command(&fr, &v, &n, &id, &m_mod2, &m_mod1, &m_type, &value, &time, &date, &record_count);
break;
case 'c':
c_command(&fr, v);
break;
case 's':
s_command(n, m_mod2, m_mod1, m_type, value, time, date, record_count);
break;
case 'h':
h_command(n, m_type, value, record_count);
break;
case 'r':
r_command(n, time, record_count);
break;
case 'z':
z_command(n, &id, &m_mod2, &m_mod1, &m_type, &value, &time, &date, &record_count);
break;
case 'k':
if (n)
{
for (int i = 0; i < record_count; i++)
{
*(id + i) = 0;
}
free(id);
id = NULL;
free(m_mod2);
m_mod2 = NULL;
free(m_mod1);
m_mod1 = NULL;
free(m_type);
m_type = NULL;
free(value);
value = NULL;
free(time);
time = NULL;
free(date);
date = NULL;
}
if (v)
{
fclose(fr);
}
break;
default:
break;
}
}
return 0;
}