6. 簡易霍夫曼解碼(Huffman decoding)資料解壓縮系統
namespace Q6
{
public partial class Form1 : Form
{
Dictionary<string, char> map = new Dictionary<string, char>();
public Form1()
{
InitializeComponent();
map["10"] = 'A';
map["01"] = 'B';
map["11"] = 'C';
map["001"] = 'D';
map["000"] = 'E';
}
private void button3_Click(object sender, EventArgs e)
{
Close();
}
private void button1_Click(object sender, EventArgs e)
{
var txt = "";
var n = new Random().Next(26, 51);
for (int i = 0; i < n; i++)
{
txt += new Random().Next(0, 2) == 1 ? "0" : "1";
}
input1.Text = txt;
vaild1.Clear();
decode1.Clear();
}
private void button2_Click(object sender, EventArgs e)
{
var txt = "";
var n = new Random().Next(26, 51);
for (int i = 0; i < n; i++)
{
txt += new Random().Next(0, 2) == 1 ? "0" : "1";
}
input2.Text = txt;
valid2.Clear();
decode2.Clear();
}
private void button4_Click(object sender, EventArgs e)
{
string a = "";
string copy = input1.Text;
while (copy.Length != 0)
{
bool success = false;
foreach (var item in map)
{
if (copy.StartsWith(item.Key))
{
a += item.Value;
copy = copy.Substring(item.Key.Length, copy.Length - item.Key.Length);
success = true;
vaild1.Text = "";
break;
}
}
if (!success)
{
a = "";
vaild1.Text = "不合理";
break;
}
}
decode1.Text = a;
a = "";
copy = input2.Text;
while (copy.Length != 0)
{
bool success = false;
foreach (var item in map)
{
if (copy.StartsWith(item.Key))
{
a += item.Value;
copy = copy.Substring(item.Key.Length, copy.Length - item.Key.Length);
success = true;
valid2.Text = "";
break;
}
}
if (!success)
{
a = "";
valid2.Text = "不合理";
break;
}
}
decode2.Text = a;
}
}
}
Last updated