·您的位置: 首页 » 资源教程 » 编程开发 » ASP.NET » LZW算法的 C#实现

LZW算法的 C#实现

类别: ASP.NET教程  评论数:0 总得分:0

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

本新闻共2页,当前在第1页  1  2  

本新闻共2页,当前在第1页  1  2  

-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1