From 7b000321caa5d3f2e86bed5265cfc655f4f0bf0e Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Sun, 11 Apr 2021 02:46:14 +0300 Subject: [PATCH 1/5] add createGraph and not finished AlgorithmPrima --- hw5Routers/hw5Routers.sln | 25 +++++++++ hw5Routers/hw5Routers/AlgorithmPrima.cs | 53 ++++++++++++++++++ hw5Routers/hw5Routers/Program.cs | 12 ++++ hw5Routers/hw5Routers/Routers.cs | 74 +++++++++++++++++++++++++ hw5Routers/hw5Routers/Routers.txt | 2 + hw5Routers/hw5Routers/hw5Routers.csproj | 8 +++ 6 files changed, 174 insertions(+) create mode 100644 hw5Routers/hw5Routers.sln create mode 100644 hw5Routers/hw5Routers/AlgorithmPrima.cs create mode 100644 hw5Routers/hw5Routers/Program.cs create mode 100644 hw5Routers/hw5Routers/Routers.cs create mode 100644 hw5Routers/hw5Routers/Routers.txt create mode 100644 hw5Routers/hw5Routers/hw5Routers.csproj diff --git a/hw5Routers/hw5Routers.sln b/hw5Routers/hw5Routers.sln new file mode 100644 index 0000000..a989b1c --- /dev/null +++ b/hw5Routers/hw5Routers.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31005.135 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hw5Routers", "hw5Routers\hw5Routers.csproj", "{55F17858-3620-46FF-BF81-AF2E7DD2595D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {55F17858-3620-46FF-BF81-AF2E7DD2595D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {55F17858-3620-46FF-BF81-AF2E7DD2595D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {55F17858-3620-46FF-BF81-AF2E7DD2595D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {55F17858-3620-46FF-BF81-AF2E7DD2595D}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {054D180F-116B-4C02-B1C8-C947EB1CCD17} + EndGlobalSection +EndGlobal diff --git a/hw5Routers/hw5Routers/AlgorithmPrima.cs b/hw5Routers/hw5Routers/AlgorithmPrima.cs new file mode 100644 index 0000000..8d253bc --- /dev/null +++ b/hw5Routers/hw5Routers/AlgorithmPrima.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace hw5Routers +{ + class AlgorithmPrima + { + static int maxKey(int[] key, bool[] mstSet) + { + int max = int.MinValue; + int indexMax = -1; + for (int i = 0; i < key.Length; i++) + if (mstSet[i] == false && key[i] > max) + { + max = key[i]; + indexMax = i; + } + return indexMax; + } + + static void Algorithm(int[,] matrix) + { + int size = matrix.Length; + int[] parent = new int[size]; + int[] key = new int[size]; + bool[] isConsist = new bool[size]; + for (int i = 0; i < size; i++) + { + key[i] = int.MaxValue; + isConsist[i] = false; + } + key[0] = 0; + parent[0] = -1; + for (int count = 0; count < size - 1; count++) + { + int u = maxKey(key, isConsist); + isConsist[u] = true; + for (int v = 0; v < size; v++) + { + if (matrix[u, v] != 0 && isConsist[v] == false && matrix[u, v] < key[v]) + { + parent[v] = u; + key[v] = matrix[u, v]; + } + } + } + /// + /// + /// + } + } +} \ No newline at end of file diff --git a/hw5Routers/hw5Routers/Program.cs b/hw5Routers/hw5Routers/Program.cs new file mode 100644 index 0000000..310d5c5 --- /dev/null +++ b/hw5Routers/hw5Routers/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace hw5Routers +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/hw5Routers/hw5Routers/Routers.cs b/hw5Routers/hw5Routers/Routers.cs new file mode 100644 index 0000000..20a42c3 --- /dev/null +++ b/hw5Routers/hw5Routers/Routers.cs @@ -0,0 +1,74 @@ +using System; +using System.IO; +using System.Collections.Generic; + +namespace hw5Routers +{ + public class Routers + { + public int[,] CreateGraph(string filePath) + { + int vertices = CountsVertices(filePath); + var matrix = new int[vertices, vertices]; + var file = new StreamReader(filePath); + string stringLine = file.ReadLine(); + while (stringLine != null) + { + stringLine = stringLine.Replace(':', ' '); + stringLine = stringLine.Replace(',', ' '); + string[] lineDrop = stringLine.Split(" ", StringSplitOptions.RemoveEmptyEntries); + var numberFirst = Int32.Parse(lineDrop[0]); + for (int i = 1; i < lineDrop.Length; ++i) + { + var numberSecond = Int32.Parse(lineDrop[i]); + var distance = Int32.Parse(lineDrop[i + 1].Substring(1, lineDrop.Length - 2)); + matrix[numberFirst, numberSecond] = matrix[numberSecond, numberFirst] = distance; + } + } + return matrix; + } + + public int CountsVertices(string path) + { + var file = new StreamReader(path); + string stringLine = file.ReadLine(); + var list = new List(); + int vertice = 0; + while (stringLine != null) + { + stringLine = stringLine.Replace(':', ' '); + stringLine = stringLine.Replace(',', ' '); + string[] lineDrop = stringLine.Split(" ", StringSplitOptions.RemoveEmptyEntries); + var number = Int32.Parse(lineDrop[0]); + if (!list.Contains(number)) + { + list.Add(number); + vertice++; + } + for (int i = 0; i < lineDrop.Length / 2; ++i) + { + number = Int32.Parse(lineDrop[2 * i + 1]); + if (!list.Contains(number)) + { + list.Add(number); + vertice++; + } + } + } + return vertice; + } + + public void WriteInFile(int[,] matrix, string filePath) + { + FileInfo fileOut = new FileInfo(filePath); + if (fileOut.Exists) + { + fileOut.Delete(); + } + FileStream currentFile = new FileStream(filePath, FileMode.Create); + StreamWriter writer = new StreamWriter(currentFile); + + + } + } +} diff --git a/hw5Routers/hw5Routers/Routers.txt b/hw5Routers/hw5Routers/Routers.txt new file mode 100644 index 0000000..3d90741 --- /dev/null +++ b/hw5Routers/hw5Routers/Routers.txt @@ -0,0 +1,2 @@ +1: 2 (10), 3 (5) +2: 3 (1) \ No newline at end of file diff --git a/hw5Routers/hw5Routers/hw5Routers.csproj b/hw5Routers/hw5Routers/hw5Routers.csproj new file mode 100644 index 0000000..c73e0d1 --- /dev/null +++ b/hw5Routers/hw5Routers/hw5Routers.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + From 6f8a014040fb2d2e72a61532038daf9ae68674d5 Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Sun, 11 Apr 2021 20:29:38 +0300 Subject: [PATCH 2/5] add tests --- hw5Routers/hw5Routers.Test/AlgorithmTest.cs | 56 +++++++++++++ .../hw5Routers.Test/FileFunctionsTest.cs | 20 +++++ hw5Routers/hw5Routers.Test/RoutersTest.txt | 2 + .../hw5Routers.Test/RoutersTestResult.txt | 1 + .../hw5Routers.Test/hw5Routers.Test.csproj | 19 +++++ hw5Routers/hw5Routers.sln | 8 +- hw5Routers/hw5Routers/AlgorithmPrima.cs | 81 +++++++++++++++---- .../{Routers.cs => FileFunctions.cs} | 50 ++++++++---- .../hw5Routers/GraphDisconnectedException.cs | 18 +++++ hw5Routers/hw5Routers/Program.cs | 11 ++- hw5Routers/hw5Routers/Routers2.txt | 2 + 11 files changed, 235 insertions(+), 33 deletions(-) create mode 100644 hw5Routers/hw5Routers.Test/AlgorithmTest.cs create mode 100644 hw5Routers/hw5Routers.Test/FileFunctionsTest.cs create mode 100644 hw5Routers/hw5Routers.Test/RoutersTest.txt create mode 100644 hw5Routers/hw5Routers.Test/RoutersTestResult.txt create mode 100644 hw5Routers/hw5Routers.Test/hw5Routers.Test.csproj rename hw5Routers/hw5Routers/{Routers.cs => FileFunctions.cs} (55%) create mode 100644 hw5Routers/hw5Routers/GraphDisconnectedException.cs create mode 100644 hw5Routers/hw5Routers/Routers2.txt diff --git a/hw5Routers/hw5Routers.Test/AlgorithmTest.cs b/hw5Routers/hw5Routers.Test/AlgorithmTest.cs new file mode 100644 index 0000000..346aa69 --- /dev/null +++ b/hw5Routers/hw5Routers.Test/AlgorithmTest.cs @@ -0,0 +1,56 @@ +using NUnit.Framework; + +namespace hw5Routers.Test +{ + public class Tests + { + [Test] + public void AlgorithmTest() + { + int[,] graphFirst = + { + {0, 2, 4, 6 }, + {2, 0, 0, 8 }, + {4, 0, 0, 9 }, + {6, 8, 9, 0 } + }; + int[,] graphFirstResult = + { + {0, 0, 0, 6 }, + {0, 0, 0, 8 }, + {0, 0, 0, 9 }, + {6, 8, 9, 0 } + }; + Assert.AreEqual(AlgorithmPrima.Algorithm(graphFirst), graphFirstResult); + int[,] graphSecond = + { + {0, 0, 0, 3 }, + {0, 0, 0, 6 }, + {0, 0, 0, 9 }, + {3, 6, 9, 0 } + }; + int[,] graphSecondResult = + { + {0, 0, 0, 3 }, + {0, 0, 0, 6 }, + {0, 0, 0, 9 }, + {3, 6, 9, 0 } + }; + Assert.AreEqual(AlgorithmPrima.Algorithm(graphSecond), graphSecondResult); + } + + [Test] + public void AlgorithmExceptionTest() + { + int[,] graph = + { + {0, 3, 0, 0}, + {3, 0, 0, 0}, + {0, 0, 0, 2}, + {0, 0, 2, 0} + }; + Assert.Throws(() => AlgorithmPrima.Algorithm(graph)); + } + + } +} \ No newline at end of file diff --git a/hw5Routers/hw5Routers.Test/FileFunctionsTest.cs b/hw5Routers/hw5Routers.Test/FileFunctionsTest.cs new file mode 100644 index 0000000..4a75133 --- /dev/null +++ b/hw5Routers/hw5Routers.Test/FileFunctionsTest.cs @@ -0,0 +1,20 @@ +using NUnit.Framework; +using System.IO; + +namespace hw5Routers.Test +{ + class FileFunctionsTest + { + [Test] + public void FileTest() + { + string[] result = + { + "1: 2 (10), 3 (5)" + }; + FileFunctions.WriteInFile(AlgorithmPrima.Algorithm(FileFunctions.CreateGraph("..\\..\\..\\RoutersTest.txt")), "..\\..\\RoutersTestResult.txt"); + var resultFromFile = File.ReadLines("..\\..\\RoutersTestResult.txt"); + Assert.AreEqual(result, resultFromFile); + } + } +} \ No newline at end of file diff --git a/hw5Routers/hw5Routers.Test/RoutersTest.txt b/hw5Routers/hw5Routers.Test/RoutersTest.txt new file mode 100644 index 0000000..3d90741 --- /dev/null +++ b/hw5Routers/hw5Routers.Test/RoutersTest.txt @@ -0,0 +1,2 @@ +1: 2 (10), 3 (5) +2: 3 (1) \ No newline at end of file diff --git a/hw5Routers/hw5Routers.Test/RoutersTestResult.txt b/hw5Routers/hw5Routers.Test/RoutersTestResult.txt new file mode 100644 index 0000000..58c5eed --- /dev/null +++ b/hw5Routers/hw5Routers.Test/RoutersTestResult.txt @@ -0,0 +1 @@ +1: 2 (10), 3 (5) \ No newline at end of file diff --git a/hw5Routers/hw5Routers.Test/hw5Routers.Test.csproj b/hw5Routers/hw5Routers.Test/hw5Routers.Test.csproj new file mode 100644 index 0000000..18d899d --- /dev/null +++ b/hw5Routers/hw5Routers.Test/hw5Routers.Test.csproj @@ -0,0 +1,19 @@ + + + + netcoreapp3.1 + + false + + + + + + + + + + + + + diff --git a/hw5Routers/hw5Routers.sln b/hw5Routers/hw5Routers.sln index a989b1c..ff75a42 100644 --- a/hw5Routers/hw5Routers.sln +++ b/hw5Routers/hw5Routers.sln @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.31005.135 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hw5Routers", "hw5Routers\hw5Routers.csproj", "{55F17858-3620-46FF-BF81-AF2E7DD2595D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "hw5Routers", "hw5Routers\hw5Routers.csproj", "{55F17858-3620-46FF-BF81-AF2E7DD2595D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hw5Routers.Test", "hw5Routers.Test\hw5Routers.Test.csproj", "{B3DF091C-3CAD-4E6A-9401-94EFDBA9E0D2}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +17,10 @@ Global {55F17858-3620-46FF-BF81-AF2E7DD2595D}.Debug|Any CPU.Build.0 = Debug|Any CPU {55F17858-3620-46FF-BF81-AF2E7DD2595D}.Release|Any CPU.ActiveCfg = Release|Any CPU {55F17858-3620-46FF-BF81-AF2E7DD2595D}.Release|Any CPU.Build.0 = Release|Any CPU + {B3DF091C-3CAD-4E6A-9401-94EFDBA9E0D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B3DF091C-3CAD-4E6A-9401-94EFDBA9E0D2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B3DF091C-3CAD-4E6A-9401-94EFDBA9E0D2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B3DF091C-3CAD-4E6A-9401-94EFDBA9E0D2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/hw5Routers/hw5Routers/AlgorithmPrima.cs b/hw5Routers/hw5Routers/AlgorithmPrima.cs index 8d253bc..332bad2 100644 --- a/hw5Routers/hw5Routers/AlgorithmPrima.cs +++ b/hw5Routers/hw5Routers/AlgorithmPrima.cs @@ -4,14 +4,52 @@ namespace hw5Routers { - class AlgorithmPrima + public static class AlgorithmPrima { - static int maxKey(int[] key, bool[] mstSet) + private static bool CheckGraph(int[,] matrix) + { + var vertexStatus = new int[matrix.GetLength(0)]; + int number = 1; + vertexStatus[0] = number; + int count = 0; + while (count < matrix.GetLength(0) && !Check(vertexStatus)) + { + for (int i = 0; i < matrix.GetLength(0); i++) + { + if (vertexStatus[i] == number) + { + for (int j = 0; j < matrix.GetLength(0); j++) + { + if (matrix[i, j] != 0 && vertexStatus[j] == 0) + { + vertexStatus[j] = number + 1; + } + } + } + } + number++; + count++; + } + return Check(vertexStatus); + } + + private static bool Check(int[] vetrexStatus) + { + for (int i = 0; i < vetrexStatus.Length; i++) + { + if (vetrexStatus[i] == 0) + { + return false; + } + } + return true; + } + public static int MaxKey(int[] key, bool[] isConsist) { int max = int.MinValue; int indexMax = -1; for (int i = 0; i < key.Length; i++) - if (mstSet[i] == false && key[i] > max) + if (isConsist[i] == false && key[i] > max) { max = key[i]; indexMax = i; @@ -19,35 +57,46 @@ static int maxKey(int[] key, bool[] mstSet) return indexMax; } - static void Algorithm(int[,] matrix) + public static int[,] Algorithm(int[,] matrix) { - int size = matrix.Length; + if (!CheckGraph(matrix)) + { + throw new GraphDisconnectedException(); + } + int size = matrix.GetLength(0); int[] parent = new int[size]; int[] key = new int[size]; bool[] isConsist = new bool[size]; for (int i = 0; i < size; i++) { - key[i] = int.MaxValue; - isConsist[i] = false; + key[i] = int.MinValue; } key[0] = 0; parent[0] = -1; for (int count = 0; count < size - 1; count++) { - int u = maxKey(key, isConsist); - isConsist[u] = true; - for (int v = 0; v < size; v++) + int indexFirst = MaxKey(key, isConsist); + isConsist[indexFirst] = true; + for (int indexSecond = 0; indexSecond < size; indexSecond++) { - if (matrix[u, v] != 0 && isConsist[v] == false && matrix[u, v] < key[v]) + if (matrix[indexFirst, indexSecond] != 0 && isConsist[indexSecond] == false && matrix[indexFirst, indexSecond] > key[indexSecond]) { - parent[v] = u; - key[v] = matrix[u, v]; + parent[indexSecond] = indexFirst; + key[indexSecond] = matrix[indexFirst, indexSecond]; } } } - /// - /// - /// + return CreateMatrix(parent, matrix); + } + + private static int[,] CreateMatrix(int[] parent, int[,] matrix) + { + var newMatrix = new int[matrix.GetLength(0), matrix.GetLength(0)]; + for (int i = 1; i < matrix.GetLength(0); ++i) + { + newMatrix[parent[i], i] = newMatrix[i, parent[i]] = matrix[i, parent[i]]; + } + return newMatrix; } } } \ No newline at end of file diff --git a/hw5Routers/hw5Routers/Routers.cs b/hw5Routers/hw5Routers/FileFunctions.cs similarity index 55% rename from hw5Routers/hw5Routers/Routers.cs rename to hw5Routers/hw5Routers/FileFunctions.cs index 20a42c3..dd3b1bd 100644 --- a/hw5Routers/hw5Routers/Routers.cs +++ b/hw5Routers/hw5Routers/FileFunctions.cs @@ -4,11 +4,11 @@ namespace hw5Routers { - public class Routers + public static class FileFunctions { - public int[,] CreateGraph(string filePath) + public static int[,] CreateGraph(string filePath) { - int vertices = CountsVertices(filePath); + int vertices = CountsVertexs(filePath); var matrix = new int[vertices, vertices]; var file = new StreamReader(filePath); string stringLine = file.ReadLine(); @@ -17,18 +17,19 @@ public class Routers stringLine = stringLine.Replace(':', ' '); stringLine = stringLine.Replace(',', ' '); string[] lineDrop = stringLine.Split(" ", StringSplitOptions.RemoveEmptyEntries); - var numberFirst = Int32.Parse(lineDrop[0]); - for (int i = 1; i < lineDrop.Length; ++i) + var numberFirst = Int32.Parse(lineDrop[0]) - 1; + for (int i = 1; i < lineDrop.Length - 1; i += 2) { - var numberSecond = Int32.Parse(lineDrop[i]); - var distance = Int32.Parse(lineDrop[i + 1].Substring(1, lineDrop.Length - 2)); + var numberSecond = Int32.Parse(lineDrop[i]) - 1; + var distance = Int32.Parse(lineDrop[i + 1].Substring(1, lineDrop[i + 1].Length - 2)); matrix[numberFirst, numberSecond] = matrix[numberSecond, numberFirst] = distance; } + stringLine = file.ReadLine(); } return matrix; } - public int CountsVertices(string path) + private static int CountsVertexs(string path) { var file = new StreamReader(path); string stringLine = file.ReadLine(); @@ -39,7 +40,7 @@ public int CountsVertices(string path) stringLine = stringLine.Replace(':', ' '); stringLine = stringLine.Replace(',', ' '); string[] lineDrop = stringLine.Split(" ", StringSplitOptions.RemoveEmptyEntries); - var number = Int32.Parse(lineDrop[0]); + var number = Int32.Parse(lineDrop[0]) - 1; if (!list.Contains(number)) { list.Add(number); @@ -47,28 +48,47 @@ public int CountsVertices(string path) } for (int i = 0; i < lineDrop.Length / 2; ++i) { - number = Int32.Parse(lineDrop[2 * i + 1]); + number = Int32.Parse(lineDrop[2 * i + 1]) - 1; if (!list.Contains(number)) { list.Add(number); vertice++; } } + stringLine = file.ReadLine(); } return vertice; } - public void WriteInFile(int[,] matrix, string filePath) + public static void WriteInFile(int[,] matrix, string filePath) { FileInfo fileOut = new FileInfo(filePath); if (fileOut.Exists) { fileOut.Delete(); } - FileStream currentFile = new FileStream(filePath, FileMode.Create); - StreamWriter writer = new StreamWriter(currentFile); - - + FileStream newFile = new FileStream(filePath, FileMode.Create); + StreamWriter file = new StreamWriter(newFile); + for (int i = 0; i < matrix.GetLength(0) - 1; i++) + { + var line = $"{i + 1}: "; + for (int j = i + 1; j < matrix.GetLength(0); ++j) + { + if (matrix[i, j] != 0) + { + line += $"{j + 1} ({matrix[i, j]}), "; + } + } + if (line != $"{i + 1}: ") + { + file.WriteLine(line.Substring(0, line.Length - 2)); + } + } + file.Close(); + if (fileOut.Exists) + { + fileOut.MoveTo(filePath); + } } } } diff --git a/hw5Routers/hw5Routers/GraphDisconnectedException.cs b/hw5Routers/hw5Routers/GraphDisconnectedException.cs new file mode 100644 index 0000000..7439aa1 --- /dev/null +++ b/hw5Routers/hw5Routers/GraphDisconnectedException.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace hw5Routers +{ + public class GraphDisconnectedException : Exception + { + public GraphDisconnectedException() + { + } + + public GraphDisconnectedException(string message) + : base(message) + { + } + } +} diff --git a/hw5Routers/hw5Routers/Program.cs b/hw5Routers/hw5Routers/Program.cs index 310d5c5..3d08a8f 100644 --- a/hw5Routers/hw5Routers/Program.cs +++ b/hw5Routers/hw5Routers/Program.cs @@ -1,4 +1,5 @@ using System; +using System.IO; namespace hw5Routers { @@ -6,7 +7,15 @@ class Program { static void Main(string[] args) { - Console.WriteLine("Hello World!"); + try + { + FileFunctions.WriteInFile(AlgorithmPrima.Algorithm(FileFunctions.CreateGraph("..\\..\\..\\Routers2.txt")), "RoutersTestResult.txt"); + } + catch (GraphDisconnectedException) + { + Console.Error.WriteLine("Граф несвязный!"); + } + //Routers.WriteInFile(AlgorithmPrima.Algorithm(Routers.CreateGraph(args[0])), args[1]); } } } diff --git a/hw5Routers/hw5Routers/Routers2.txt b/hw5Routers/hw5Routers/Routers2.txt new file mode 100644 index 0000000..1b57312 --- /dev/null +++ b/hw5Routers/hw5Routers/Routers2.txt @@ -0,0 +1,2 @@ +1: 2 (10) +3: 4 (1) \ No newline at end of file From 663c2f00ba2f95d028afcd0183901e1b2ef40c4b Mon Sep 17 00:00:00 2001 From: MikePuzanov <89803250877@mail.ru> Date: Fri, 16 Apr 2021 23:52:55 +0300 Subject: [PATCH 3/5] add comments --- hw5Routers/hw5Routers/AlgorithmPrima.cs | 4 ++++ hw5Routers/hw5Routers/FileFunctions.cs | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/hw5Routers/hw5Routers/AlgorithmPrima.cs b/hw5Routers/hw5Routers/AlgorithmPrima.cs index 332bad2..1be50ea 100644 --- a/hw5Routers/hw5Routers/AlgorithmPrima.cs +++ b/hw5Routers/hw5Routers/AlgorithmPrima.cs @@ -57,6 +57,10 @@ public static int MaxKey(int[] key, bool[] isConsist) return indexMax; } + /// + /// алгоритм Прима + /// + /// новую матрицу public static int[,] Algorithm(int[,] matrix) { if (!CheckGraph(matrix)) diff --git a/hw5Routers/hw5Routers/FileFunctions.cs b/hw5Routers/hw5Routers/FileFunctions.cs index dd3b1bd..3f916a8 100644 --- a/hw5Routers/hw5Routers/FileFunctions.cs +++ b/hw5Routers/hw5Routers/FileFunctions.cs @@ -6,6 +6,10 @@ namespace hw5Routers { public static class FileFunctions { + /// + /// создает граф по файлу + /// + /// матрицу графа public static int[,] CreateGraph(string filePath) { int vertices = CountsVertexs(filePath); @@ -60,6 +64,9 @@ private static int CountsVertexs(string path) return vertice; } + /// + /// записывает итогую матрицу в файл + /// public static void WriteInFile(int[,] matrix, string filePath) { FileInfo fileOut = new FileInfo(filePath); From 056f2e909e3c50cd13552711bada8054a5402007 Mon Sep 17 00:00:00 2001 From: MikePuzanov <89803250877@mail.ru> Date: Sun, 16 May 2021 20:40:26 +0300 Subject: [PATCH 4/5] fix --- hw5Routers/hw5Routers.Test/AlgorithmTest.cs | 11 ++++++++--- .../hw5Routers.Test/FileFunctionsTest.cs | 2 +- .../hw5Routers.Test/hw5Routers.Test.csproj | 2 +- hw5Routers/hw5Routers.sln | 4 ++-- hw5Routers/hw5Routers/AlgorithmPrima.cs | 18 ++++++++++++------ hw5Routers/hw5Routers/FileFunctions.cs | 19 +++++++++---------- .../hw5Routers/GraphDisconnectedException.cs | 5 ++++- hw5Routers/hw5Routers/Program.cs | 5 ++--- 8 files changed, 39 insertions(+), 27 deletions(-) diff --git a/hw5Routers/hw5Routers.Test/AlgorithmTest.cs b/hw5Routers/hw5Routers.Test/AlgorithmTest.cs index 346aa69..1da0844 100644 --- a/hw5Routers/hw5Routers.Test/AlgorithmTest.cs +++ b/hw5Routers/hw5Routers.Test/AlgorithmTest.cs @@ -1,11 +1,11 @@ using NUnit.Framework; -namespace hw5Routers.Test +namespace Hw5Routers.Test { public class Tests { [Test] - public void AlgorithmTest() + public void AlgorithmTestFirst() { int[,] graphFirst = { @@ -22,13 +22,18 @@ public void AlgorithmTest() {6, 8, 9, 0 } }; Assert.AreEqual(AlgorithmPrima.Algorithm(graphFirst), graphFirstResult); + } + + [Test] + public void AlgorithmTestSecond() + { int[,] graphSecond = { {0, 0, 0, 3 }, {0, 0, 0, 6 }, {0, 0, 0, 9 }, {3, 6, 9, 0 } - }; + }; int[,] graphSecondResult = { {0, 0, 0, 3 }, diff --git a/hw5Routers/hw5Routers.Test/FileFunctionsTest.cs b/hw5Routers/hw5Routers.Test/FileFunctionsTest.cs index 4a75133..0bb3faf 100644 --- a/hw5Routers/hw5Routers.Test/FileFunctionsTest.cs +++ b/hw5Routers/hw5Routers.Test/FileFunctionsTest.cs @@ -1,7 +1,7 @@ using NUnit.Framework; using System.IO; -namespace hw5Routers.Test +namespace Hw5Routers.Test { class FileFunctionsTest { diff --git a/hw5Routers/hw5Routers.Test/hw5Routers.Test.csproj b/hw5Routers/hw5Routers.Test/hw5Routers.Test.csproj index 18d899d..3bac018 100644 --- a/hw5Routers/hw5Routers.Test/hw5Routers.Test.csproj +++ b/hw5Routers/hw5Routers.Test/hw5Routers.Test.csproj @@ -13,7 +13,7 @@ - + diff --git a/hw5Routers/hw5Routers.sln b/hw5Routers/hw5Routers.sln index ff75a42..bb76626 100644 --- a/hw5Routers/hw5Routers.sln +++ b/hw5Routers/hw5Routers.sln @@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.31005.135 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "hw5Routers", "hw5Routers\hw5Routers.csproj", "{55F17858-3620-46FF-BF81-AF2E7DD2595D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hw5Routers", "hw5Routers\Hw5Routers.csproj", "{55F17858-3620-46FF-BF81-AF2E7DD2595D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hw5Routers.Test", "hw5Routers.Test\hw5Routers.Test.csproj", "{B3DF091C-3CAD-4E6A-9401-94EFDBA9E0D2}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "hw5Routers.Test", "hw5Routers.Test\hw5Routers.Test.csproj", "{B3DF091C-3CAD-4E6A-9401-94EFDBA9E0D2}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/hw5Routers/hw5Routers/AlgorithmPrima.cs b/hw5Routers/hw5Routers/AlgorithmPrima.cs index 1be50ea..9030a3f 100644 --- a/hw5Routers/hw5Routers/AlgorithmPrima.cs +++ b/hw5Routers/hw5Routers/AlgorithmPrima.cs @@ -2,8 +2,11 @@ using System.Collections.Generic; using System.Text; -namespace hw5Routers +namespace Hw5Routers { + /// + /// Класс, реализующий алгоритм Прима + /// public static class AlgorithmPrima { private static bool CheckGraph(int[,] matrix) @@ -33,27 +36,30 @@ private static bool CheckGraph(int[,] matrix) return Check(vertexStatus); } - private static bool Check(int[] vetrexStatus) + private static bool Check(int[] vertexStatus) { - for (int i = 0; i < vetrexStatus.Length; i++) + for (int i = 0; i < vertexStatus.Length; i++) { - if (vetrexStatus[i] == 0) + if (vertexStatus[i] == 0) { return false; } } return true; } - public static int MaxKey(int[] key, bool[] isConsist) + + private static int MaxKey(int[] key, bool[] isConsist) { int max = int.MinValue; int indexMax = -1; for (int i = 0; i < key.Length; i++) - if (isConsist[i] == false && key[i] > max) + { + if (!isConsist[i] && key[i] > max) { max = key[i]; indexMax = i; } + } return indexMax; } diff --git a/hw5Routers/hw5Routers/FileFunctions.cs b/hw5Routers/hw5Routers/FileFunctions.cs index 3f916a8..864c81d 100644 --- a/hw5Routers/hw5Routers/FileFunctions.cs +++ b/hw5Routers/hw5Routers/FileFunctions.cs @@ -2,7 +2,7 @@ using System.IO; using System.Collections.Generic; -namespace hw5Routers +namespace Hw5Routers { public static class FileFunctions { @@ -12,15 +12,14 @@ public static class FileFunctions /// матрицу графа public static int[,] CreateGraph(string filePath) { - int vertices = CountsVertexs(filePath); + int vertices = CountVertexes(filePath); var matrix = new int[vertices, vertices]; - var file = new StreamReader(filePath); + using var file = new StreamReader(filePath); string stringLine = file.ReadLine(); while (stringLine != null) { - stringLine = stringLine.Replace(':', ' '); - stringLine = stringLine.Replace(',', ' '); - string[] lineDrop = stringLine.Split(" ", StringSplitOptions.RemoveEmptyEntries); + string[] split = new string[] { " ", ",", ":" }; + string[] lineDrop = stringLine.Split(split, StringSplitOptions.RemoveEmptyEntries); var numberFirst = Int32.Parse(lineDrop[0]) - 1; for (int i = 1; i < lineDrop.Length - 1; i += 2) { @@ -33,9 +32,9 @@ public static class FileFunctions return matrix; } - private static int CountsVertexs(string path) + private static int CountVertexes(string path) { - var file = new StreamReader(path); + using var file = new StreamReader(path); string stringLine = file.ReadLine(); var list = new List(); int vertice = 0; @@ -69,12 +68,12 @@ private static int CountsVertexs(string path) /// public static void WriteInFile(int[,] matrix, string filePath) { - FileInfo fileOut = new FileInfo(filePath); + var fileOut = new FileInfo(filePath); if (fileOut.Exists) { fileOut.Delete(); } - FileStream newFile = new FileStream(filePath, FileMode.Create); + using var newFile = new FileStream(filePath, FileMode.Create); StreamWriter file = new StreamWriter(newFile); for (int i = 0; i < matrix.GetLength(0) - 1; i++) { diff --git a/hw5Routers/hw5Routers/GraphDisconnectedException.cs b/hw5Routers/hw5Routers/GraphDisconnectedException.cs index 7439aa1..6c36378 100644 --- a/hw5Routers/hw5Routers/GraphDisconnectedException.cs +++ b/hw5Routers/hw5Routers/GraphDisconnectedException.cs @@ -2,8 +2,11 @@ using System.Collections.Generic; using System.Text; -namespace hw5Routers +namespace Hw5Routers { + /// + /// Исключение, когда граф несвязанный + /// public class GraphDisconnectedException : Exception { public GraphDisconnectedException() diff --git a/hw5Routers/hw5Routers/Program.cs b/hw5Routers/hw5Routers/Program.cs index 3d08a8f..cd80feb 100644 --- a/hw5Routers/hw5Routers/Program.cs +++ b/hw5Routers/hw5Routers/Program.cs @@ -1,7 +1,7 @@ using System; using System.IO; -namespace hw5Routers +namespace Hw5Routers { class Program { @@ -9,13 +9,12 @@ static void Main(string[] args) { try { - FileFunctions.WriteInFile(AlgorithmPrima.Algorithm(FileFunctions.CreateGraph("..\\..\\..\\Routers2.txt")), "RoutersTestResult.txt"); + FileFunctions.WriteInFile(AlgorithmPrima.Algorithm(FileFunctions.CreateGraph(args[0])), args[1]); } catch (GraphDisconnectedException) { Console.Error.WriteLine("Граф несвязный!"); } - //Routers.WriteInFile(AlgorithmPrima.Algorithm(Routers.CreateGraph(args[0])), args[1]); } } } From 499f6df25bfa00dd89be8fff1dad673d137ba6e2 Mon Sep 17 00:00:00 2001 From: MikePuzanov <89803250877@mail.ru> Date: Thu, 3 Jun 2021 18:39:59 +0300 Subject: [PATCH 5/5] fix mistakes --- hw5Routers/hw5Routers/AlgorithmPrima.cs | 2 +- hw5Routers/hw5Routers/FileFunctions.cs | 16 ++++++++++++++-- hw5Routers/hw5Routers/NoParametersException.cs | 18 ++++++++++++++++++ hw5Routers/hw5Routers/Program.cs | 4 ++++ 4 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 hw5Routers/hw5Routers/NoParametersException.cs diff --git a/hw5Routers/hw5Routers/AlgorithmPrima.cs b/hw5Routers/hw5Routers/AlgorithmPrima.cs index 9030a3f..312766d 100644 --- a/hw5Routers/hw5Routers/AlgorithmPrima.cs +++ b/hw5Routers/hw5Routers/AlgorithmPrima.cs @@ -89,7 +89,7 @@ private static int MaxKey(int[] key, bool[] isConsist) isConsist[indexFirst] = true; for (int indexSecond = 0; indexSecond < size; indexSecond++) { - if (matrix[indexFirst, indexSecond] != 0 && isConsist[indexSecond] == false && matrix[indexFirst, indexSecond] > key[indexSecond]) + if (matrix[indexFirst, indexSecond] != 0 && !isConsist[indexSecond] && matrix[indexFirst, indexSecond] > key[indexSecond]) { parent[indexSecond] = indexFirst; key[indexSecond] = matrix[indexFirst, indexSecond]; diff --git a/hw5Routers/hw5Routers/FileFunctions.cs b/hw5Routers/hw5Routers/FileFunctions.cs index 864c81d..21b8f60 100644 --- a/hw5Routers/hw5Routers/FileFunctions.cs +++ b/hw5Routers/hw5Routers/FileFunctions.cs @@ -4,6 +4,9 @@ namespace Hw5Routers { + /// + /// класс для работы с файлами + /// public static class FileFunctions { /// @@ -12,6 +15,10 @@ public static class FileFunctions /// матрицу графа public static int[,] CreateGraph(string filePath) { + if (filePath == "") + { + throw new NoParametersException(); + } int vertices = CountVertexes(filePath); var matrix = new int[vertices, vertices]; using var file = new StreamReader(filePath); @@ -25,7 +32,8 @@ public static class FileFunctions { var numberSecond = Int32.Parse(lineDrop[i]) - 1; var distance = Int32.Parse(lineDrop[i + 1].Substring(1, lineDrop[i + 1].Length - 2)); - matrix[numberFirst, numberSecond] = matrix[numberSecond, numberFirst] = distance; + matrix[numberFirst, numberSecond] = matrix[numberSecond, numberFirst]; + matrix[numberFirst, numberSecond] = distance; } stringLine = file.ReadLine(); } @@ -68,13 +76,17 @@ private static int CountVertexes(string path) /// public static void WriteInFile(int[,] matrix, string filePath) { + if (filePath == "") + { + throw new NoParametersException(); + } var fileOut = new FileInfo(filePath); if (fileOut.Exists) { fileOut.Delete(); } using var newFile = new FileStream(filePath, FileMode.Create); - StreamWriter file = new StreamWriter(newFile); + var file = new StreamWriter(newFile); for (int i = 0; i < matrix.GetLength(0) - 1; i++) { var line = $"{i + 1}: "; diff --git a/hw5Routers/hw5Routers/NoParametersException.cs b/hw5Routers/hw5Routers/NoParametersException.cs new file mode 100644 index 0000000..8541790 --- /dev/null +++ b/hw5Routers/hw5Routers/NoParametersException.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Hw5Routers +{ + public class NoParametersException : Exception + { + public NoParametersException() + { + } + + public NoParametersException(string message) + : base(message) + { + } + } +} diff --git a/hw5Routers/hw5Routers/Program.cs b/hw5Routers/hw5Routers/Program.cs index cd80feb..b859e31 100644 --- a/hw5Routers/hw5Routers/Program.cs +++ b/hw5Routers/hw5Routers/Program.cs @@ -15,6 +15,10 @@ static void Main(string[] args) { Console.Error.WriteLine("Граф несвязный!"); } + catch (NoParametersException) + { + Console.Error.WriteLine("Введите параметры: путь до входного файла и путь до выходного файла."); + } } } }