⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 huffman.cpp

📁 老问题
💻 CPP
📖 第 1 页 / 共 2 页
字号:
                    ptr=tree;                    //指针复位
                    index=0;
                }
            }
            if(ptr->rchild&&ptr->rchild->mark==0)
            {
                ptr=ptr->rchild;
                code[index++]='1';                //右支路编码为1
            }
            if(!ptr->lchild&&!ptr->rchild)        //如果没有左右孩子,即叶子结点
            {
                ptr->mark=1;
                code[index++]='\0';
                codedictionary[k].ch=ptr->ch;     //给字典赋字符值
                for(index=0;code[index]!='\0';index++)
                    codedictionary[k].code[index]=code[index];//给字典赋码值
                codedictionary[k].code[index]='\0';
                k++;
                ptr=tree;
                index=0;
            }
            if(ptr->lchild->mark==1&&ptr->rchild->mark==1)//如果左右孩子都已标记
            {
                ptr->mark=1;
                ptr=tree;
                index=0;
            }
        }
    }
    printf("\n");
}

/*
 *解码,即将huffman编码转化为字符串
 */
void decodeHTree(LinkTree tree , char *code , char *filestring)
{    
    int i=0,j=0,k=0;
    char *char0_1;
    LinkTree ptr=tree;
    char0_1=(char *)malloc(MAX_SINGLECODE_LEN);  //此数组用于统计输入的0-1序列
    
    printf("解码后的字符串如下:\n");
    for(j=0,ptr=tree;code[i]!='\0'&&ptr->lchild&&ptr->rchild;j=0,ptr=tree)
    {
        for(j=0;code[i]!='\0'&&ptr->lchild&&ptr->rchild;j++,i++)
        {
            if(code[i]=='0')
            {    
                ptr=ptr->lchild;
                char0_1[j]='0';  
            }
            if(code[i]=='1')
            {
                ptr=ptr->rchild;
                char0_1[j]='1';
            }
        }                        
        if(!ptr->lchild&&!ptr->rchild)
        {
            printf("%c",ptr->ch);                //显示解压后的字符
            filestring[k++]=ptr->ch;             //将字符逐一保存到字符串里
        }
        if(code[i]=='\0'&&ptr->lchild&&ptr->rchild)
        {    
            char0_1[j]='\0';
            printf("\n没有与最后的几个0-1序列:%s相匹配的字符!\n",char0_1);
            return;
        }
    }
    printf("\n\n");
    filestring[k]='\0';
    free(char0_1);
}

/*
 *释放哈夫曼树所占用的空间
 */
void deleteNode(LinkTree tree)
{    
    LinkTree ptr=tree;
    if(ptr)
    {    
        deleteNode(ptr->lchild);
        deleteNode(ptr->rchild);
        free(ptr);
    }
}

/*
 *将整个字符串转化为0-1的字符串
 */
void compressString(char *string,CodeDictionary *codedictionary,char *codestring)
{
    int i=0,j=0,k=0,m;

    while(string[i])                             //整个文件字符串没结束时
    {
        while(string[i]!=codedictionary[j].ch&&j<MAX_WORDS)
                                                 //找与对应字符相同的字符
            j++;
        if(string[i]==codedictionary[j].ch)      //如果找到与对应字符相同的字符
            for(m=0;codedictionary[j].code[m];m++,k++)
                codestring[k]=codedictionary[j].code[m];
        j=0;                                     //字典复位
        i++;
    }
    codestring[k]='\0';
}

/*
 *把指定文件读到字符串中
 */
void readFile(char *string)
{
    FILE *fp;
    int  i;
    char ch;                                     //记录读入的字符
    char path[PATH_LEN];                         //文本文件的读路径
    printf("请输入要进行编码的文件:");
    gets(path);
    while((fp=fopen(strcat(path,".txt"),"r"))==NULL)//用字符串拼接函数自动添加扩展名 
    {
        printf("\n错误!!!无法读取源文件!\n");
        printf("\n请重新输入要进行编码的文件:");
        gets(path);
    }

    ch=fgetc(fp);
    for(i=0;ch!=EOF;i++)
    {
        string[i]=ch;
        ch=fgetc(fp);
    }
    string[i]='\0';
    fclose(fp);
}

/*
 *保存编码后的解码树和字符串
 */
void writeCode(LinkTree tree,char *string)
{
    FILE *fp;
    int  i;
    int  weight;                                 //记录写入的权值
    char ch;                                     //记录写入的字符
    LinkTree p;
    char path[PATH_LEN];                         //0-1码文件的写路径

    printf("请输入保存编码的文件:");
    gets(path);
    if((fp=fopen(strcat(path,".txt"),"w+"))==NULL)
    {
        printf("\n错误!!!写文件出错!\n");
        getch();
        return;
    }
    p=tree->next;

    /*解码树部分写入文件前部分*/
    do
    {
        ch=p->ch;
        weight=p->weight;
        fprintf(fp,"%c%d",ch,weight);
        p=p->next;
    }while(p);
    fprintf(fp,"%c%d",'$',END_TREE);

    fseek(fp,sizeof(char),1);                    //空出区分位
    
    /*字符串的Huffman编码写入文件后部分*/
    for(i=0;string[i];i++)
    {
        ch=string[i];
        fputc(ch,fp);
    }
    printf("\n文件编码成功!任意键返回\n");
    getch();
    fclose(fp);
}

/*
 *读取编码后的0-1字符串
 */
void readCode(LinkTree tree,char *string)
{

    FILE *fp;
    int  i;
    int  weight;                                 //记录读入的权值
    char ch;                                     //记录读入的字符
    LinkTree ptr,beforeptr;
    char path[PATH_LEN];                         //0-1码文件的读路径

    printf("请输入要进行解码的文件:");
    gets(path);
    while((fp=fopen(strcat(path,".txt"),"r"))==NULL)//用字符串拼接函数自动添加扩展名 
    {
        printf("\n错误!!!无法读取源文件!\n");
        printf("\n请重新输入要进行解码的文件:");
        gets(path);
    }
    beforeptr=tree;

    /*从文件前部分读出解码树*/
    fscanf(fp,"%c%d",&ch,&weight);
    while(weight!=END_TREE)
    {
        if((ptr=(LinkTree)malloc(sizeof(HTNode)))==NULL)
        {
            printf("内存分配失败!");
            getch();
            exit(1);                             //错误出口
        }
        ptr->ch=ch;
        ptr->weight=weight;
        ptr->lchild=NULL;
        ptr->rchild=NULL;
        ptr->parent=NULL;
        ptr->mark=0;
        beforeptr->next=ptr;
        beforeptr=ptr;
        fscanf(fp,"%c%d",&ch,&weight);
    }
    beforeptr->next=NULL;

    fseek(fp,sizeof(char),1);                    //文件指针定位
    
    /*从文件后部分读出0-1码*/
    ch=fgetc(fp);
    for(i=0;ch!=EOF;i++)
    {
        string[i]=ch;
        ch=fgetc(fp);
    }
    string[i]='\0';
    fclose(fp);
}

/*
 *保存解码后的文件
 */
void writeFile(char *string)
{
    FILE *fp;
    char ch;                                     //记录写入的字符
    int  i;
    char path[PATH_LEN];                         //文本文件的写路径

    printf("请输入保存以上字符串的文件:");
    gets(path);
    if((fp=fopen(strcat(path,".txt"),"w+"))==NULL)//用字符串拼接函数自动添加扩展名 
    {
        printf("\n错误!!!文件写入失败!\n");
        getch();
        return;
    }

    for(i=0;string[i];i++)
    {
        ch=string[i];
        fputc(ch,fp);
    }
    printf("\n文件解码成功!任意键返回\n");
    getch();
    fclose(fp);
}

/*
 *显示主菜单
 */
void menu()
{
    printf("\n\n\n\n\n\n\n");
    printf("\n\t\t\t\t请选择运行方式:\n");
    printf("\n\t\t\t\t  (c)文件编码\n");    
    printf("\n\t\t\t\t  (d)文件解码\n");
    printf("\n\t\t\t\t  (q)退    出\n");
    printf("\n\n\n\n\n\n\n\n\n");

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -