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
|
#include <stdio.h>
#include <stdlib.h>
#include <explode.c>
#define delim argv[2]
#define valuesfield argv[1]
#define vfdelim argv[3]
int main(int argc, char *argv[]) {
if(argc != 1+3) {
fprintf(stderr, "usage: %s valuesfield delim vfdelim\n", argv[0]);
return 1;
}
char **list;
size_t i, len;
size_t * counts;
char line[1337]; // yeah
int iter = 0, count;
double value;
double * values;
char * delimpointer;
char * nextdp;
fgets(line, 1337, stdin);
line[strcspn(line, "\n")] = 0;
while(1) {
explode(line, delim, &list, &len);
if (iter == 0) {
values = malloc(sizeof(double)*(len+1));
counts = malloc(sizeof(size_t)*(len+1));
for (i = 0; i <= len; i++) {
values[i] = 0;
counts[i] = 0;
}
}
if (iter != 0) // prvi je header
for (i = 0; i < len; i++) {
fprintf(stdout, "%s", list[i]);
if (i + 1 == len && i != atoi(valuesfield))
fprintf(stdout, "\n");
else
fprintf(stdout, ",");
if (i == atoi(valuesfield)) {
delimpointer = strtok(list[i], vfdelim);
value = 0;
count = 0;
while (delimpointer != NULL) {
value = value + atof(delimpointer);
count++;
delimpointer = strtok(NULL, vfdelim);
}
fprintf(stdout, "%lf", value/count);
values[i] = 0;
counts[i] = 1;
values[i+1] = values[i+1]/++(counts[i+1]);
if (i + 1 == len)
fprintf(stdout, "\n");
else
fprintf(stdout, ",");
} else {
if (i > atoi(valuesfield)) {
values[i+1] = values[i+1]/++(counts[i+1]);
} else {
values[i] = values[i]/counts[i];
}
}
}
/* free list */
for(i = 0; i < len; ++i) {
free(list[i]);
}
free(list);
fgets(line, 1337, stdin);
if(feof(stdin)) {
fprintf(stderr, "averages:\n");
for (i = 0; i <= len; i++) {
fprintf(stderr, "%lf", values[i]/counts[i]);
if (i + 1 == len)
fprintf(stderr, ",");
else
fprintf(stderr, "\n");
}
break;
}
// fprintf(stderr, "d: %s", line);
line[strcspn(line, "\n")] = 0;
iter++;
}
return 0;
}
|