The C ++ implementation of the Harman encoding and decoding algorithms encapsulates functional modules into a class of Huffman
: Http://download.csdn.net/source/1409937
File main. cpp
// Main. CPP <br/> # include <iostream> <br/> # include "Huffman. H "<br/> using namespace STD; <br/> int main () <br/>{< br/> Huffman; <br/> Huffman. readtextfromfile ("text.txt "); <br/>/*************** Step 1: Enter the characters and corresponding weights *********** * ******/<br/> cout <"select: 1. the program automatically counts characters and weights (recommended) 2. user input "<Endl; <br/> int R; <br/> DO <br/>{< br/> CIN> r; <br/>}< br/> while (R! = 1) & (R! = 2); <br/> If (r = 1) <br/> Huffman. countcharsweight (); <br/> else <br/> Huffman. inputcharsweight (); <br/> cout <"character and corresponding weight:" <Endl; <br/> Huffman. printcharweight (); <br/> system ("pause"); <br/> cout <Endl; <br/>/***************** Step 2 to build the hafman tree, */<br/> Huffman. makecharmap (); <br/> cout <"character and corresponding encoding:" <Endl; <br/> Huffman. printcharcode (); <br/> system ("pause"); <br/> cout <Endl; <br/>, output the result and save it to the file *****************/<br/> cout <"encode the original text: "<Endl; <br/> cout <" Original: "<Endl; <br/> Huffman. printtext (); <br/> Huffman. encode (); <br/> cout <"encoding:" <Endl; <br/> Huffman. printcode (); <br/> Huffman. savecodetofile ("code.txt"); <br/> system ("pause"); <br/> cout <Endl; <br/>/**************** Step 4: Read the 0 and 1 code strings from the file, decode them, and output and store them to the file *** * **************/<br/> cout <"decodes the encoding: "<Endl; <br/> Huffman. readcodefromfile ("code.txt"); <br/> cout <"encoding:" <Endl; <br/> Huffman. printcode (); <br/> Huffman. decode (); <br/> cout <"Original:" <Endl; <br/> Huffman. printtext (); <br/> Huffman. savetexttofile ("resulttext.txt"); <br/> cout <"/Ngame over cmd_^. "<Endl; <br/> system (" pause "); <br/> return 0; <br/>}
File Huffman. h
// Huffman. h: interface for the Huffman class. <br/> ////////////////////////////// //////////////////////////////////////// <br/> # If! Defined (afx_huffman_h000008a1863a_6641084fe9_9596_5eebe76b53f70000included _) <br/> # define compiler _ <br/> # If _ msc_ver> 1000 <br/> # pragma once <br/> # endif // _ msc_ver> 1000 <br/> # include <string> <br/>/************************* Data Structure **** * ******************/<br/> // Haffman Tree node <br/> typedef struct <br/> {<br/> unsigned int weight; <br/> unsigned int parent; <br/> Unsigned int lchild; <br/> unsigned int rchild; <br/>} hufftreenode, * hufftree; <br/> // character-weight-encoding ing <br/> typedef struct <br/> {<br/> char C; <br/> unsigned int weight; <br/> char * Code; <br/>} charmapnode, * charmap; <br/> ******** * ******************/<br/> class Huffman <br/>{< br/> PRIVATE: <br/> void select (int n, Int & S1, Int & S2); <br/> hufftree; // hafman tree <br/> charmap char S; // numbers table <br/> int N; // number of characters <br/> STD: String text; // original <br/> STD: String code; // Code <br/> Public: <br/> void inputcharsweight (); <br/> void countcharsweight (); <br/> void decode (); <br/> void readtextfromfile (char * filename); <br/> void readcodefromfile (char * filename); <br/> void savetexttofile (char * filename ); <br/> void savecodetofile (char * filename); <br/> void printcode (); <br/> void makecharmap (); <br/> voi D printtext (); <br/> void printcharcode (); <br/> void printcharweight (); <br/> void setcharmap (charmap M, int number ); <br/> void encode (); <br/> Huffman (); <br/> virtual ~ Huffman (); <br/>}; <br/> # endif //! Defined (afx_huffman_h000008a1863a_6641084fe9_9596_5eebe76b53f70000included _) <br/>
File Huffman. cpp
// Huffman. CPP: Implementation of the Huffman class. <br/> ////////////////////////////// //////////////////////////////////////// <br/> # include "Huffman. H "<br/> # include <iostream> <br/> # include <fstream> <br/> using namespace STD; <br/> //////////////////////////////////// /// // <br/> // construction/destruction <br/> ///////////////////////////////// //////////////// /// // <Br/> Huffman: Huffman () <br/>{< br/> hufftree = NULL; <br/> chars = NULL; <br/> n = 0; <br/>}< br/> Huffman: :~ Huffman () <br/>{< br/>}< br/> // hafman encoding for text strings <br/> void Huffman: encode () <br/> {<br/> code = ""; <br/> for (string: size_type I = 0; I! = Text. size (); ++ I) <br/>{< br/> for (Int J = 1; j <= N; ++ J) <br/> If (chars [J]. C = text [I]) <br/> code + = chars [J]. code; <br/>}< br/> // set the comment table <br/> void Huffman: setcharmap (charmap M, int number) <br/> {<br/> chars = m; <br/> n = number; <br/>}< br/> // on the hufftree [1 .. in N], select the parent 0 node and the smallest weight node. Its serial number is S1, S2 <br/> void Huffman: Select (int n, Int & S1, Int & S2) <br/> {<br/> S1 = S2 = 0; <br/> for (INT I = 1; I <= N; ++ I) <br/>{< br/> If (hufftree [I]. parent! = 0) <br/> continue; <br/> If (S1 = 0) <br/> S1 = I; <br/> else if (s2 = 0) <br/>{< br/> // The policy used here to make the S1 weight less than S2 in the whole process <br/> If (hufftree [I]. weight <pufftree [S1]. weight) <br/>{< br/> S2 = S1; <br/> S1 = I; <br/>}< br/> else <br/> S2 = I; <br/>}< br/> else <br/> {<br/> If (hufftree [I]. weight <pufftree [S1]. weight) <br/>{< br/> S2 = S1; <br/> S1 = I; <br/>}< br/> else if (hufftree [I]. weight <pufftree [s2]. weight) <Br/> S2 = I; <br/>}< br/> void Huffman: printcharweight () <br/> {<br/> for (INT I = 1; I <= N; ++ I) <br/> {<br/> switch (chars [I]. c) <br/> {<br/> case '/t': <br/> cout <"/t"; <br/> break; <br/> case '/N': <br/> cout <"// n"; <br/> break; <br/> default: <br/> cout <chars [I]. c; <br/> break; <br/>}< br/> cout <"--" <chars [I]. weight <Endl; <br/>}< br/> void Huffman: printcharcode () <Br/> {<br/> for (INT I = 1; I <= N; ++ I) <br/> {<br/> switch (chars [I]. c) <br/> {<br/> case '/t': <br/> cout <"/t"; <br/> break; <br/> case '/N': <br/> cout <"// n"; <br/> break; <br/> default: <br/> cout <chars [I]. c; <br/> break; <br/>}< br/> cout <"--" <chars [I]. code <Endl; <br/>}< br/> // output text string <br/> void Huffman: printtext () <br/>{< br/> cout <text <Endl; <br/>}< br/> // output 0-1 encoding <br/> V Oid Huffman: printcode () <br/>{< br/> cout <Code <Endl; <br/>}< br/> // create a character-encoding table based on the weight of each character <br/> void Huffman: makecharmap () <br/>{< br/> If (n <= 1) <br/> return; <br/> int m = 2 * n-1; // number of nodes required by the Haffman tree <br/> hufftree = new hufftreenode [M + 1]; // unit 0 is not used <br/> // initialization <br/> int I; <br/> for (I = 1; I <= N; ++ I) <br/> {<br/> hufftree [I]. weight = chars [I]. weight; <br/> hufftree [I]. parent = 0; <br/> hufftree [I]. lchild = 0; <br/> hufftree [I]. rchild = 0; <br/>}< br/> for (I = n + 1; I <= m; ++ I) <br/> {<br/> hufftree [I]. weight = 0; <br/> hufftree [I]. parent = 0; <br/> hufftree [I]. lchild = 0; <br/> hufftree [I]. rchild = 0; <br/>}< br/> // create a Haffman tree <br/> for (I = n + 1; I <= m; ++ I) <br/>{< br/> int S1, S2; <br/> select (I-1, S1, S2); <br/> hufftree [S1]. parent = hufftree [s2]. parent = I; <br/> hufftree [I]. lchild = S1; <br/> hufftree [I]. Rchild = S2; <br/> hufftree [I]. weight = hufftree [S1]. weight + hufftree [s2]. weight; <br/>}< br/> // reverse evaluate the Haffman encoding of each character from the leaf to the root node <br/> char * Cd = new char [N]; // allocate the workspace for encoding (the maximum number of characters encoded is n-1 plus '/0') <br/> Cd [n-1] ='/0 '; // encoding Terminator <br/> for (I = 1; I <= N; ++ I) // calculate the hafman encoding character by character <br/>{< br/> int start = n-1; <br/> int C, F; <br/> // reverse coding from leaves to roots <br/> for (C = I, F = hufftree [I]. parent; F! = 0; C = F, F = hufftree [f]. parent) <br/>{< br/> If (hufftree [f]. lchild = C) // The left child code is 0 <br/> Cd [-- start] = '0 '; <br/> else // The right child code is 1 <br/> Cd [-- start] = '1'; <br/>}< br/> chars [I]. code = new char [n-start]; // allocate space for the I-character encoding <br/> strcpy (chars [I]. code, & CD [start]); <br/>}< br/> Delete [] cd; <br/>}< br/> // read the original text from the file <br/> void Huffman: readtextfromfile (char * filename) <br/>{< br/> ifstream infile (filename); <br/> If (! Infile) <br/>{< br/> cerr <"the file cannot be opened! "<Endl; <br/> return; <br/>}< br/> char C; <br/> while (infile. get (c) <br/>{< br/> text + = C; <br/>}< br/> // Save the encoding to a file <br/> void Huffman: savecodetofile (char * filename) <br/>{< br/> ofstream OUTFILE (filename); <br/> If (! OUTFILE) <br/>{< br/> cerr <"An error occurred while saving the file! "<Endl; <br/> return; <br/>}< br/> OUTFILE <code; <br/>}< br/> // read the encoding from the file <br/> void Huffman: readcodefromfile (char * filename) <br/>{< br/> ifstream infile (filename); <br/> If (! Infile) <br/>{< br/> cerr <"the file cannot be opened! "<Endl; <br/> return; <br/>}< br/> infile> code; <br/>}< br/> // decodes the 0-1 encoding string <br/> void Huffman: Decode () <br/> {<br/> text = ""; <br/> string: size_type I, count; <br/> for (I = 0; I <code. size (); I + = count) <br/>{< br/> // the maximum length of the encoding result for each character is n-1, try <br/> for (COUNT = 1; count <n; ++ count) <br/> {<br/> for (Int J = 1; j <= N; ++ J) <br/> If (code. substr (I, count) = chars [J]. code) <br/>{< br/> text + = chars [J]. c; <br/> goto next; <br/>}< br/> next: <br/>; <br/>}< br/> // calculate the weights of each character in the original text. <br/> void Huffman: countcharsweight () <br/>{< br/> If (text. empty () <br/> return; <br/> If (chars! = NULL) <br/> Delete [] chars; <br/> int I = 0; <br/> n = 0; <br/> chars = new charmapnode [2]; <br/> chars [1]. C = text [I]; <br/> chars [1]. weight = 1; <br/> ++ N; <br/> for (I = 1; I! = Text. size (); ++ I) <br/>{< br/> Int J; <br/> for (j = 1; j <= N; ++ J) // traverse the current orders table. If this character already exists, the weight is + 1 <br/>{< br/> If (Text [I] = chars [J]. c) <br/> {<br/> ++ chars [J]. weight; <br/> break; <br/>}< br/> If (j> N) // The character does not exist, add this character <br/> {<br/> + + N; <br/> charmap newchars = new charmapnode [n + 1]; <br/> memcpy (newchars, chars, N * sizeof (charmapnode); <br/> Delete [] chars; <br/> chars = newchars; <br/> chars [N]. C = Text [I]; <br/> chars [N]. weight = 1; <br/>}< br/> // enter characters and corresponding weights <br/> void Huffman :: inputcharsweight () <br/>{< br/> cout <"Enter the character set size n (n> 1):" <Endl; <br/> CIN> N; <br/> If (chars! = NULL) <br/> Delete [] chars; <br/> chars = new charmapnode [n + 1]; // unit 0 is not used <br/> cout <"Enter the character and weight:" <Endl; <br/> for (INT I = 1; I <= N; ++ I) <br/>{< br/> cin. ignore (); // clear the input buffer <br/> cin. get (chars [I]. c); // enter a single character, which can be a blank space <br/> CIN> chars [I]. weight; <br/>}< br/> void Huffman: savetexttofile (char * filename) <br/>{< br/> ofstream OUTFILE (filename); <br/> If (! OUTFILE) <br/>{< br/> cerr <"An error occurred while saving the file! "<Endl; <br/> return; <br/>}< br/> OUTFILE <text; <br/>}< br/>