分享
 
 
 

LZW算法的 C#实现

王朝c#·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

LZW

#undef debug

#define debugdisplay

#undef debugdictionary

using System;

using System.Collections;

namespace LZW

{

public class cLZW

{

#region Constrcut

public cLZW()

{

}

#endregion

#region Coding

public string InCharStream

{

set { _InCharStream = value; }

get {return _InCharStream; }

}

public ArrayList CodingCodeStream

{

get {return _CodingCodeStream;}

}

public ArrayList CodingDictionary

{

get {return _CodingDictionary;}

}

private void InitCodingDictionary()

{

_CodingDictionary.Clear();

#if debug

_CodingDictionary.Add("A");

_CodingDictionary.Add("B");

_CodingDictionary.Add("C");

#else

for(int i = 0; i < 256; i++)

{

_CodingDictionary.Add((char)i);

}

#endif

}

private void AddCodingDictionary(object str)

{

_CodingDictionary.Add(str);

}

private void AddCodingCodeStream(object str)

{

_CodingCodeStream.Add(str);

}

private bool ISInCodingDictionary(string Prefix)

{

bool result = false;

int count = _CodingDictionary.Count;

for(int i = 0; i < count; i++)

{

string temp = _CodingDictionary[i].ToString();

if (temp.IndexOf(Prefix) >= 0)

{

result = true;

break;

}

}

return result;

}

private string GetIndexCodingDictionary(string Prefix)

{

string result ="0";

int count = _CodingDictionary.Count;

for(int i = 0; i < count; i++)

{

string temp = _CodingDictionary[i].ToString();

if (temp.IndexOf(Prefix) >= 0)

{

result = Convert.ToString(i + 1);

break;

}

}

return result;

}

private void DisplayCodingCodeStream()

{

System.Console.WriteLine("*********_CodingCodeStream************");

for(int i = 0; i < _CodingCodeStream.Count; i++)

{

System.Console.WriteLine(_CodingCodeStream[i].ToString());

}

}

private void DisplayCodingDictionary()

{

System.Console.WriteLine("*********_CodingDictionary************");

for(int i = 0; i < _CodingDictionary.Count; i++)

{

System.Console.WriteLine(_CodingDictionary[i].ToString());

}

}

private void DisplayInCharStream()

{

System.Console.WriteLine("*********_InCharStream************");

System.Console.WriteLine(_InCharStream);

}

private void InitCodingCodeStream()

{

_CodingCodeStream.Clear();

}

private ArrayList _CodingDictionary = new ArrayList();

private string _InCharStream = "";

private ArrayList _CodingCodeStream = new ArrayList();

public void Coding()

{

string Prefix ="" ;

string c ="";

string PrefixIndex= "0";

int count = _InCharStream.Length;

if (count == 0) return ;

InitCodingDictionary();

InitCodingCodeStream();

Prefix = _InCharStream[0].ToString();

for(int i = 1; i < count; i++)

{

c = _InCharStream[i].ToString();

if (ISInCodingDictionary( Prefix + c))

{

Prefix += c;

}

else

{

PrefixIndex = GetIndexCodingDictionary(Prefix);

AddCodingCodeStream(PrefixIndex);

AddCodingDictionary( Prefix + c);

Prefix = c;

}

}

PrefixIndex = GetIndexCodingDictionary(Prefix);

AddCodingCodeStream(PrefixIndex);

#if debugdisplay

DisplayInCharStream();

DisplayCodingCodeStream();

#if debugdictionary

DisplayCodingDictionary();

#endif

#endif

}

#endregion

#region Decode

private ArrayList _DeCodeDictionary = new ArrayList();

private ArrayList _OutCharStream = new ArrayList();

private int[] _DeCodeCodeStream ;

public void SetDeCodeSCodetream(int[] obj)

{

int count = obj.Length;

_DeCodeCodeStream = new int[count];

for(int i =0; i < count ; i++)

{

_DeCodeCodeStream[i] = obj[i];

}

}

public void SetDeCodeSCodetream(ArrayList obj)

{

int count = obj.Count;

_DeCodeCodeStream = new int[count];

for(int i =0; i < count ; i++)

{

_DeCodeCodeStream[i] = System.Convert.ToInt32(obj[i]);

}

}

public int[] GetDeCodeCodeStream()

{

return _DeCodeCodeStream;

}

public string OutCharStream

{

get

{

string result = "";

for(int i = 0,count = _OutCharStream.Count; i < count; i++)

{

result += _OutCharStream[i].ToString();

}

return result;

}

}

public ArrayList DeCodeDictionary

{

get

{

return _DeCodeDictionary;

}

}

private void InitDeCodeDictionary()

{

_DeCodeDictionary.Clear();

#if debug

_DeCodeDictionary.Add("A");

_DeCodeDictionary.Add("B");

_DeCodeDictionary.Add("C");

#else

for(int i = 0; i < 256; i++)

{

_DeCodeDictionary.Add((char)i);

}

#endif

}

private void InitOutCharStream()

{

_OutCharStream.Clear();

}

private void DisplayOutCharStream()

{

System.Console.WriteLine("*********_OutCharStream************");

string temp = "";

for(int i = 0; i < _OutCharStream.Count; i++)

{

temp = temp + (_OutCharStream[i].ToString());

}

System.Console.WriteLine(temp);

}

private void DisplayDeCodeDictionary()

{

System.Console.WriteLine("*********_DeCodeDictionary************");

for(int i = 0; i < _DeCodeDictionary.Count; i++)

{

System.Console.WriteLine(_DeCodeDictionary[i].ToString());

}

}

private void DisplayDeCodeCodeStream()

{

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();

}

}

}

----------------------------------------------

τ 认识你会不会是一种错误

如果这是个错误 也应该是个美丽的错误

但愿这个错误能够一辈子

______________ζ浮云¢惊龙

原文http://www.whsdn.net/bbs/?p=thread&ForumID=30&ThreadID=44

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有