#include #include #include #include #include "nr.h" using namespace std; // Driver for routines hufmak, hufenc, hufdec int main(void) { const unsigned int MC=512,MQ=MC+MC-1; unsigned long i,j,ilong,n,nb,nlong,nt,lcode,nch; int k; string mess,ness,code; huffcode hcode(MQ,MQ,MQ,MQ); Vec_ULNG nfreq(256); ifstream fp("text.dat"); for (i=0;i= 32) nfreq[mess[k]-32]++; } fp.close(); nch=96; // here is the initialization that constructs the code NR::hufmak(nfreq,nch,ilong,nlong,hcode); cout << "ind char nfreq ncod icod" << endl; for (j=0;j>3)+1; // message termination (encode a single int character) NR::hufenc(ilong,code,nb,hcode); // here we decode the message to get the original back nb=0; ness.erase(); // decode up to 10 chars more than should be needed for (j=0;j= n+10) NR::nrerror("Huffman coding: Never get here"); cout << "Length of line input,coded= " << n << " " << lcode << endl; cout << "Decoded output:" << endl << ness << endl; nt=ness.length(); if (nt != n) cout << "Error! : n decoded != n input" << endl; if (nt-n == 1) cout << "May be harmless spurious character." << endl; } return 0; }