From 1ec86abc606ccefad68899762f14e92f290966f0 Mon Sep 17 00:00:00 2001 From: Anton Skalkin Date: Thu, 27 Apr 2023 16:15:04 +0400 Subject: [PATCH] init --- AiSD.Laba2.sln | 6 +++ HuffmanCode/Algoritm.cs | 73 ++++++++++++++++++++++++++++++++++ HuffmanCode/HuffmanCode.csproj | 10 +++++ HuffmanCode/Node.cs | 16 ++++++++ HuffmanCode/Program.cs | 7 ++++ 5 files changed, 112 insertions(+) create mode 100644 HuffmanCode/Algoritm.cs create mode 100644 HuffmanCode/HuffmanCode.csproj create mode 100644 HuffmanCode/Node.cs create mode 100644 HuffmanCode/Program.cs diff --git a/AiSD.Laba2.sln b/AiSD.Laba2.sln index 1e5c80a..75e4fd1 100644 --- a/AiSD.Laba2.sln +++ b/AiSD.Laba2.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 17.4.33403.182 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AiSD.Laba2", "AiSD.Laba2\AiSD.Laba2.csproj", "{FDC419D9-4F2A-4771-840D-46A2EFD4AA6E}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HuffmanCode", "HuffmanCode\HuffmanCode.csproj", "{A3AA3A92-EEE1-4133-95B3-C8C6814A0964}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {FDC419D9-4F2A-4771-840D-46A2EFD4AA6E}.Debug|Any CPU.Build.0 = Debug|Any CPU {FDC419D9-4F2A-4771-840D-46A2EFD4AA6E}.Release|Any CPU.ActiveCfg = Release|Any CPU {FDC419D9-4F2A-4771-840D-46A2EFD4AA6E}.Release|Any CPU.Build.0 = Release|Any CPU + {A3AA3A92-EEE1-4133-95B3-C8C6814A0964}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A3AA3A92-EEE1-4133-95B3-C8C6814A0964}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A3AA3A92-EEE1-4133-95B3-C8C6814A0964}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A3AA3A92-EEE1-4133-95B3-C8C6814A0964}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/HuffmanCode/Algoritm.cs b/HuffmanCode/Algoritm.cs new file mode 100644 index 0000000..9a2a675 --- /dev/null +++ b/HuffmanCode/Algoritm.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Collections; + +namespace ЛР_2._0 +{ + internal class Algoritm + { + Node root = null; + Dictionary characters = new Dictionary(); + + public void work(string data) + { + var charDict = new Dictionary(); + for (int i = 0; i < data.Length; i++) + { + if (charDict.ContainsKey(data[i])) + { + charDict[data[i]]++; + } + else + { + charDict.Add(data[i], 1); + } + } + + List nodes = new List(); + foreach (var item in charDict) + { + nodes.Add(new Node() { Character = item.Key.ToString(), Count = item.Value }); + } + nodes.Sort(); + while (nodes.Count != 1) + { + var tempNode = new Node(); + tempNode.Character = nodes[0].Character + nodes[1].Character; + //нужно чтобы сложило две буквы в одно + tempNode.Count = nodes[0].Count + nodes[1].Count; + tempNode.Left = nodes[0]; + tempNode.Right = nodes[1]; + + nodes.RemoveAt(0); + nodes.RemoveAt(0); + nodes.Add(tempNode); + nodes.Sort(); + } + root = nodes[0]; + + MyEncode(root); + + //TODO: красиво вывести + } + + protected void MyEncode(Node node, string code = "") + { + if (node.Character.Length == 1) + { + characters.Add(node.Character[0], code); + } + else + { + MyEncode(node.Left, code + "0"); + MyEncode(node.Right, code + "1"); + } + } + + } + +} + diff --git a/HuffmanCode/HuffmanCode.csproj b/HuffmanCode/HuffmanCode.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/HuffmanCode/HuffmanCode.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/HuffmanCode/Node.cs b/HuffmanCode/Node.cs new file mode 100644 index 0000000..4542de9 --- /dev/null +++ b/HuffmanCode/Node.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HuffmanCode +{ + internal class Node + { + public string Сharacter { get; set; } + public int Count { get; set; } + public Node? Left { get; set; } + public Node? Right { get; set; } + } +} diff --git a/HuffmanCode/Program.cs b/HuffmanCode/Program.cs new file mode 100644 index 0000000..98469a6 --- /dev/null +++ b/HuffmanCode/Program.cs @@ -0,0 +1,7 @@ +using ЛР_2._0; + +Console.WriteLine("Please enter the string:"); +string data = Console.ReadLine(); +Algoritm algoritm = new Algoritm(); +algoritm.work(data); +