#include #include #include #include #include using namespace std; struct HNode { HNode *parent; HNode *lch, *rch; int weight,codelen; char data; char *code; //������ }; //�����������ͣ�ָ�룩 typedef HNode* HTree; typedef HNode* ElemType; //˳��� typedef struct { ElemType *elem; int length; //��ǰ���ݵ�ʵ�ʳ���(�ж�������) } seqList; //����˳���ht��������Ź��������Ľ�� seqList ht; //��ʼ����������������Ҷ�ӽ����Ϣ���뵽˳��� //a:Ȩֵ��b��Ҷ����ַ���n��Ҷ������ void input(int a[], char b[], int n) { for (int i = 0; i < n; i++) { HNode *tmp = new HNode; tmp->parent = NULL; tmp->weight = a[i]; tmp->data = b[i]; tmp->lch = NULL; tmp->rch = NULL; ht.elem[i]=tmp; ht.length++; } } // n:�±�Ϊ[0~n)��������n����˳���ht�� 0~n��ѡ��2��Ȩֵ��С�Ľ�㣬����±��ŵ�s1,s2�� void selectMin(int n, int &s1, int &s2) { int m1 = 0x7fff; int m2 = 0x7fff; // m1ȡ��СȨ�أ�m2ȡ��СȨ�� int lnode = 0; int rnode = 0; // lnode, rnode�ֱ�ȡ������СȨ�صĽ��λ�� for(int k=0; kparent==NULL){ if(ht.elem[k]->weightweight; lnode=k; } else if(ht.elem[k]->weightweight; rnode = k; } } } s1=lnode; s2=rnode; } //����n��Ҷ�ӽ�㴴�����������������������cht�� void creatHt(HTree &cht, int n) { HNode tNode; int m = 2 * n - 1; int s1, s2; //��ʼ0~n-1��ѡ for (int i = n; i < m; i++) { HNode *r = new HNode; r->parent = NULL; selectMin(i, s1, s2); r->weight=ht.elem[s1]->weight+ht.elem[s2]->weight; r->lch=ht.elem[s1]; r->rch=ht.elem[s2]; ht.elem[s1]->parent=r; ht.elem[s2]->parent=r; ht.elem[i]=r; ht.length++; } cht = ht.elem[m-1]; } void printHfTreePre(HTree t)//��������������� { if (t != NULL) { if (t->lch == NULL && t->rch == NULL) { cout << "weight:" << t->weight << " " << t->data << ":" <code << endl;// } printHfTreePre(t->lch); printHfTreePre(t->rch); } } void hufmadecode(char* filename,HTree t,char *ziped,int len){//����ѹ���ĵ� ifstream infile(filename); if(!infile.is_open()){ cerr<<"ifstream open file error\n"; return; } infile.seekg(0,ios_base::end); int length = infile.tellg(); infile.seekg(ios_base::beg); char* buff=new char[length+1](); infile.read(buff,length+1); int count =0; for(int i=0;idata){ strcpy(ziped+count,ht.elem[j]->code); count+=ht.elem[j]->codelen; break; } } } ziped[count+1]='\0'; infile.close(); } //�������±������� void hufEncode(HTree t, int k, char *code) { if (t == NULL) return; if (t->lch == NULL && t->rch == NULL) { code[k] = '\0'; t->code = new char[k + 1]; strncpy(t->code, code ,k + 1); t->codelen=k; } if(t->lch != NULL){ code[k]='0'; hufEncode(t->lch, k+1, code); } if(t->rch != NULL){ code[k]='1'; hufEncode(t->rch, k+1, code); } } //����: �����dec�� void hufDecode(HTree &ht, char str[], char *dec) { HNode *t = ht; while (*(str+1)) { if(*str=='0')t=t->lch; else t=t->rch; if(t->lch==NULL&&t->rch==NULL){ cout<data; t=ht; } str++; } } int Search(char *a,char ch) {//�����������ַ�ch���ڵ�λ�ã����������±꣬���򷵻�-1 for(int i=0;a[i]!='\0';i++){ if(a[i]==ch)return i; } return -1; } /* ͳ���ļ����ַ��ij��ִ��� freq����Ŵ��� letters������ַ� */ int countLetter(char* filename, int *freq, char *letters) { ifstream infile(filename); if(!infile.is_open()){ cerr<<"ifstream open file error\n"; return 0; } infile.seekg(0,ios_base::end); int length = infile.tellg(); infile.seekg(ios_base::beg); char* buff=new char[length+1](); infile.read(buff,length+1); int count=0; for(int i=0;i>s; while(strcmp(s,choe)){ if(strcmp(s,chob)==0){ cout<<"������txt�ļ�·����"; cin>>s; len = countLetter(s, freq, letters); cout<<"-----------------------------------------"<>s; } return 0; }