eksemum před 3 roky
rodič
revize
6f6c019edc

+ 20 - 0
SORTER/Diagram.xaml

@@ -0,0 +1,20 @@
+<Window x:Class= "SORTER.Diagram"
+        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+        xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
+        xmlns:local="clr-namespace:SORTER"
+        mc:Ignorable="d"
+        Title="Diagram" Height="450" Width="800" Icon="/iconDiagram.ico">
+    <Grid>
+        <lvc:CartesianChart Series="{Binding SeriesCollection}" LegendLocation="Left">
+            <lvc:CartesianChart.AxisX>
+                <lvc:Axis Title="Алгоритмы сортировки" Labels="{Binding Labels}"></lvc:Axis>
+            </lvc:CartesianChart.AxisX>
+            <lvc:CartesianChart.AxisY>
+                <lvc:Axis Title="Затраченное время" LabelFormatter="{Binding Formatter}"></lvc:Axis>
+            </lvc:CartesianChart.AxisY>
+        </lvc:CartesianChart>
+    </Grid>
+</Window>

+ 48 - 0
SORTER/Diagram.xaml.cs

@@ -0,0 +1,48 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Shapes;
+using LiveCharts;
+using LiveCharts.Wpf;
+
+namespace SORTER
+{
+    /// <summary>
+    /// Логика взаимодействия для Diagram.xaml
+    /// </summary>
+    public partial class Diagram : Window
+    {
+        public Diagram(long linearTime, long minMaxTime, long bubleTime, long shuttleTime, long insertionTime)
+        {
+            InitializeComponent();
+
+            SeriesCollection = new SeriesCollection
+            {
+                new ColumnSeries
+                {
+                    Title = null,
+                    Values = new ChartValues<long> { linearTime, minMaxTime, bubleTime, shuttleTime, insertionTime }
+                }
+            };
+
+            Labels = new[] { "Линейная", "МинМакс", "Пузырьковая", "Челночная", "Вставки" };
+            Formatter = value => value.ToString("N");
+
+            DataContext = this;
+        }
+
+        public SeriesCollection SeriesCollection { get; set; }
+        public string[] Labels { get; set; }
+        public Func<double, string> Formatter { get; set; }
+    }
+}

+ 2 - 1
SORTER/MainWindow.xaml

@@ -5,7 +5,7 @@
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:local="clr-namespace:SORTER"
         mc:Ignorable="d"
-        Title="Sorter" Height="400" Width="400" ResizeMode="NoResize" Icon="/icon.png">
+        Title="Sorter" Height="400" Width="400" ResizeMode="NoResize" Icon="/icon.png" WindowStartupLocation="CenterScreen">
     <Grid>
         <Grid.ColumnDefinitions>
             <ColumnDefinition/>
@@ -18,6 +18,7 @@
         </Grid.RowDefinitions>
 
         <Button x:Name="SelectFile" Content="Выбрать файл..." Margin="30 50 50 5" Click="SelectFile_Click"/>
+        <Button x:Name="Diagram" Grid.Column="1" Content="Построить диаграмму" Margin="30 50 40 5" Click="Diagram_Click"/>
         <Button x:Name="SortButton" Content="Отсортировать" Grid.ColumnSpan="2" Grid.Row="1" Margin="30 20 30 20" Click="SortButton_Click"/>
 
         <Border Grid.Row="2" BorderBrush="Black" BorderThickness="1"  HorizontalAlignment="Center" Margin="30 12 18 26">

+ 106 - 30
SORTER/MainWindow.xaml.cs

@@ -1,6 +1,7 @@
 using Microsoft.Win32;
 using System;
 using System.Collections.Generic;
+using System.Diagnostics;
 using System.IO;
 using System.Linq;
 using System.Text;
@@ -24,15 +25,16 @@ namespace SORTER
     {
         int[] array;
         string path;
+        FileInfo file;
+        Stopwatch linearTime;
+        Stopwatch minMaxTime;
+        Stopwatch bubleTime;
+        Stopwatch shuttleTime;
+        Stopwatch insertionTime;
 
         public MainWindow()
         {
             InitializeComponent();
-            //linearSelectionSort(false);
-            //methodMinMax();
-            //BubbleSort();
-            //ShuttleSort();
-            //InsertionSort();
         }
 
 
@@ -50,9 +52,18 @@ namespace SORTER
                 arrayCopy[i] = array[i];
             }
 
+            string pathCopy = file.DirectoryName + @"\Линейная сортировка.txt";
+            if (!File.Exists(pathCopy))
+            {
+                File.Copy(path, pathCopy);
+            }
+
+            linearTime = new Stopwatch();
+            linearTime.Start();
+
             if (ordering == false)
             {
-                using (StreamWriter sw = new StreamWriter(path, false))
+                using (StreamWriter sw = new StreamWriter(pathCopy, false))
                 {
                     while (arraySorted.Count < 100)
                     {
@@ -66,14 +77,14 @@ namespace SORTER
                         }
                         arrayCopy[index] = 100;
                         arraySorted.Add(max);
-                        sw.Write(arraySorted[index] + " ");
+                        sw.Write(max + " ");
                         max = int.MinValue;
                     }
                 }
             }
             else
             {
-                using (StreamWriter sw = new StreamWriter(path, false))
+                using (StreamWriter sw = new StreamWriter(pathCopy, false))
                 {
                     while (arraySorted.Count < 100)
                     {
@@ -87,18 +98,23 @@ namespace SORTER
                         }
                         arrayCopy[index] = 100;
                         arraySorted.Add(min);
-                        sw.Write(arraySorted[min] + " ");
+                        sw.Write(min + " ");
                         min = int.MaxValue;
                     }
                 }
             }
 
+            linearTime.Stop();
+
             arraySorted.Clear();
         }
 
 
         void methodMinMax(bool? ordering)
         {
+            minMaxTime = new Stopwatch();
+            minMaxTime.Start();
+
             if(ordering == false)
             {
                 for (int i = 0; i < array.Length - 1; ++i)
@@ -140,8 +156,15 @@ namespace SORTER
 
             }
 
+            minMaxTime.Stop();
 
-            using (StreamWriter sw = new StreamWriter(path, false))
+            string pathCopy = file.DirectoryName + @"\МинМакс.txt";
+            if(!File.Exists(pathCopy))
+            {
+                File.Copy(path, pathCopy);
+            }
+
+            using (StreamWriter sw = new StreamWriter(pathCopy, false))
             {
                 for (int i = 0; i < array.Length; ++i)
                 {
@@ -155,6 +178,9 @@ namespace SORTER
         {
             int temp;
 
+            bubleTime = new Stopwatch();
+            bubleTime.Start();
+
             if (ordering == false)
             {
                 for (int i = 0; i < array.Length - 1; i++)
@@ -186,7 +212,15 @@ namespace SORTER
                 }
             }
 
-            using (StreamWriter sw = new StreamWriter(path, false))
+            bubleTime.Stop();
+
+            string pathCopy = file.DirectoryName + @"\Пузырьковая сортировка.txt";
+            if (!File.Exists(pathCopy))
+            {
+                File.Copy(path, pathCopy);
+            }
+
+            using (StreamWriter sw = new StreamWriter(pathCopy, false))
             {
                 for (int i = 0; i < array.Length; ++i)
                 {
@@ -201,6 +235,9 @@ namespace SORTER
 
             int temp;
 
+            shuttleTime = new Stopwatch();
+            shuttleTime.Start();
+
             if(ordering == false)
             {
                 for (int i = 1; i < array.Length; i++)
@@ -252,8 +289,15 @@ namespace SORTER
                 }
             }
 
+            shuttleTime.Stop();
+
+            string pathCopy = file.DirectoryName + @"\Челночная сортировка.txt";
+            if (!File.Exists(pathCopy))
+            {
+                File.Copy(path, pathCopy);
+            }
 
-            using (StreamWriter sw = new StreamWriter(path, false))
+            using (StreamWriter sw = new StreamWriter(pathCopy, false))
             {
                 for (int i = 0; i < array.Length; ++i)
                 {
@@ -268,6 +312,9 @@ namespace SORTER
 
             int index, temp;
 
+            insertionTime = new Stopwatch();
+            insertionTime.Start();
+
             if (ordering == false)
             {
                 for (int i = 0; i < array.Length; ++i)
@@ -305,7 +352,15 @@ namespace SORTER
                 }
             }
 
-            using (StreamWriter sw = new StreamWriter(path, false))
+            insertionTime.Stop();
+
+            string pathCopy = file.DirectoryName + @"\Сортировка вставки.txt";
+            if (!File.Exists(pathCopy))
+            {
+                File.Copy(path, pathCopy);
+            }
+
+            using (StreamWriter sw = new StreamWriter(pathCopy, false))
             {
                 for (int i = 0; i < array.Length; ++i)
                 {
@@ -316,25 +371,32 @@ namespace SORTER
 
         private void SortButton_Click(object sender, RoutedEventArgs e)
         {
-            if (LinearButton.IsChecked == true)
-            {
-                linearSelectionSort(Ascending.IsChecked);
-            }
-            else if (minMaxButton.IsChecked == true)
-            {
-                methodMinMax(Ascending.IsChecked);
-            }
-            else if (BubbleButton.IsChecked == true)
+            if(string.IsNullOrEmpty(path))
             {
-                BubbleSort(Ascending.IsChecked);
+                MessageBox.Show("Для сортировки нужно выбрать файл");
             }
-            else if (ShuttleButton.IsChecked == true)
-            {
-                ShuttleSort(Ascending.IsChecked);
-            }
-            else if (InsertionButton.IsChecked == true)
+            else
             {
-                InsertionSort(Ascending.IsChecked);
+                if (LinearButton.IsChecked == true)
+                {
+                    linearSelectionSort(Ascending.IsChecked);
+                }
+                else if (minMaxButton.IsChecked == true)
+                {
+                    methodMinMax(Ascending.IsChecked);
+                }
+                else if (BubbleButton.IsChecked == true)
+                {
+                    BubbleSort(Ascending.IsChecked);
+                }
+                else if (ShuttleButton.IsChecked == true)
+                {
+                    ShuttleSort(Ascending.IsChecked);
+                }
+                else if (InsertionButton.IsChecked == true)
+                {
+                    InsertionSort(Ascending.IsChecked);
+                }
             }
         }
 
@@ -345,10 +407,11 @@ namespace SORTER
             if (OPF.ShowDialog() == true)
             {
                 path = OPF.FileName;
+                file = new FileInfo(path);
 
                 using (StreamReader sr = new StreamReader(OPF.FileName))
                 {
-                    string str = sr.ReadToEnd();
+                    string str = sr.ReadToEnd().Trim();
                     string[] arrayStr = str.Split(" ");
 
                     array = new int[arrayStr.Length];
@@ -360,5 +423,18 @@ namespace SORTER
                 }
             }
         }
+
+        private void Diagram_Click(object sender, RoutedEventArgs e)
+        {
+            if (linearTime == null || minMaxTime == null || bubleTime == null || shuttleTime == null || insertionTime == null)
+            {
+                MessageBox.Show("Для того, чтобы построить диаграмму, нужно отсортировать массив всемя способами");
+            }
+            else
+            {
+                Diagram wnd = new Diagram(linearTime.ElapsedTicks, minMaxTime.ElapsedTicks, bubleTime.ElapsedTicks, shuttleTime.ElapsedTicks, insertionTime.ElapsedTicks);
+                wnd.Show();
+            }
+        }
     }
 }

+ 6 - 0
SORTER/SORTER.csproj

@@ -8,10 +8,16 @@
 
   <ItemGroup>
     <None Remove="icon.png" />
+    <None Remove="iconDiagram.ico" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <PackageReference Include="LiveCharts.Wpf" Version="0.9.7" />
   </ItemGroup>
 
   <ItemGroup>
     <Resource Include="icon.png" />
+    <Resource Include="iconDiagram.ico" />
   </ItemGroup>
 
 </Project>

binární
SORTER/iconDiagram.ico


+ 1 - 0
SORTER/Линейная сортировка.txt

@@ -0,0 +1 @@
+99 99 99 99 98 98 98 98 98 98 98 98 98 98 98 98 97 97 97 97 95 95 95 95 94 94 94 94 93 93 93 93 91 91 91 91 90 90 90 90 89 89 89 89 88 88 88 88 87 87 87 87 86 86 86 86 84 84 84 84 84 84 84 84 82 82 82 82 78 78 78 78 78 78 78 78 76 76 76 76 76 76 76 76 76 76 76 76 72 72 72 72 68 68 68 68 67 67 67 67 

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 1 - 0
SORTER/МинМакс.txt


Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 1 - 0
SORTER/Пузырьковая сортировка.txt


Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 1 - 0
SORTER/Сортировка вставки.txt


Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 1 - 0
SORTER/Челночная сортировка.txt


+ 0 - 0
SORTER/массив — копия.txt


Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 1 - 1
SORTER/массив.txt