diff options
Diffstat (limited to 'bio/vaje/1/programi/povpreci.c')
-rw-r--r-- | bio/vaje/1/programi/povpreci.c | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/bio/vaje/1/programi/povpreci.c b/bio/vaje/1/programi/povpreci.c new file mode 100644 index 0000000..6f3e7c1 --- /dev/null +++ b/bio/vaje/1/programi/povpreci.c @@ -0,0 +1,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; +} |