⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 compress.h

📁 我最近写的一个LZW的压缩与解压缩程序。可能有个别地方尚有缺陷
💻 H
字号:
#ifndef COMPRESS_H
#define COMPRESS_H
class element {
   friend void Compress();
   public:
      operator unsigned long() const {return key;}
      element& operator =(unsigned long y)
         {key = y; return *this;}
   private:
      int code;
      unsigned long key;
};

unsigned char s[codes];  // used to reconstruct text
int size,                // size of reconstructed text
    LeftOver,            // left over bits from last code
    status = 0;          // 0 iff no left over bits
element ht[codes];       // dictionary
ifstream in;
ofstream out;

void SetFiles(int argc, char* argv[])
{// Create input and output streams.
   char OutputFile[50], InputFile[50];
   // see if file name provided
   if (argc >= 2) strcpy(InputFile,argv[1]);
   else {// name not provided, ask for it
         cout << "Enter name of file to compress"
              << endl;
         cout << "File name should have no extension"  
              << endl;
         cin >> InputFile;}

   // name should not have an extension
   if (strchr(InputFile,'.')) {
       cerr << "File name has extension" << endl;
       exit(1);}

   // open files in binary mode
   in.open(InputFile,ios::binary);
   // in.open(InputFile); for g++
   if (in.fail()) {cerr << "Cannot open " << InputFile 
                        << InputFile << endl;
                   exit(1);}
   strcpy(OutputFile,InputFile);
   strcat(OutputFile, ".zzz");
   out.open(OutputFile,ios::binary);
   // out.open(OutputFile); for g++
}

void Output(unsigned long code);

void Compress()
{// Lempel-Ziv-Welch compressor.
   // define and initialize the code dictionary
   ChainHashTable<element, unsigned long> h(D);
   element e;
   for (int i = 0; i < alpha; i++) {// initialize
      e.key = i;
      e.code = i;
      h.Insert(e);
      }
   int used = alpha; // number of codes used

   // input and compress
   unsigned char c;
   in.get(c);      // first character of input file
   unsigned long pcode = c; // prefix code
   if (!in.eof()) {// file length is > 1
      do {// process rest of file
          in.get(c);
          if (in.eof()) break;  // finished
          unsigned long k = (pcode << ByteSize) + c;
          // see if code for k is in the dictionary
          if (h.Search(k, e)) pcode = e.code;  // yes
          else {// k not in table
                Output(pcode);
                if (used < codes) // create new code
                   {e.code = used++;
                    e.key = (pcode << ByteSize) | c;
                    h.Insert(e);}
                pcode = c;}
   	} while(true);

      // output last code(s)
      Output(pcode);
      if (status) {c = LeftOver << excess;
                   out.put(c);}
      }

   out.close();
   in.close();
}

void Output(unsigned long pcode)
{// Output 8 bits, save rest in LeftOver.
   unsigned char c,d;
   if (status) {// 4 bits remain
      d = pcode & mask1; // right ByteSize bits
      c = (LeftOver << excess) | (pcode >> ByteSize);
      out.put(c);
      out.put(d);
      status = 0;}
   else {
      LeftOver = pcode & mask2; // right excess bits
      c = pcode >> excess;
      out.put(c);
      status = 1;}
}


#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -