#include<stdio.h>
#include<stdlib.h>
#define p 3
void main()
{
while(1){
int i,k,j,f=p,shortheight=0,midheight=0,highheight=0;
float gao,max,shu,avg[p]={0};
int data[p]={0};
struct data
{
float height;
int cls;
}dataset[15];
FILE *fp = NULL;
fp=fopen("f:/knn/knn.csv","r");
if (fp == NULL)
{
printf("open the file is failed!\n");
}
for(i=0;i<p;i++)
{
data[i]=i;
}
for(i=0;i<15;i++)
{
fscanf(fp,"%f, %d",&dataset[i].height,&dataset[i].cls);
//printf("%f,%d\n",dataset[i].height,dataset[i].cls);
}
printf("please input person height!\n");
scanf("%f",&gao);
printf("\n");
for(j=0;j<p;j++)
{
avg[j]=dataset[j].height-gao;
if(avg[j]<0)
{
avg[j]=-avg[j];
}
}
for(i=p;i<15;i++)
{
for(j=0;j<p;j++)
{
if(avg[j]<0)
{
avg[j]=-avg[j];
}
if(j==0)
{
max=avg[0];
}
if(max<avg[j])
{
max=avg[j];
f=j;
}
}
shu=dataset[i].height-gao;
if(shu<0)
{
shu=-shu;
}
if(shu<max)
{
data[f]=i;
avg[f]=shu;
}
}
for(k=0;k<p;k++)
{
if(dataset[data[k]].cls==0)
{
shortheight++;
}
if(dataset[data[k]].cls==1)
{
midheight++;
}
if(dataset[data[k]].cls==2)
{
highheight++;
}
// printf("%d\n",dataset[data[k]].cls);
}
// printf("\n");
// printf("%d\n",shortheight);
// printf("%d\n",midheight);
// printf("%d\n",highheight);
if(shortheight>midheight && shortheight>highheight)
{
printf("the person height is short-height!\n");
}
if(midheight>shortheight && midheight>highheight)
{
printf("the person height is mid-height!\n");
}
if(highheight>shortheight && highheight>midheight)
{
printf("the person height is high-height!\n");
}
}
}