eksemum 3 年之前
父节点
当前提交
6e0e0e7248
共有 5 个文件被更改,包括 76 次插入46 次删除
  1. 7 1
      SORTER.sln
  2. 26 45
      SORTER/MainWindow.xaml.cs
  3. 1 0
      SORTER/Линейная сортировка.txt
  4. 27 0
      TestSorter/TestSorter.csproj
  5. 15 0
      TestSorter/UnitTest1.cs

+ 7 - 1
SORTER.sln

@@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
 # Visual Studio Version 17
 VisualStudioVersion = 17.0.32014.148
 MinimumVisualStudioVersion = 10.0.40219.1
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SORTER", "SORTER\SORTER.csproj", "{2734C2E0-DAEA-4312-A462-7A4F20593CA9}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SORTER", "SORTER\SORTER.csproj", "{2734C2E0-DAEA-4312-A462-7A4F20593CA9}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestSorter", "TestSorter\TestSorter.csproj", "{E5723B14-9CFD-479E-8AAA-35DEBAA8E042}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -15,6 +17,10 @@ Global
 		{2734C2E0-DAEA-4312-A462-7A4F20593CA9}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{2734C2E0-DAEA-4312-A462-7A4F20593CA9}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{2734C2E0-DAEA-4312-A462-7A4F20593CA9}.Release|Any CPU.Build.0 = Release|Any CPU
+		{E5723B14-9CFD-479E-8AAA-35DEBAA8E042}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{E5723B14-9CFD-479E-8AAA-35DEBAA8E042}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{E5723B14-9CFD-479E-8AAA-35DEBAA8E042}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{E5723B14-9CFD-479E-8AAA-35DEBAA8E042}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 26 - 45
SORTER/MainWindow.xaml.cs

@@ -38,9 +38,22 @@ namespace SORTER
         }
 
 
-        void linearSelectionSort(bool? ordering)
+        bool writeFile(string pathCopy, int[] arraySorted)
         {
-            List<int> arraySorted = new List<int>();
+            using (StreamWriter sw = new StreamWriter(pathCopy, false))
+            {
+                for (int i = 0; i < arraySorted.Length; ++i)
+                {
+                    sw.Write(arraySorted[i] + " ");
+                }
+            }
+
+            return true;
+        }
+
+        (int[], string) linearSelectionSort(bool? ordering)
+        {
+            int[] arraySorted = new int[array.Length];
             int[] arrayCopy = new int[array.Length];
 
             int max = int.MinValue;
@@ -63,7 +76,7 @@ namespace SORTER
 
             if (ordering == false)
             {
-                while (arraySorted.Count < arrayCopy.Length)
+                while (arraySorted.Length < arrayCopy.Length)
                 {
                     for (int i = 0; i < arrayCopy.Length; ++i)
                     {
@@ -74,13 +87,13 @@ namespace SORTER
                         }
                     }
                     arrayCopy[index] = 100;
-                    arraySorted.Add(max);
+                    arraySorted.Append(max);
                     max = int.MinValue;
                 }
             }
             else
             {
-                while (arraySorted.Count < arrayCopy.Length)
+                while (arraySorted.Length < arrayCopy.Length)
                 {
                     for (int i = 0; i < arrayCopy.Length; ++i)
                     {
@@ -91,22 +104,13 @@ namespace SORTER
                         }
                     }
                     arrayCopy[index] = 100;
-                    arraySorted.Add(min);
+                    arraySorted.Append(min);
                     min = int.MaxValue;
                 }  
             }
 
-            using (StreamWriter sw = new StreamWriter(pathCopy, false))
-            {
-                for (int i = 0; i < arraySorted.Count; ++i)
-                {
-                    sw.Write(arraySorted[i] + " ");
-                }
-            }
-
             linearTime.Stop();
-
-            arraySorted.Clear();
+            return (arraySorted, pathCopy);
         }
 
 
@@ -172,13 +176,7 @@ namespace SORTER
                 File.Copy(path, pathCopy);
             }
 
-            using (StreamWriter sw = new StreamWriter(pathCopy, false))
-            {
-                for (int i = 0; i < arrayCopy.Length; ++i)
-                {
-                    sw.Write(arrayCopy[i] + " ");
-                }
-            }
+            writeFile(pathCopy, arrayCopy);
         }
 
 
@@ -236,13 +234,7 @@ namespace SORTER
                 File.Copy(path, pathCopy);
             }
 
-            using (StreamWriter sw = new StreamWriter(pathCopy, false))
-            {
-                for (int i = 0; i < arrayCopy.Length; ++i)
-                {
-                    sw.Write(arrayCopy[i] + " ");
-                }
-            }
+            writeFile(pathCopy, arrayCopy);
         }
 
 
@@ -320,13 +312,7 @@ namespace SORTER
                 File.Copy(path, pathCopy);
             }
 
-            using (StreamWriter sw = new StreamWriter(pathCopy, false))
-            {
-                for (int i = 0; i < arrayCopy.Length; ++i)
-                {
-                    sw.Write(arrayCopy[i] + " ");
-                }
-            }
+            writeFile(pathCopy, arrayCopy);
         }
 
 
@@ -390,13 +376,7 @@ namespace SORTER
                 File.Copy(path, pathCopy);
             }
 
-            using (StreamWriter sw = new StreamWriter(pathCopy, false))
-            {
-                for (int i = 0; i < arrayCopy.Length; ++i)
-                {
-                    sw.Write(arrayCopy[i] + " ");
-                }
-            }
+            writeFile(pathCopy, arrayCopy);
         }
 
         private void SortButton_Click(object sender, RoutedEventArgs e)
@@ -409,7 +389,8 @@ namespace SORTER
             {
                 if (LinearButton.IsChecked == true)
                 {
-                    linearSelectionSort(Ascending.IsChecked);
+                    (int[] array, string pathCopy) = linearSelectionSort(Ascending.IsChecked);
+                    writeFile(pathCopy, array);
                 }
                 else if (minMaxButton.IsChecked == true)
                 {

文件差异内容过多而无法显示
+ 1 - 0
SORTER/Линейная сортировка.txt


+ 27 - 0
TestSorter/TestSorter.csproj

@@ -0,0 +1,27 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <TargetFramework>net6.0-windows</TargetFramework>
+    <Nullable>enable</Nullable>
+
+    <IsPackable>false</IsPackable>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
+    <PackageReference Include="xunit" Version="2.4.1" />
+    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
+      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+      <PrivateAssets>all</PrivateAssets>
+    </PackageReference>
+    <PackageReference Include="coverlet.collector" Version="3.1.0">
+      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+      <PrivateAssets>all</PrivateAssets>
+    </PackageReference>
+  </ItemGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\SORTER\SORTER.csproj" />
+  </ItemGroup>
+
+</Project>

+ 15 - 0
TestSorter/UnitTest1.cs

@@ -0,0 +1,15 @@
+using Xunit;
+using SORTER;
+
+namespace TestSorter
+{
+    public class UnitTest1
+    {
+        [Fact]
+        public void Test1()
+        {
+            int[] array = new int[] { 3, 2, 3 };
+            Assert.Equal(1, 1);
+        }
+    }
+}