LZW算法的 C#实现
作者: 来源: 添加时间:2006-5-21 19:49:36{
System.Console.WriteLine("*********_DeCodeCodeStream************");
int count = _DeCodeCodeStream.Length;
for(int i = 0; i < count; i++)
{
System.Console.WriteLine("{0}",_DeCodeCodeStream[i]);
}
}
private void AddOutCharStream(object str)
{
_OutCharStream.Add(str);
}
private void AddDeCodeDictionary(object str)
{
_DeCodeDictionary.Add(str);
}
private bool ISInDeCodeDictionary(int cw)
{
bool result = false;
int count = _DeCodeDictionary.Count;
if (cw <= count - 1)
{
result = true;
}
return result;
}
public void Decode()
{
InitDeCodeDictionary();
InitOutCharStream();
int cw = 0;
int pw = 0;
string Prefix = "";
string c="";
cw = _DeCodeCodeStream[0] - 1;
this.AddOutCharStream(this._DeCodeDictionary[cw]);
pw = cw;
int count = _DeCodeCodeStream.Length;
if (count == 0) return;
for(int i = 1; i < count; i++)
{
cw = _DeCodeCodeStream[i] - 1;
if (ISInDeCodeDictionary(cw))
{
this.AddOutCharStream(this._DeCodeDictionary[cw]);
Prefix = this._DeCodeDictionary[pw].ToString();
c = (this._DeCodeDictionary[cw].ToString())[0].ToString();
this.AddDeCodeDictionary(Prefix + c);
}
else
{
Prefix = this._DeCodeDictionary[pw].ToString();
c = Prefix[0].ToString();
this.AddOutCharStream(Prefix + c);
this.AddDeCodeDictionary(Prefix + c);
}
pw = cw;
}
#if debugdisplay
DisplayOutCharStream();
DisplayDeCodeCodeStream();
#if debugdictionary
DisplayDeCodeDictionary();
#endif
#endif
}
#endregion
}
}
#undef debug
using System;
namespace LZW
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
cLZW lzw = new cLZW();
#if debug
lzw.InCharStream = "ABBABABACCBBAAA";
#else
System.Console.WriteLine("Enter the Tests CharArray [a-zA-Z0-9]:");
lzw.InCharStream = System.Console.ReadLine();
#endif
System.Console.WriteLine("The Coding ... ...");
lzw.Coding();
System.Console.WriteLine("The DeCode ... ...");
lzw.SetDeCodeSCodetream(lzw.CodingCodeStream);
lzw.Decode();
System.Console.ReadLine();
}
}
}
第 2 页,共 2 页 [1] [2]
站内搜索