Ranshair Ranshair
关注数: 3 粉丝数: 0 发帖数: 165 关注贴吧数: 2
非常急!~求高手相助一下,哈弗曼编码问题。 以下程序,希望高手能帮改一下,变成输入数列,然后输出编码的程序,而不是读取文件。最后输出权值,和WPL,谢谢!~ #include <stdio.h> #include <string.h> #include <malloc.h> #include <stdlib.h> #include <conio.h> #define NN 10000 #define M 2*NN-1 #define IN 0 #define OUT 1 #define MAX_INT 32767 #define PRINTLN printf("\n\n\n\n\n\n\n\n"); typedef struct { int wi; char c; int tag; int parent, lchild, rchild; }huffmtype; typedef char* encodetype[NN+1]; typedef struct { char c; int times; }codetype; void PRINT() {      PRINTLN; printf("\t\t\t      Huffman编/译码器\n"); printf("\t\t\t    ====================\n"); printf("\t\t\t    1.编码 2.译码 3.退出\n\n"); printf("\t\t\t    >>选择:"); } FILE* OpenFile(char filename[20], char type[3]) { FILE *fp = NULL; if((fp = fopen(filename, type)) == NULL) exit(1); return fp; } int ReadCode(codetype* code, FILE* fp) { char c;//保存某次从文件中读入字符- int n = 1;//记录首次出现字符在数组中的下标- int i;//循环变量- int cnt = 1; int tag;//标志某次读入字符是否为首次出现字符,tag=1表示是首次出现;tag=0表示本次读入字符为已有字符 while((c = fgetc(fp)) != EOF)//当文件没有结束时读入字符- {    //从fp指向文件中读取字符,存入字符变量c中-    tag = 1;//假设本次读取字符为首次出现字符-    for(i = 1; i < n; i++)    {     if(c == code[i].c)//如果本次读入字符为存储在下标为i已有字符-     {      code[i].times++;//权值加1-      tag = 0;//标记本字符为已有字符-      break;//在已有数组元素中找到本次读入字符,结束for(;;)循环-     }    }       if(tag)//当本字符为首次出现字符时-    {     code[n].c = c;//把改字符存入n指向的数组单元中-     code[n].times = 1;//记字符出现次数为1-     n++;//n指向下一个数组地址-    } } return n - 1;//返回文件中字符集合的元素个数- } void InitHuffmanTree(huffmtype* huffmtree, int real_n, int real_m)//初始化- { int i;       for(i = real_n; i <= real_m; i++) {    huffmtree[i].wi = 0;    huffmtree[i].c = 0;    huffmtree[i].tag = IN;    huffmtree[i].lchild = huffmtree[i].rchild = huffmtree[i].parent = 0; } } void ReadDataWeight_Init(huffmtype* huffmtree, codetype* code, int real_n)//获取权值及数值域值- { int i;       for(i = 1; i <= real_n; i++)//- {    huffmtree[i].wi = code[i].times;    huffmtree[i].c = code[i].c;    huffmtree[i].tag = IN;    huffmtree[i].lchild = huffmtree[i].rchild = huffmtree[i].parent = 0;
1 下一页