#include <stdio.h>

#include <math.h>

struct POINT{

double x;

double y; };

typedef struct POINT Point;

#define PI 3.14

double dist( Point p1, Point p2 );

int repeat( int, int, int, int nGan_tree3[][100] );

int repeat( int nGan, int nGrade, int nCount, int nGan_tree3[][100] )

{

int i;

for( i = 0; i < nCount; i++ )

{

if( nGan == nGan_tree3[nGrade][i] )

return 1;

}

return 0;

}

double dist( Point p1, Point p2 )

{

double distance;

float f;

f = (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y);

distance = sqrt( f );

return distance;

}

int main( )

{

FILE *file[6];

FILE *file1[6];

int num;

int *pClass;//shu zhong

double *pCircle;//fuan fu

Point *pTree;//zuo biao

int *pGrade;//deng ji

int nCount3[4];/* amount of trees in stage3*/

int nTree3[4][100]; /* number of trees in stage3*/

int nGan_tree3[4][100];

int nCount2[4];/* amount of trees in stage2*/

int nTree2[4][100]; /* number of trees in stage2*/

int nGan_tree2[4][100];

int nCount1[4];/* amount of trees in stage1*/

int nTree1[4][100]; /* number of trees in stage1*/

int nGan_tree1[4][100];

int i, m;

int j, k;

double *pPressure, r, r2, distance,temp;

for( i = 0; i < 4; i++ )

{

nCount1[i] = 0;

nCount2[i] = 0;

nCount3[i] = 0;

for( j = 0; j < 100; j++ )

{

nTree3[i][j] = 0;

nGan_tree3[i][j] = 0;

nTree2[i][j] = 0;

nGan_tree2[i][j] = 0;

nTree1[i][j] = 0;

nGan_tree1[i][j] = 0;

}

}

printf( "open file\n" );

file[0] = fopen( "//root//Project//ganranyali//fenlei//Sample1.txt", "r" );

if( NULL == file[0] )

{

printf( "fail to open file!\n" );

return 0;

}

file[1] = fopen( "//root//Project//ganranyali//fenlei//Sample2.txt", "r" );

if( NULL == file[1] )

{

printf( "fail to open file!\n" );

return 0;

}

file[2] = fopen( "//root//Project//ganranyali//fenlei//Sample3.txt", "r" );

if( NULL == file[2] )

{

printf( "fail to open file!\n" );

return 0;

}

file[3] = fopen( "//root//Project//ganranyali//fenlei//Sample4.txt", "r" );

if( NULL == file[3] )

{

printf( "fail to open file!\n" );

return 0;

}

file[4] = fopen( "//root//Project//ganranyali//fenlei//Sample5.txt", "r" );

if( NULL == file[4] )

{

printf( "fail to open file!\n" );

return 0;

}

file[5] = fopen( "//root//Project//ganranyali//fenlei//Sample6.txt", "r" );

if( NULL == file[5] )

{

printf( "fail to open file!\n" );

return 0;

}

/*------Export File------*/

file1[0] = fopen( "//root//Project//ganranyali//fenlei//fenlei1.txt", "w" );

if( NULL == file1[0] )

{

printf( "fail to open file!\n" );

return 0;

}

file1[1] = fopen( "//root//Project//ganranyali//fenlei//fenlei2.txt", "w" );

if( NULL == file1[1] )

{

printf( "fail to open file!\n" );

return 0;

}

file1[2] = fopen( "//root//Project//ganranyali//fenlei//fenlei3.txt", "w" );

if( NULL == file1[2] )

{

printf( "fail to open file!\n" );

return 0;

}

file1[3] = fopen( "//root//Project//ganranyali//fenlei//fenlei4.txt", "w" );

if( NULL == file1[3] )

{

printf( "fail to open file!\n" );

return 0;

}

file1[4] = fopen( "//root//Project//ganranyali//fenlei//fenlei5.txt", "w" );

if( NULL == file1[4] )

{

printf( "fail to open file!\n" );

return 0;

}

file1[5] = fopen( "//root//Project//ganranyali//fenlei//fenlei6.txt", "w" );

if( NULL == file1[5] )

{

printf( "fail to open file!\n" );

return 0;

}

/////////////////////

for( k = 0; k < 6; k++ )/* six transects were processed respectively*/

{

for( i = 0; i < 4; i++ )

{

nCount1[i] = 0;

nCount2[i] = 0;

nCount3[i] = 0;

for( j = 0; j < 100; j++ )

{

nTree3[i][j] = 0;

nGan_tree3[i][j] = 0;

nTree2[i][j] = 0;

nGan_tree2[i][j] = 0;

nTree1[i][j] = 0;

nGan_tree1[i][j] = 0;

}

}

printf( "alloc memory\n" );

fscanf( file[k], "%d\n", &num );

printf( "num = %d\n", num );

pClass = (int *)malloc( num * sizeof(int) );

pCircle = (double*)malloc( num * sizeof(double) );

pTree = (Point*)malloc( num*sizeof(Point) );

pGrade = (int*)malloc( num*sizeof(int) );

for( i = 0; i < num; i++ )

{

fscanf( file[k], "%d ", &pClass[i] );

fscanf( file[k], "%lf ", &temp );

pCircle[i] = temp;

fscanf( file[k], "%lf ", &temp );

pTree[i].x = temp ;

fscanf( file[k], "%lf ", &temp);

pTree[i].y = temp;

fscanf( file[k], "%d\n", &pGrade[i] );

}

fclose( file[k] );

printf( "start to class\n" );

for( i = 0; i < num; i++ )

{

r = pCircle[i] / 2;

if( pGrade[i] == 1 )/* this is a tree in stage1*/

{

for( j = 0; j < num; j++ )

{

if( j != i )

{

distance = dist(pTree[i], pTree[j]);

if( distance <= r )

{

if( pGrade[j] == 0 )

{

if( !repeat( j, 0, nCount1[0], nGan_tree1 ) )

{

nGan_tree1[0][nCount1[0]] = j;

nTree1[0][nCount1[0]] = pClass[j];

nCount1[0]++;

}

}

else if( pGrade[j] == 1 )

{

if( !repeat( j, 1, nCount1[1], nGan_tree1 ) )

{

nGan_tree1[1][nCount1[1]] = j;

nTree1[1][nCount1[1]] = pClass[j];

nCount1[1]++;

}

}

else if( pGrade[j] == 2 )

{

if( !repeat( j, 2, nCount1[2], nGan_tree1 ) )

{

nGan_tree1[2][nCount1[2]] = j;

nTree1[2][nCount1[2]] = pClass[j];

nCount1[2]++;

}

}

else if( pGrade[j] == 3 )

{

if( !repeat( j, 3, nCount1[3], nGan_tree1 ) )

{

nGan_tree1[3][nCount1[3]] = j;

nTree1[3][nCount1[3]] = pClass[j];

nCount1[3]++;

}

}

else

{

printf( "input a wring grade\n" );

}

}//end of distance

}//end of j

}//end of for j

}//end of if pGrade

else if( pGrade[i] == 2 )/* this is a tree in stage2*/

{

for( j = 0; j < num; j++ )

{

if( j != i )

{

distance = dist(pTree[i], pTree[j]);

if( distance <= r )

{

if( pGrade[j] == 0 )

{

if( !repeat( j, 0, nCount2[0], nGan_tree2 ) )

{

nGan_tree2[0][nCount2[0]] = j;

nTree2[0][nCount2[0]] = pClass[j];

nCount2[0]++;

}

}

else if( pGrade[j] == 1 )

{

if( !repeat( j, 1, nCount2[1], nGan_tree2 ) )

{

nGan_tree2[1][nCount2[1]] = j;

nTree2[1][nCount2[1]] = pClass[j];

nCount2[1]++;

}

}

else if( pGrade[j] == 2 )

{

if( !repeat( j, 2, nCount2[2], nGan_tree2 ) )

{

nGan_tree2[2][nCount2[2]] = j;

nTree2[2][nCount2[2]] = pClass[j];

nCount2[2]++;

}

}

else if( pGrade[j] == 3 )

{

if( !repeat( j, 3, nCount2[3], nGan_tree2 ) )

{

nGan_tree2[3][nCount2[3]] = j;

nTree2[3][nCount2[3]] = pClass[j];

nCount2[3]++;

}

}

else

{

printf( "input a wring grade\n" );

}

}//end of distance

}//end of j

}//end of for j

}//end of if pGrade

else if( pGrade[i] == 3 )/* this is a tree in stage3*/

{

for( j = 0; j < num; j++ )

{

if( j != i )

{

distance = dist(pTree[i], pTree[j]);

if( distance <= r )

{/*repeat( int nGan, int nGrade, int nCount, int nGan_tree3[4][] )*/

if( pGrade[j] == 0 )

{

if( !repeat( j, 0, nCount3[0], nGan_tree3 ) )

{

nGan_tree3[0][nCount3[0]] = j;

nTree3[0][nCount3[0]] = pClass[j];

nCount3[0]++;

}

}

else if( pGrade[j] == 1 )

{

if( !repeat( j, 1, nCount3[1], nGan_tree3 ) )

{

nGan_tree3[1][nCount3[1]] = j;

nTree3[1][nCount3[1]] = pClass[j];

nCount3[1]++;

}

}

else if( pGrade[j] == 2 )

{

if( !repeat( j, 2, nCount3[2], nGan_tree3 ) )

{

nGan_tree3[2][nCount3[2]] = j;

nTree3[2][nCount3[2]] = pClass[j];

nCount3[2]++;

}

}

else if( pGrade[j] == 3 )

{

if( !repeat( j, 3, nCount3[3], nGan_tree3 ) )

{

nGan_tree3[3][nCount3[3]] = j;

nTree3[3][nCount3[3]] = pClass[j];

nCount3[3]++;

}

}

else

{

printf( "input a wring grade\n" );

}

}//end of distance

}//end of j

}//end of for j

}//end of if pGrade

}//end of for i

//print result of fenlei

fprintf( file1[k], "take trees in stage3 as the center\n" );

fprintf( file1[k], "species in stage0:\n" );

for( i = 0; i < nCount3[0]; i++ )

{

fprintf( file1[k], "%d\t", nGan_tree3[0][i] + 1 );

fprintf( file1[k], "%d\n", nTree3[0][i] );

}

fprintf( file1[k], "species in stage1:\n" );

for( i = 0; i < nCount3[1]; i++ )

{

fprintf( file1[k], "%d\t", nGan_tree3[1][i] + 1 );

fprintf( file1[k], "%d\n", nTree3[1][i] );

}

fprintf( file1[k], "species in stage2:\n" );

for( i = 0; i < nCount3[2]; i++ )

{

fprintf( file1[k], "%d\t", nGan_tree3[2][i] + 1 );

fprintf( file1[k], "%d\n", nTree3[2][i] );

}

fprintf( file1[k], "species in stage3:\n" );

for( i = 0; i < nCount3[3]; i++ )

{

fprintf( file1[k], "%d\t", nGan_tree3[3][i] + 1 );

fprintf( file1[k], "%d\n", nTree3[3][i] );

}

fprintf( file1[k], "take trees in stage2 as the center\n" );

fprintf( file1[k], "species in stage0:\n" );

for( i = 0; i < nCount2[0]; i++ )

{

fprintf( file1[k], "%d\t", nGan_tree2[0][i] + 1 );

fprintf( file1[k], "%d\n", nTree2[0][i] );

}

fprintf( file1[k], "species in stage1:\n" );

for( i = 0; i < nCount2[1]; i++ )

{

fprintf( file1[k], "%d\t", nGan_tree2[1][i] + 1 );

fprintf( file1[k], "%d\n", nTree2[1][i] );

}

fprintf( file1[k], "species in stage2:\n" );

for( i = 0; i < nCount2[2]; i++ )

{

fprintf( file1[k], "%d\t", nGan_tree2[2][i] + 1 );

fprintf( file1[k], "%d\n", nTree2[2][i] );

}

fprintf( file1[k], "species in stage3:\n" );

for( i = 0; i < nCount2[3]; i++ )

{

fprintf( file1[k], "%d\t", nGan_tree2[3][i] + 1 );

fprintf( file1[k], "%d\n", nTree2[3][i] );

}

fprintf( file1[k], "take trees in stage1 as the center\n" );

fprintf( file1[k], "species in stage0:\n" );

for( i = 0; i < nCount1[0]; i++ )

{

fprintf( file1[k], "%d\t", nGan_tree1[0][i] + 1 );

fprintf( file1[k], "%d\n", nTree1[0][i] );

}

fprintf( file1[k], "species in stage1:\n" );

for( i = 0; i < nCount1[1]; i++ )

{

fprintf( file1[k], "%d\t", nGan_tree1[1][i] + 1 );

fprintf( file1[k], "%d\n", nTree1[1][i] );

}

fprintf( file1[k], "species in stage2:\n" );

for( i = 0; i < nCount1[2]; i++ )

{

fprintf( file1[k], "%d\t", nGan_tree1[2][i] + 1 );

fprintf( file1[k], "%d\n", nTree1[2][i] );

}

fprintf( file1[k], "species in stage3:\n" );

for( i = 0; i < nCount1[3]; i++ )

{

fprintf( file1[k], "%d\t", nGan_tree1[3][i] + 1 );

fprintf( file1[k], "%d\n", nTree1[3][i] );

}

fclose( file1[k] );

free( pClass );

free( pCircle );

free( pTree );

free( pGrade );

}

}