C++课程设计:哈夫曼编码器
【问题描述】:利用哈夫曼树实现编码并译码的系统。【基本要求】:从终端读入一段字符集,系统自动统计出字符的个数n以及各个字符出现的次数w作为权值,建立哈夫曼树,并将哈夫曼树以凹入表示法的形式显示在屏幕上。利用已建好的哈夫曼树对字符进行编码,并将该段文字的编码存人一个文件code中,然后输出这段编码。
答案:#include <iostream.h>#include <iomanip.h>#include <string.h>#include <malloc.h>#include <stdio.h>//typedef int TElemType;const int UINT_MAX = 1000;typedef struct{ int weight; int parent, lchild, rchild;} HTNode, *HuffmanTree;typedef char **HuffmanCode;HuffmanTree HT;HuffmanCode HC;int *w, i, j, n;char *z;int flag = 0;int numb = 0;int min(HuffmanTree t, int i){int j, flag; int k = UINT_MAX; for (j = 1; j <= i; j++) if (t[j].weight < k && t[j].parent == 0) k = t[j].weight, flag = j; t[flag].parent = 1; return flag;}void select(HuffmanTree t, int i, int &s1, int &s2){int j; s1 = min(t, i); s2 = min(t, i); if (s1 > s2) { j = s1; s1 = s2; s2 = j; }}void HuffmanCoding(HuffmanTree &HT, HuffmanCode &HC, int *w, int n){int m, i, s1, s2, start; //unsigned c,f; int c, f; HuffmanTree p; char *cd; if (n <= 1) return ;m = 2 * n - 1; HT = (HuffmanTree)malloc((m + 1) *sizeof(HTNode)); for (p = HT + 1, i = 1; i <= n; ++i, ++p, ++w) { p->weight = *w; p->parent = 0; p->lchild = 0; p->rchild = 0; } for (; i <= m; ++i, ++p) p->parent = 0; for (i = n + 1; i <= m; ++i){ select(HT, i - 1, s1, s2); HT[s1].parent = HT[s2].parent = i; HT[i].lchild = s1; HT[i].rchild = s2; HT[i].weight = HT[s1].weight + HT[s2].weight; }HC = (HuffmanCode)malloc((n + 1) *sizeof(char*));cd = (char*)malloc(n *sizeof(char)); cd[n - 1] = '\0'; for (i = 1; i <= n; i++) { start = n - 1; for (c = i, f = HT[i].parent; f != 0; c = f, f = HT[f].parent) if (HT[f].lchild == c) cd[--start] = '0'; else cd[--start] = '1'; HC[i] = (char*)malloc((n - start) *sizeof(char)); strcpy(HC[i], &cd[start]); } free(cd); }void Initialization(){ flag = 1; int num; int num2;cout << " The initial hoffman linked list " << endl << "Please enter a number of nodes n:";
cin >> num; n = num; w = (int*)malloc(n *sizeof(int)); z = (char*)malloc(n *sizeof(char)); cout << "\n Please please input" << n << "\n Characters (character type) note: must end to enter:" << endl; char base[2]; for (i = 0; i < n; i++) { cout << " The first " << i + 1 << " characters:" << endl; gets(base); *(z + i) = *base; } for (i = 0; i <= n - 1; i++) { cout << setw(6) << *(z + i); } cout << "\n Please please input" << n << " A weights:" << endl; for (i = 0; i <= n - 1; i++) { cout << endl << " The first " << i + 1 << " A character metric:"; cin >> num2; *(w + i) = num2; } HuffmanCoding(HT, HC, w, n);cout << " Characters corresponding coding for:" << endl; for (i = 1; i <= n; i++) { //cout<<" characters "<<*(z+i-1)<<" coding "; puts(HC[i]); }cout << " Below will hoffman code written to the file " << endl << "...................." << endl; FILE *htmTree; char r[] = { ' ', '\0' }; if ((htmTree = fopen("htmTree.txt", "w")) == NULL) { cout << "can not open file" << endl; return ; } fputs(z, htmTree); for (i = 0; i < n + 1; i++) { fprintf(htmTree, "%6d", *(w + i)); fputs(r, htmTree); } for (i = 1; i <= n; i++) { fputs(HC[i], htmTree); fputs(r, htmTree); } fclose(htmTree); cout << "Characters and corresponding coding has written to the root directory file htmTree.txt中" << endl << endl;}void InputCode(){ //cout<<" Please input your want to code of characters "<<endl; FILE *tobetran; char str[100]; if ((tobetran = fopen("tobetran.txt", "w")) == NULL) { cout << " Cannot open file " << endl; return ; }cout << " Please input your want to code of characters" << endl;
gets(str); fputs(str, tobetran); cout << " Message for success " << endl; fclose(tobetran);}void Encoding(){ cout << "To directory file tobetran.txt the characters of coded" << endl; FILE *tobetran, *codefile; if ((tobetran = fopen("tobetran.txt", "rb")) == NULL) { cout << " Cannot open file " << endl; } if ((codefile = fopen("codefile.txt", "wb")) == NULL) { cout << " Cannot open file " << endl; } char *tran; i = 99; tran = (char*)malloc(100 *sizeof(char)); while (i == 99) { if (fgets(tran, 100, tobetran) == NULL) { cout << " Cannot open file " << endl; break; } for (i = 0; *(tran + i) != '\0'; i++) { for (j = 0; j <= n; j++) { if (*(z + j - 1) == *(tran + i)) { fputs(HC[j], codefile); if (j > n) { cout << " Characters mistake, can't coding!" << endl; break; } } } } }cout << " Coding work" << endl << "Code written to the directory codefile.txt" << endl <<
endl; fclose(tobetran); fclose(codefile); free(tran);}void Decoding(){ cout << "To root directory file codefile.txt the characters of decode" << endl; FILE *codef, *txtfile; if ((txtfile = fopen("Textfile.txt", "w")) == NULL) { cout << " Cannot open file " << endl; } //txtfile=fopen("Textfile.txt","w"); if ((codef = fopen("codefile.txt", "r")) == NULL) { cout << " Cannot open file " << endl; } //codef=fopen("codefile.txt","r"); char *work, *work2, i2; int i4 = 0, i, i3; unsigned long length = 10000; work = (char*)malloc(length *sizeof(char)); fgets(work, length, codef); work2 = (char*)malloc(length *sizeof(char)); i3 = 2 * n - 1; for (i = 0; *(work + i - 1) != '\0'; i++) { i2 = *(work + i); if (HT[i3].lchild == 0) { *(work2 + i4) = *(z + i3 - 1); i4++; i3 = 2 * n - 1; i--; } else if (i2 == '0') i3 = HT[i3].lchild; else if (i2 == '1') i3 = HT[i3].rchild; } *(work2 + i4) = '\0'; fputs(work2, txtfile);
上一个:C++学生管理系统的程序。
下一个:C++题目 很急 在线等