Skip to content
Snippets Groups Projects
Aggregate.cpp 4.94 KiB
Newer Older
Lukas Rapant's avatar
Lukas Rapant committed
#ifdef __CYGWIN__
#undef __STRICT_ANSI__
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

struct my_record {
    char *time;
    int session;
    int speed;
    int gid;
};

struct road_network {
    int gid;
    int type;
    int speed;
};

struct aggregates {
    int gid;
    double speed [100];
    int num;
    int sess [100];

};

float harmonicMean(double arr[], int n) {
    // Declare sum variables and initialize
    // with zero.
    double sum = 0;
    for (int i = 0; i < n; i++)
        sum = sum + (double) 1 / arr[i];

    return (double) n / sum;
}

int unique_elements(int arr[], int len) {

    int counted[len], j, n, count, flag;

    counted[0] = arr[0];

    count = 1; /*one element is counted*/

    for (j = 0; j <= len - 1; ++j) {
        flag = 1;
        ;
        /*the counted array will always have 'count' elements*/
        for (n = 0; n < count; ++n) {
            if (arr[j] == counted[n]) {
                flag = 0;
            }
        }
        if (flag == 1) {
            ++count;
            counted[count - 1] = arr[j];
        }
    }
    return count;
}

double r2()
{
    return (double)rand() / (double)RAND_MAX ;
}
double r3()
{
    return ((double)rand() / (double)RAND_MAX )*90;
}
int main(int argc, char *argv[]) {
   char mystring [100];
   char mystring1 [100];

   char *str;
   char *str1;
   printf("%s\n",argv[1]);
   printf("%s\n",argv[2]);

    FILE* my_file = fopen(argv[1], "r");
     if (my_file == NULL) {
        perror(argv[1]);
        exit(1);
    }
    struct my_record records[10000];
    size_t count = 0;
    while (fgets(mystring, 100, my_file)!= NULL) {
        str = strtok(mystring, ";");
        records[count].time = str;
        str = strtok(NULL, ";");
        records[count].gid = atoi(str);
        str = strtok(NULL, ";");
        records[count].session = atoi(str);
        str = strtok(NULL, ";");
        records[count].speed = atoi(str);
        str = strtok(NULL, ";");

        ++count;
       

    }
    

   
    fclose(my_file);

    FILE* my_file1 = fopen(argv[2], "r");
         if (my_file == NULL) {
        perror(argv[1]);
        exit(1);
    }
    struct road_network Trnava[30];
    size_t count1 = 0;
    while (fgets(mystring1, 100, my_file1)!= NULL) {
        str1 = strtok(mystring1, ";");;
        Trnava[count1].gid = atoi(str1);
        str1 = strtok(NULL, ";");
        Trnava[count1].type = atoi(str1);
        str1 = strtok(NULL, ";");
        Trnava[count1].speed = atoi(str1);
        str1 = strtok(NULL, ";");
        ++count1;
       
    }
    
   
    fclose(my_file1);



    struct aggregates Agg [30];

    for (int i = 0; i<count1; i++) {
        int temp = 0;
        Agg[i].gid = Trnava[i].gid;
        for (int j = 0; j<count; j++) {
            if (records[j].gid == Trnava[i].gid) {
                Agg[i].speed[temp] = records[j].speed;               
                Agg[i].sess[temp] = records[j].session;
            }
            temp++;
        }
        Agg[i].num = temp;
    }
    double means [count1];
    int rel [count1];

    for (int i = 0; i<count1; i++) {
        if (Agg[i].speed[0] != 0) {
            means[i] = harmonicMean(Agg[i].speed, Agg[i].num);           
            int tt = unique_elements(Agg[i].sess, Agg[i].num);
            rel[i] = tt / ((1 / Trnava[i].type + 1)*100);
        } else {
            means[i] = -1;
            rel[i] = -1;
        }
    }
    
    

    FILE* my_file3 = fopen(argv[3], "w");
    if (my_file3 == NULL) {
        exit(1);
    }
    fprintf(my_file3, "%s;", records[0].time);
    fprintf(my_file3, "%f", rel[0]);
    for (int i = 1; i < count1; i++) {
        fprintf(my_file3, ";%f", rel[i]);
    }
    fprintf(my_file3, "\n");
    fclose(my_file3);


    FILE* my_file4 = fopen(argv[4], "w");
    if (my_file4 == NULL) {
        exit(201);
    }
    fprintf(my_file4,"%s;", records[0].time);
    fprintf(my_file4,"%f", means[0]);
    for (int i = 0; i<count1; i++) {
        fprintf(my_file4,";%f", means[i]);
    }
    fprintf(my_file4,"\n");
    fclose(my_file4);

    FILE* my_file5 = fopen(argv[5], "w");
    if (my_file5 == NULL) {
        exit(201);
    }
    fprintf(my_file5,"%d ", Trnava[0].gid);
    for (int i = 1; i<count1; i++) {
        fprintf(my_file5,";%d", Trnava[i].gid);
    }
    fprintf(my_file5,"\n");
    for (int j=0;j<2016;j++){
        fprintf(my_file5,"%f", r2);
        for (int i = 1; i<count1;i++) {
            fprintf(my_file5,";%f", r2);
        }
        fprintf(my_file5,"\n");

    }
    fclose(my_file5);

    FILE* my_file6 = fopen(argv[6], "w");
    if (my_file6== NULL) {
        exit(201);
    }
    fprintf(my_file6,"%d", Trnava[0].gid);
    for (int i = 1; i<count1; i++) {
        fprintf(my_file6,"%d;", Trnava[i].gid);
    }
    fprintf(my_file6,"\n");
    for (int j=0;j<2016;j++){
        fprintf(my_file6,"%f;", r3);
        for (int i = 0; i<count1;i++) {
            fprintf(my_file6,";%f", r3);
        }
        fprintf(my_file6,"\n");

    }
    fclose(my_file6);
    }