static void Main(string[] args) { string[] lines = System.IO.File.ReadAllLines(@"C:\Users\monitor\Desktop\KNN\iris.txt"); List <Flor> flores = new List <Flor>(); for (int i = 0; i < lines.Length; i++) { string[] aux; aux = lines[i].Split(','); Vector2 sepala = new Vector2(float.Parse(aux[0], new CultureInfo("en-US")), float.Parse(aux[1], new CultureInfo("en-US"))); Vector2 petala = new Vector2(float.Parse(aux[2], new CultureInfo("en-US")), float.Parse(aux[3], new CultureInfo("en-US"))); string type = aux[4]; flores.Add(new Flor(type, sepala, petala)); } do { double[] distancias = new double[flores.Count]; Vector2 sepala = new Vector2(); Vector2 petala = new Vector2(); Console.Write("Insira o comprimento da sepala: "); sepala.x = float.Parse(Console.ReadLine(), new CultureInfo("en-US")); Console.Write("Insira a largura da sepala: "); sepala.y = float.Parse(Console.ReadLine(), new CultureInfo("en-US")); Console.Write("Insira a comprimento da petala: "); petala.x = float.Parse(Console.ReadLine(), new CultureInfo("en-US")); Console.Write("Insira a largura da petala: "); petala.y = float.Parse(Console.ReadLine(), new CultureInfo("en-US")); Flor amostra = new Flor("", sepala, petala); for (int i = 0; i < distancias.Length; i++) { distancias[i] = amostra.Distance(flores[i]); } Console.WriteLine("Distancias:"); flores = TimSort.InsertionSort(distancias, flores); Console.Write("Insira o valor K: "); int K = int.Parse(Console.ReadLine()); Console.WriteLine("Maior probalidade da planta inserida ser uma " + ChooseType(flores, K)); Console.Write("Deseja repetir? 1-Sim / 0-Não "); } while (int.Parse(Console.ReadLine()) != 0); Console.ReadKey(); }
public double Distance(Flor target) { double x = Math.Pow(sepala.x - target.sepala.x, 2); double y = Math.Pow(this.sepala.y - target.sepala.y, 2); double x1 = Math.Pow(this.petala.x - target.petala.x, 2); double y1 = Math.Pow(this.petala.y - target.petala.y, 2); double distance = Math.Sqrt(x + y + x1 + y1); return(distance); }
public static List <Flor> InsertionSort(double[] vetor, List <Flor> flores) { for (var i = 1; i < vetor.Length; i++) { Flor aux1 = flores[i]; var aux = vetor[i]; var j = i - 1; while (j >= 0 && vetor[j] > aux) { vetor[j + 1] = vetor[j]; flores[j + 1] = flores[j]; j -= 1; } flores[j + 1] = aux1; vetor[j + 1] = aux; } return(flores); }