diff --git a/main.cpp b/main.cpp index 8f1536e..349ca1e 100644 --- a/main.cpp +++ b/main.cpp @@ -1,55 +1,357 @@ -#include -#include -class HuffmanCode{ -public: - explicit HuffmanCode(char *base){init(base);} - void init(char* base); - char* encode(char *in); - char* decode(char *in); -private: - struct codeTable{ - char code; - bool* coding; - }; - struct BiNode{ - char code; - int power; - bool* coding; - BiNode lchild; - BiNode rchild; - bool operator<(BiNode& in){return this->power < in.power;} - }; - BiNode* BiHead = nullptr; - std::list BiList; - std::list CodeTableList; - //void freeTheTree(BiNode* inHead); +//#include +#define Change(x,y) (x)^=(y)^=(x)^=(y); +struct HNode{ + int weight; + int parent; + int LChild; + int RChild; + char data; + bool operator<(HNode & r){ + if(this->parent != -1) return false; + if(r.parent != -1) return true; + return (this->weight < r.weight); + } }; -void HuffmanCode::init(char *base) { +struct HCode{ + char data; + char code[100]; + bool operator==(char &_in){ return this->data == _in;} +}; +class Huffman{ +private: + HNode *HTree; // 哈弗曼树 + HCode *HCodeTable; // 哈弗曼编码表 + void SelectMin(int &x,int &y,int begin,int end); // 找两个最小值 + void Reveres(char a[]); // 字符串倒置 +public: + void CreateHTree(const int a[],const char *b,int n); + void CreateCodeTable(int n); + void Encode(char *s,char *b); + void Decode(char *s,char *b); + explicit Huffman(const char *a); + HCode *GetHCodeTable(){return HCodeTable;} + HNode *GetHTree(){return HTree;} + ~Huffman(); +}; +void Huffman::SelectMin(int &x, int &y, int begin, int end){ + x = begin-1;while (HTree[++x].parent !=-1); + y = x;while (HTree[++y].parent !=-1); + if(HTree[y] < HTree[x]) Change(x,y); + for(int i = x;i < end;i++) if(HTree[i] < HTree[x]) x = i; + for(int i = y;i < end;i++) if(HTree[i] < HTree[y] && i != x) y = i; +} +void Huffman::Reveres(char *a) { + int l = 0; + while (a[++l] != '\0'); + for(int i = 0;i +#include +#include +#include + +#ifdef WIN32 +#define CLEAR "cls" +#elif defined(linux) +#define CLEAR "clear" +#endif +using namespace std; +void printTheHuffmanTree(Huffman &_in); +void printTheHuffmanCodeTable(Huffman &_in); +void DEplay(Huffman &_in); +int main(){ + system(CLEAR); + cout <<"+----------------------------+"<> a; + Huffman Huffman1(a); + char b[800]={'\0'}; + char c[100]={'\0'}; + Huffman1.Encode(a,b); + Huffman1.Decode(b,c); + system(CLEAR); + cout <<"+----------------------------+"<> chouse; + switch(chouse){ + case 0: + printTheHuffmanTree(Huffman1); + break; + case 1: + printTheHuffmanCodeTable(Huffman1); + break; + case 2: + DEplay(Huffman1); + break; + case 3: + return 0; + default: + goto goback; + } + goto goback; +} +void printTheHuffmanTree(Huffman & _in){ + HNode * root = _in.GetHTree(); + int a = -1; + HNode * Hlist = root; + while (Hlist[++a].parent != -1); + root = &Hlist[a]; + HNode * list[100][100]; + list[0][0] = root; + int deep = 0; + bool end; do{ - for (auto &i : BiList) { - if(i.code == *base){ - i.power++; - goto goOut; + deep++; + for(int bh = 0;bhLChild != -1) + list[deep][bh*2] = &Hlist[list[deep-1][bh]->LChild]; + if(list[deep-1][bh]->RChild != -1) + list[deep][bh*2+1] = &Hlist[list[deep-1][bh]->RChild]; } } - BiList.push_back(BiNode{*base,0}); - goOut:; - }while (*(++base)); - std::list copy(BiList); - while (BiList.size() > 1){ - BiList.sort(); - BiNode L = *BiList.begin(); - BiList.pop_front(); - BiNode R = *BiList.begin(); - BiList.pop_front(); - BiList.push_back(BiNode{0,L.power+R.power, nullptr,L,R}); + end = true; + for(int bh = 0;bhdata == '\0') + cout << list[n][bh]->weight ; + else + cout << list[n][bh]->data; + } + } + cout << endl; + for(int bh = 0;bh < pow(2,n);bh++) { + if(list[n][bh] && list[n][bh]->data == '\0') + cout << setw(soot[deep-n-1]+1) + << setfill('-') + << setiosflags(ios::left) + << '|' + << setw(soot[deep-n-1]+1) + << setfill(' ') + << setiosflags(ios::left) + << '+'; + else cout << setw(2*(soot[deep-n-1]+1)) + << setfill(' ') << ' '; + } + cout << endl; + for(int bh = 0;bh < pow(2,n);bh++) { + if(list[n][bh] && list[n][bh]->data == '\0') + cout << setw(soot[deep-n-1]+1) + << setfill(' ') + << setiosflags(ios::left) + << '|' + << setw(soot[deep-n-1]+1) + << setfill(' ') + << setiosflags(ios::left) + << '|'; + else cout << setw(2*(soot[deep-n-1]+1)) + << setfill(' ') << ' '; + } + cout << endl; } - BiHead = &*BiList.begin(); - std::stack S; - S.push(BiHead); - while (!S.empty()){ - BiNode* tmp = S.top(); - S.pop(); - if(tmp->rchild||tmp->lchild) + delete[] soot; + cout<< "按回车回到主菜单"; + cin.get();cin.get(); +} +/* + * 0 0 + * |-----+ |-----+ + * | | | | + * 1 1 1 1 + * |--+ |--+ |--+ |--+ + * | | | | | | | | + * 2 2 2 2 2 2 2 2 + */ +void printTheHuffmanCodeTable(Huffman &_in){ + HCode * table = _in.GetHCodeTable(); + system(CLEAR); + cout << "+------+----------+"<> i; + char a[800]; + char b[100]; + if(i == 2){ + cout << "请输入需要解码的字符:"; + cin >> a; + _in.Decode(a,b); + cout << "解码完毕 结果是:"<> b; + _in.Encode(b,a); + cout << "编码完毕 结果为:"<< endl; + cout << a<