Huffman實現檔案壓縮

2021-07-22 10:17:53 字數 2297 閱讀 9319

#pragma once

#includeusing namespace std;

#include "huffmantree.hpp"

typedef long long longtype;

//#define _debug_

// 檔案資訊

struct fileinfo

bool operator < (const fileinfo& info) const

bool operator != (const fileinfo& info) const

fileinfo operator +(const fileinfo& info) };

ostream& operator<<(ostream& os, const fileinfo& info)

class filecompress

} void _generatehuffmancode(huffmannode* root)

// 尋找編碼是從葉子節點到根節點,所以要對編碼進行逆置

reverse(code.begin(), code.end());

}} }

// 遞迴的方式生成編碼

void _generatehuffmancode_r(huffmannode* root, string code)

}} bool readline(file* fout, string& line)

return true;

} void compress(const char* filename)

// 2.根據字元出現的次數構建huffman樹,並生成每個字元對應的huffman編碼

huffmantree_ptree;

fileinfo invalid(0);

tree.createhuffmantree(_fileinfos, 256, invalid);

#ifdef _debug_

tree.levelorder();

#endif

//string code;

//_generatehuffmancode(tree.getrootnode(), code);

_generatehuffmancode(tree.getrootnode());

cout<";

#endif

for (size_t i = 0; i < code.size(); ++i)

}} if (pos)

cout<>32, countstr, 10);

fputs(countstr, finconfig);

fputc('\n', finconfig);

_itoa(charcount&0xffffffff, countstr, 10);

fputs(countstr, finconfig);

fputc('\n', finconfig);*/

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

}fclose(fin);

fclose(fout);

fclose(finconfig);

} void uncompress(const char* filename)

else

}// 2.根據讀出的配置資訊,重建huffman樹

huffmantree_ptree;

fileinfo invalid(0);

tree.createhuffmantree(_fileinfos, 256, invalid);

#ifdef _debug_

tree.levelorder();

#endif

//// huffman樹的根節點的權值就是所有檔案字元出現次數的和,

// 也就是檔案字元出現的次數

//huffmannode* root = tree.getrootnode();

long long charcount = 0;

cout<<"解壓縮"<* cur = root;

ch = fgetc(fout);

while(1)

if (pos == 0)

}fclose(fin);

fclose(fout);

fclose(foutconfig);

}private:

fileinfo _fileinfos[256];

};void testcompress()

void testuncompress()

void testfilecompress()

huffman實現檔案壓縮

mark一下 include include include include struct headheader 512 tmp void compress printf destination filename gets outputfile ofp fopen outputfile,wb if ...

利用Huffman樹實現檔案壓縮

利用huffman樹實現檔案壓縮 使用的編輯語言是c 專案目的是能夠實現對檔案的壓縮及解壓,涉及到的技術主要有huffman樹的實現,檔案的io操作,優先順序佇列等 整體思路 整個過程是依賴於huffman樹,因此要構建出乙個可供我們使用的huffman樹 壓縮時,操作原始檔,以字元形式讀取檔案資訊...

huffman編碼實現資料壓縮

huffman編碼壓縮 huffman編碼壓縮的 如下 主方法 public static void main string args 2.統計內容的字元數量,構建list為哈夫曼樹做準備 將統計字元個數的方法封裝 public static list countcharnum byte bytes...