Advertisement
Adik28

K średnich

Jan 25th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.73 KB | None | 0 0
  1. *********************************// FORMS //**********************
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Globalization;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13.  
  14. namespace Algorytm_KSrednich
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         public static string nazwa_pliku_z_wartosciami;
  19.         public static string nazwa_pliku_z_opisem_atr;
  20.         public static List<List<string>> probki = new List<List<string>>();
  21.         public static List<bool> czy_atr_symb = new List<bool>();
  22.         public static List<string> nazwy_atr = new List<string>();
  23.  
  24.         public Form1()
  25.         {
  26.             InitializeComponent();
  27.  
  28.         }
  29.  
  30.         private void button1_Click(object sender, EventArgs e)
  31.         {
  32.             richTextBox1.Clear();
  33.             chart1.ChartAreas.Clear();
  34.             System.Windows.Forms.DataVisualization.Charting.ChartArea przestrzen;
  35.             chart1.ChartAreas.Add("");
  36.             przestrzen = chart1.ChartAreas[0];
  37.  
  38.             chart1.Series.Clear();
  39.             chart1.Series.Add("Punkty K");
  40.             System.Windows.Forms.DataVisualization.Charting.Series seria_akt;
  41.             seria_akt = chart1.Series[0];
  42.             seria_akt.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
  43.  
  44.             chart1.Series.Add("Punkt");
  45.             System.Windows.Forms.DataVisualization.Charting.Series seria_akt2;
  46.             seria_akt2 = chart1.Series[1];
  47.             seria_akt2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
  48.             seria_akt2.Color = Color.Red;
  49.  
  50.  
  51.             List<KSrednieMetody.Punkt> punkty = new List<KSrednieMetody.Punkt>();
  52.  
  53.  
  54.             for (int i = 0; i <= probki.Count - 1; i++)
  55.             {
  56.  
  57.                 punkty.Add(new KSrednieMetody.Punkt(Convert.ToDouble(probki[i][0]), Convert.ToDouble(probki[i][1])));
  58.  
  59.             }
  60.  
  61.             foreach (var item in punkty)
  62.             {
  63.                 richTextBox1.Text += item.ToString() + "/n";
  64.             }
  65.  
  66.  
  67.             List<KSrednieMetody.PunktK> kpunkty = new List<KSrednieMetody.PunktK>();
  68.  
  69.             Random r = new Random();
  70.             int K1 = r.Next(1, 101);
  71.             int K2 = r.Next(1, 101);
  72.             int K3 = r.Next(1, 101);
  73.  
  74.  
  75.             kpunkty.Add(new KSrednieMetody.PunktK() { X1 = punkty[K1].X1, X2 = punkty[K1].X2, Grupa = 1 });
  76.             kpunkty.Add(new KSrednieMetody.PunktK() { X1 = punkty[K2].X1, X2 = punkty[K2].X2, Grupa = 2 });
  77.             kpunkty.Add(new KSrednieMetody.PunktK() { X1 = punkty[K3].X1, X2 = punkty[K3].X2, Grupa = 3 });
  78.  
  79.             for (int i = 0; i < kpunkty.Count; i++)
  80.             {
  81.                 seria_akt.Color = Color.Green;
  82.                 seria_akt.Points.AddXY(kpunkty[i].X1, kpunkty[i].X2);
  83.             }
  84.  
  85.  
  86.             //wyświetlenie wszystkich punktów
  87.             //for (int i = 0; i < punkty.Count; i++)
  88.             //{
  89.             //    seria_akt2.Points.AddXY(Convert.ToDouble(punkty[i].X1), Convert.ToDouble(punkty[i].X2));
  90.             //}
  91.  
  92.             int counter = 0;
  93.             KSrednieMetody.AlgoStart(ref punkty, ref kpunkty, 100);
  94.  
  95.             richTextBox1.Clear();
  96.             richTextBox1.Text = "";
  97.  
  98.  
  99.             chart1.Series.Add("gr. 1");
  100.             System.Windows.Forms.DataVisualization.Charting.Series seria_akt3;
  101.             seria_akt3 = chart1.Series[2];
  102.             seria_akt3.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
  103.             seria_akt3.Color = Color.Cyan;
  104.  
  105.             chart1.Series.Add("gr. 2");
  106.             System.Windows.Forms.DataVisualization.Charting.Series seria_akt4;
  107.             seria_akt4 = chart1.Series[3];
  108.             seria_akt4.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
  109.             seria_akt4.Color = Color.Orange;
  110.  
  111.             chart1.Series.Add("gr. 3");
  112.             System.Windows.Forms.DataVisualization.Charting.Series seria_akt5;
  113.             seria_akt5 = chart1.Series[4];
  114.             seria_akt5.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
  115.             seria_akt5.Color = Color.Pink;
  116.  
  117.             chart1.Series.Add("Punkty K po zmianie");
  118.             System.Windows.Forms.DataVisualization.Charting.Series seria_akt6;
  119.             seria_akt6 = chart1.Series[5];
  120.             seria_akt6.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
  121.             seria_akt6.Color = Color.Black;
  122.  
  123.  
  124.             foreach (var punkt in punkty)
  125.             {
  126.                 richTextBox1.Text += ("\tX1: " + punkt.X1 + " X2 " + punkt.X2 + "  Grupa " + punkt.Grupa + " \n");
  127.  
  128.                 if (punkt.Grupa == 1)
  129.                 {
  130.  
  131.                     seria_akt3.Points.AddXY(punkt.X1, punkt.X2);
  132.                 }
  133.  
  134.                 if (punkt.Grupa == 2)
  135.                 {
  136.                     seria_akt4.Points.AddXY(punkt.X1, punkt.X2);
  137.                 }
  138.  
  139.                 if (punkt.Grupa == 3)
  140.                 {
  141.                     seria_akt5.Points.AddXY(punkt.X1, punkt.X2);
  142.                 }
  143.             }
  144.  
  145.             richTextBox1.Text += ("\nK punkty: \n");
  146.  
  147.             foreach (var punktK in kpunkty)
  148.             {
  149.                 richTextBox1.Text += ("\tX1: " + punktK.X1 + " X2: " + punktK.X2 + "  Grupa " + punktK.Grupa + " \n");
  150.                 seria_akt6.Points.AddXY(punktK.X1, punktK.X2);
  151.             }
  152.  
  153.         }
  154.  
  155.         private void button2_Click(object sender, EventArgs e)
  156.         {
  157.             OpenFileDialog openFileOpisAtr = new OpenFileDialog();
  158.             openFileOpisAtr.Title = "Wczytywanie pliku z informacjami o atrybutach";
  159.             openFileOpisAtr.Filter = "TXT files|*.txt";
  160.  
  161.             if (openFileOpisAtr.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  162.             {
  163.                 nazwa_pliku_z_opisem_atr = openFileOpisAtr.FileName;
  164.                 rtb_WczytajInformacjeOAtrybutach.LoadFile(nazwa_pliku_z_opisem_atr, RichTextBoxStreamType.PlainText);
  165.             }
  166.             using (StreamReader reader1 = new StreamReader(nazwa_pliku_z_opisem_atr))
  167.             {
  168.                 try
  169.                 {
  170.  
  171.                     string line;
  172.                     string[] lineChars;
  173.  
  174.  
  175.                     while ((line = reader1.ReadLine()) != null)
  176.                     {
  177.  
  178.                         line = line.Replace("\t", " ");
  179.                         while (line.Contains("  ")) line = line.Replace("  ", " ");
  180.                         string linia = line.Trim();
  181.  
  182.                         Console.WriteLine(line);
  183.  
  184.                         if (linia != "")
  185.                         {
  186.                             lineChars = linia.Split(' ');
  187.                             probki.Add(lineChars.ToList());
  188.                             var x = "b";
  189.                             if (lineChars[0] != "")
  190.                             {
  191.                                 nazwy_atr.Add(lineChars[0]);
  192.                             }
  193.                             if (lineChars[1] == "s")
  194.                             {
  195.                                 czy_atr_symb.Add(true);
  196.                             }
  197.                             else czy_atr_symb.Add(false);
  198.                         }
  199.  
  200.                     }
  201.  
  202.  
  203.                 }
  204.                 catch (Exception)
  205.                 {
  206.                     MessageBox.Show("Błąd!");
  207.                 }
  208.             }
  209.         }
  210.  
  211.  
  212.     }
  213.  
  214. }
  215.  
  216.  
  217.  
  218. *********************************// KŚREDNIE METODY //**********************
  219.  
  220. using System;
  221. using System.Collections.Generic;
  222. using System.Linq;
  223. using System.Text;
  224. using System.Threading.Tasks;
  225. using Algorytm_KSrednich;
  226. using System.Runtime.Serialization.Formatters.Binary;
  227. using System.IO;
  228.  
  229.  
  230. namespace Algorytm_KSrednich
  231. {
  232.     class KSrednieMetody
  233.     {
  234.         [Serializable]
  235.         public class Punkt
  236.         {
  237.             public double X1 { get; set; }
  238.             public double X2 { get; set; }
  239.  
  240.             public int Grupa { get; set; }
  241.  
  242.  
  243.             public Punkt(double x1, double x2)
  244.             {
  245.                 this.X1 = x1;
  246.                 this.X2 = x2;
  247.             }
  248.            
  249.         }
  250.  
  251.         [Serializable]
  252.         public class PunktK
  253.         {
  254.             public double X1 { get; set; }
  255.             public double X2 { get; set; }
  256.  
  257.             public int Grupa { get; set; }
  258.         }
  259.  
  260.  
  261.         public static object GlebokaKopia(object obj)
  262.         {
  263.             object objResult = null;
  264.             using (MemoryStream ms = new MemoryStream())
  265.             {
  266.                 BinaryFormatter bf = new BinaryFormatter();
  267.                 bf.Serialize(ms, obj);
  268.                 ms.Position = 0;
  269.                 objResult = bf.Deserialize(ms);
  270.             }
  271.             return objResult;
  272.         }
  273.  
  274.         private static List<PunktK> memeory = new List<PunktK>();
  275.  
  276.         public static void ZapamietajOstatniePolozenieKPunktu(List<PunktK> kpunkty)
  277.         {
  278.             memeory = new List<PunktK>();
  279.             memeory = (List<PunktK>)GlebokaKopia(kpunkty);
  280.         }
  281.  
  282.         public static void ZerowanieGrup(ref List<Punkt> zbior)
  283.         {
  284.             foreach (var punkt2 in zbior)
  285.             {
  286.                 punkt2.Grupa = -1;
  287.             }
  288.         }
  289.         public static void NadanieGrupZbiorowi(ref List<Punkt> zbior, List<PunktK> kpunkty)
  290.         {
  291.             for (int i = 0; i < zbior.Count; i++)
  292.             {
  293.                 Punkt punkt2 = zbior[i];
  294.                 NadanieGrupy(ref punkt2, kpunkty);
  295.             }
  296.         }
  297.         public static double LiczenieOdleglosc(PunktK p1, Punkt p2)
  298.         {
  299.             var x = Math.Pow(p1.X1 - p2.X1, 2);
  300.             var x2 = Math.Pow(p1.X2 - p2.X2, 2);
  301.             double odleglosc = Math.Sqrt(x + x2);
  302.             return odleglosc;
  303.         }
  304.  
  305.         public static void NadanieGrupy(ref Punkt punkt, List<PunktK> kpunkty)
  306.         {
  307.             Dictionary<double, PunktK> d = new Dictionary<double, PunktK>();
  308.  
  309.             foreach (var kpunkt in kpunkty)
  310.             {
  311.                 double od = LiczenieOdleglosc(kpunkt, punkt);
  312.  
  313.                 while (d.ContainsKey(od))
  314.                 {
  315.                     od = od + 0.000001;
  316.                 }
  317.  
  318.                 d.Add(od, kpunkt);
  319.             }
  320.  
  321.             var g = d.First(k => k.Key == d.Keys.Min()).Value.Grupa;
  322.             punkt.Grupa = g;
  323.         }
  324.  
  325.  
  326.         public static void PrzesunieciePunktuK(List<Punkt> zbior, ref List<PunktK> kpunkty)
  327.         {
  328.             foreach (var kpunkt in kpunkty)
  329.             {
  330.                 List<Punkt> punkty = new List<Punkt>();
  331.  
  332.                 foreach (var punkt in zbior)
  333.                 {
  334.                     if (punkt.Grupa == kpunkt.Grupa)
  335.                     {
  336.                         punkty.Add(punkt);
  337.                     }
  338.                 }
  339.  
  340.                 double x1 = 0;
  341.                 double x2 = 0;
  342.  
  343.                 if (punkty.Count > 0)
  344.                 {
  345.                     x1 = punkty.Average(i => i.X1);
  346.                     x2 = punkty.Average(i => i.X2);
  347.  
  348.                     kpunkt.X1 = x1;
  349.                     kpunkt.X2 = x2;
  350.                 }
  351.             }
  352.         }
  353.         public static void AlgoStart(ref List<Punkt> punkty, ref List<PunktK> kpunkty, int licznikStop)
  354.         {
  355.             int licznik = 0;
  356.             while (licznik < licznikStop)
  357.             {
  358.                 ZapamietajOstatniePolozenieKPunktu(kpunkty);
  359.                 ZerowanieGrup(ref punkty);
  360.                 NadanieGrupZbiorowi(ref punkty, kpunkty);
  361.                 PrzesunieciePunktuK(punkty, ref kpunkty);
  362.  
  363.                 licznik++;
  364.             }
  365.         }
  366.     }
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement