#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <math.h>

struct iris{

 

    double sepal_length;

    double sepal_width;

    double petal_length;

    double petal_width;

    char class[30];

 

};

 

int main(int argc, char const *argv[]) {

 

  FILE *file_p;

  char *file_name = "iris.data.txt";

  char string[200];

  struct iris iris_data[350];

  struct iris iris_tmp;

  int count = 0;

 

  double setosa_sepal_length = 0;

  double setosa_sepal_width = 0;

  double setosa_petal_length = 0;

  double setosa_petal_width = 0;

 

  double setosa_sepal_length_pow = 0;

  double setosa_sepal_width_pow = 0;

  double setosa_petal_length_pow = 0;

  double setosa_petal_width_pow = 0;

 

//-----------------------------------------

 

  double versicolor_sepal_length = 0;

  double  versicolor_sepal_width = 0;

  double versicolor_petal_length = 0;

  double versicolor_petal_width = 0;

 

  double versicolor_sepal_length_pow = 0;

  double  versicolor_sepal_width_pow = 0;

  double versicolor_petal_length_pow = 0;

  double versicolor_petal_width_pow = 0;

 

//-----------------------------------------

 

 

  double virginica_sepal_length = 0;

  double virginica_sepal_width = 0;

  double virginica_petal_length = 0;

  double virginica_petal_width = 0;

 

 

  double virginica_sepal_length_pow = 0;

  double virginica_sepal_width_pow = 0;

  double virginica_petal_length_pow = 0;

  double virginica_petal_width_pow = 0;

 

 

//-----------------------------------------

 

  file_p = fopen( file_name , "r");

 

  if( file_p == NULL ){

 

      printf("%sは開けません\n", file_name);

 

      return 0;

    }

 

    for( int i = 0 ; fgets( string, 200, file_p) != NULL ; i++ ){

 

      iris_tmp.sepal_length = atof( strtok( string, ",\n"));

      iris_tmp.sepal_width = atof( strtok( NULL, ",\n"));

      iris_tmp.petal_length = atof( strtok( NULL, ",\n"));

      iris_tmp.petal_width = atof( strtok( NULL, ",\n"));

      strcpy( iris_tmp.class, strtok( NULL, ",\n"));

 

      iris_data[i] = iris_tmp;

 

      count++;

 

    }

 

    struct iris setosa;

    struct iris setosa_pow;

 

    struct iris versicolor;

    struct iris versicolor_pow;

 

    struct iris virginica;

    struct iris virginica_pow;

 

    for( int i = 0 ; i < count ; i++ ){

 

        iris_tmp = iris_data[i];

 

        if( strcmp ( iris_tmp.class, "Iris-setosa") == 0 ){

 

            setosa_sepal_length += iris_tmp.sepal_length;

            setosa_sepal_width += iris_tmp.sepal_width;

            setosa_petal_length += iris_tmp.petal_length;

            setosa_petal_width += iris_tmp.petal_width;

 

            setosa_sepal_length_pow += pow( iris_tmp.sepal_length, 2);

            setosa_sepal_width_pow += pow( iris_tmp.sepal_width, 2);

            setosa_petal_length_pow += pow(iris_tmp.petal_length, 2);

            setosa_petal_width_pow += pow ( iris_tmp.petal_width, 2);

 

        }

        else if( strcmp ( iris_tmp.class, "Iris-versicolor") == 0){

 

          versicolor_sepal_length += iris_tmp.sepal_length;

          versicolor_sepal_width += iris_tmp.sepal_width;

          versicolor_petal_length += iris_tmp.petal_length;

          versicolor_petal_width += iris_tmp.petal_width;

 

          versicolor_sepal_length_pow += pow(iris_tmp.sepal_length, 2);

          versicolor_sepal_width_pow += pow( iris_tmp.sepal_width, 2);

          versicolor_petal_length_pow += pow( iris_tmp.petal_length, 2);

          versicolor_petal_width_pow += pow( iris_tmp.petal_width, 2);

 

        }

        else if( strcmp ( iris_tmp.class, "Iris-virginica") == 0){

 

          virginica_sepal_length += iris_tmp.sepal_length;

          virginica_sepal_width += iris_tmp.sepal_width;

          virginica_petal_length += iris_tmp.petal_length;

          virginica_petal_width += iris_tmp.petal_width;

 

          virginica_sepal_length_pow += pow( iris_tmp.sepal_length, 2);

          virginica_sepal_width_pow += pow(iris_tmp.sepal_width, 2);

          virginica_petal_length_pow += pow( iris_tmp.petal_length, 2);

          virginica_petal_width_pow += pow( iris_tmp.petal_width, 2);

 

        }

 

 

      }

 

      setosa.sepal_length = setosa_sepal_length / count;

      setosa.sepal_width = setosa_sepal_width / count;

      setosa.petal_length = setosa_petal_length / count;

      setosa.petal_width = setosa_petal_width / count;

 

      setosa_pow.sepal_length = setosa_sepal_length_pow / count;

      setosa_pow.sepal_width = setosa_sepal_width_pow / count;

      setosa_pow.petal_length = setosa_petal_length_pow / count;

      setosa_pow.petal_width = setosa_petal_width_pow / count;

 

//-------------------------------------------------------------------

 

      versicolor.sepal_length = versicolor_sepal_length / count;

      versicolor.sepal_width = versicolor_sepal_width / count;

      versicolor.petal_length = versicolor_petal_length / count;

      versicolor.petal_width = versicolor_petal_width / count;

 

      versicolor_pow.sepal_length = versicolor_sepal_length_pow / count;

      versicolor_pow.sepal_width = versicolor_sepal_width_pow / count;

      versicolor_pow.petal_length = versicolor_petal_length_pow / count;

      versicolor_pow.petal_width = versicolor_petal_width_pow / count;

 

//---------------------------------------------------------------------

 

      virginica.sepal_length = virginica_sepal_length / count;

      virginica.sepal_width = virginica_sepal_width / count;

      virginica.petal_length = virginica_petal_length / count;

      virginica.petal_width = virginica_petal_width / count;

 

      virginica_pow.sepal_length = virginica_sepal_length_pow / count;

      virginica_pow.sepal_width = virginica_sepal_width_pow / count;

      virginica_pow.petal_length = virginica_petal_length_pow / count;

      virginica_pow.petal_width = virginica_petal_width_pow / count;

 

//----------------------------------------------------------------------

 

      printf("Iris-setosa(avg,sd)\n");

      printf("  sepal_length : (%f,%f)\n", setosa.sepal_length, setosa_pow.sepal_length - pow(setosa.sepal_length, 2));

      printf("  sepal_width : (%f,%f)\n", setosa.sepal_width, setosa_pow.sepal_width - pow(setosa.sepal_width, 2));

      printf("  petal_length : (%f,%f)\n", setosa.petal_length, setosa_pow.petal_length - pow(setosa.petal_length, 2));

      printf("  petal_width : (%f,%f)\n", setosa.petal_width, setosa_pow.petal_width - pow(setosa.petal_width, 2));

 

      printf("Iris-versicolor(avg,sd)\n");

      printf("  sepal_length : (%f,%f)\n", versicolor.sepal_length, versicolor_pow.sepal_length - pow ( versicolor.sepal_length, 2));

      printf("  sepal_width : (%f,%f)\n", versicolor.sepal_width, versicolor_pow.sepal_width- pow ( versicolor.sepal_width, 2));

      printf("  petal_length : (%f,%f)\n", versicolor.petal_length, versicolor_pow.petal_length- pow ( versicolor.petal_length, 2));

      printf("  petal_width : (%f,%f)\n", versicolor.petal_width, versicolor_pow.petal_width- pow ( versicolor.petal_width, 2));

 

      printf("Iris-virginica(avg,sd)\n");

      printf("  sepal_length : (%f,%f)\n", virginica.sepal_length, virginica_pow.sepal_length - pow( virginica.sepal_length, 2));

      printf("  sepal_width : (%f,%f)\n", virginica.sepal_width, virginica_pow.sepal_width - pow( virginica.sepal_width, 2));

      printf("  petal_length : (%f,%f)\n", virginica.petal_length, virginica_pow.petal_length - pow( virginica.petal_length, 2));

      printf("  petal_width : (%f,%f)\n", virginica.petal_width, virginica_pow.petal_width - pow( virginica.petal_width, 2));

 

 

    fclose ( file_p );

 

    return 0;

}