This commit is contained in:
Антон Скалкин 2023-04-27 16:15:04 +04:00
parent dbe688c904
commit 1ec86abc60
5 changed files with 112 additions and 0 deletions

View File

@ -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

73
HuffmanCode/Algoritm.cs Normal file
View File

@ -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<char, string> characters = new Dictionary<char, string>();
public void work(string data)
{
var charDict = new Dictionary<char, int>();
for (int i = 0; i < data.Length; i++)
{
if (charDict.ContainsKey(data[i]))
{
charDict[data[i]]++;
}
else
{
charDict.Add(data[i], 1);
}
}
List<Node> nodes = new List<Node>();
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");
}
}
}
}

View File

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

16
HuffmanCode/Node.cs Normal file
View File

@ -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; }
}
}

7
HuffmanCode/Program.cs Normal file
View File

@ -0,0 +1,7 @@
using ЛР_2._0;
Console.WriteLine("Please enter the string:");
string data = Console.ReadLine();
Algoritm algoritm = new Algoritm();
algoritm.work(data);