标题:我做的一阶自适应式动态哈夫曼类似的压缩与解压,,求大神来帮我看看我问题
只看楼主
yyh_630
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-12-19
 问题点数:0 回复次数:0 
我做的一阶自适应式动态哈夫曼类似的压缩与解压,,求大神来帮我看看我问题
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>

unsigned long result=0;
unsigned long b=0;
int example=0;
FILE *FT;
FILE *FP;

typedef struct dat{
    char data;
    int r;
    int l;
    int f;
    int weight;
}datalist;

typedef struct li{
    datalist *jied;
    int n;
}list;


int  deep(int n,list *q)
{
    int i,t;
    for(t=n;t!=0;t=q->jied[t].f)
        i++;
    return i;
}

void create1(list *&q)
{
    int i;
    q=(list *)malloc(sizeof(list));
    q->jied=(datalist *)malloc(514*sizeof(datalist));
    for(i=0;i<514;i++)
    {
        q->jied[i].data=-1;
        q->jied[i].r=q->jied[i].l=q->jied[i].weight=q->jied[i].f=0;
    }
    q->n=0;
}

void create2(int t,list *&q)
{
    int i;
    q=(list *)malloc(sizeof(list));
    q->jied=(datalist *)malloc(514*sizeof(datalist));
    for(i=0;i<514;i++)
    {
        q->jied[i].data=-1;
        q->jied[i].r=q->jied[i].l=q->jied[i].weight=q->jied[i].f=0;
    }
    q->jied[0].data=t;
    q->n=0;
}

void insert1(char ch,list *&q)
{
    int t=0,k,num,u,s=0,v;
    datalist *p;
    p=(datalist*)malloc(sizeof(datalist));
    t=q->n;
    q->jied[t].weight++;
    q->jied[t].l=t+2;
    q->jied[t].r=t+1;
    q->jied[t+1].data=ch;
    q->jied[t+1].weight=1;
    q->jied[t+2].f=q->jied[t+1].f=t;
    k=t;
    u=k;
    q->n=q->n+2;
    while(k!=0)
    {   
        if(k!=u)
            q->jied[k].weight++;
        for(v=0;v<k;v++)
        {
            if(deep(k,q)>=deep(v,q))
            if(q->jied[k].weight>q->jied[v].weight)
            {
                *p=q->jied[k];
                q->jied[k]=q->jied[v];
                q->jied[v]=*p;
                num=q->jied[k].f;
                q->jied[k].f=q->jied[v].f;   
                q->jied[v].f=num;
                t=k;
                k=v;
                if(q->jied[k].r!=0)
                    q->jied[q->jied[k].r].f=v;
                if(q->jied[k].l!=0)
                    q->jied[q->jied[k].l].f=v;
                if(q->jied[t].r!=0)
                    q->jied[q->jied[t].r].f=t;
                if(q->jied[t].l!=0)
                    q->jied[q->jied[t].l].f=t;
                break;
            }
        }
        k=q->jied[k].f;
    }   
    if(q->n!=2)
        q->jied[0].weight++;
}

void insert2(char ch,list *&q)
{
    int i,t=0,k,num,s=0,v;
    datalist *p;
    p=(datalist*)malloc(sizeof(datalist));
    for(i=0;i<q->n;i++)
        if(ch==q->jied[i+1].data)
        {
            q->jied[i+1].weight++;
            break;
        }
    k=i+1;
    while(k!=0)
    {   
        if(k!=i+1)
            q->jied[k].weight++;                                 
        for(v=0;v<k;v++)
        {
            if(deep(k,q)>=deep(v,q))
            if(q->jied[k].weight>q->jied[v].weight)
            {
                *p=q->jied[k];
                q->jied[k]=q->jied[v];
                q->jied[v]=*p;
                num=q->jied[k].f;
                q->jied[k].f=q->jied[v].f;   
                q->jied[v].f=num;
                t=k;
                k=v;
                if(q->jied[k].r!=0)
                    q->jied[q->jied[k].r].f=v;
                if(q->jied[k].l!=0)
                    q->jied[q->jied[k].l].f=v;
                if(q->jied[t].r!=0)
                    q->jied[q->jied[t].r].f=t;
                if(q->jied[t].l!=0)
                    q->jied[q->jied[t].l].f=t;
                break;
            }
        }
        k=q->jied[k].f;
    }
    q->jied[0].weight++;        
}



void lr(char ch,list *q)
{
    char lis[1000];
    int i,t;
    //printf("%d\n",b);
    for(i=1;i<514;i++)
    if(ch==q->jied[i+1].data)
    {
            t=i+1;
            break;
    }
    for(i=0;t!=0;t=q->jied[t].f,i++)
        if(t==q->jied[q->jied[t].f].r)
            lis[i]='1';
        else
            lis[i]='0';
    lis[i]='\0';
    t=strlen(lis);
    //printf("%s\n",lis);
    for(i=1;i<=t;i++)
    {
        if(lis[t-i]=='1')
            result =result|(1 << (30-b));
        b++;
        if(b==31)
        {
            fwrite(&result,sizeof(result),1,FT);
            printf("1");
            result=0;
            b=0;
        }
    }
}
/*开始译码*/

void jiem(list *p,char &ch)
{
    int min=1;
    datalist *z;
    if(b==30)
        fread(&result,sizeof(result),1,FP);
    z=&p->jied[0];
    while(1)
    {   
        if(z->r==0&&z->l==0)
        {
            fwrite(&z->data,sizeof(z->data),1,FT);
            ch=z->data;
            break;
        }
        if((result&(min<<b))!=0)
        {
            z=&p->jied[z->r];
            b--;
        }
        else
        {
            z=&p->jied[z->l];
            b--;
        }
        if(b==-1)
        {
            b=30;
            fread(&result,sizeof(result),1,FP);
        }

    }   

}

int main()
{
    int i=0,flag=1,h,max;
    char ch;
    list *q,**w;
    q=NULL;
    create1(q);
    for(i=0;i<256;i++)
    {
        ch=i;
        insert1(ch,q);
    }
    w=NULL;
    w=(list**)malloc(256*sizeof(list *));
    for(h=0;h<256;h++)
    {
        *(w+h)=NULL;
        create2(h,*(w+h));
        for(i=0;i<256;i++)
        {
            ch=i;
            insert1(ch,*(w+h));
        }
    }
    if((FP=fopen("E:\\file1.txt","rb"))==NULL)
    {
        printf("cannot open this file\n");
        exit(0);
    }
    if((FT=fopen("E:\\file2.txt","wb"))==NULL)
    {
        printf("cannot open this file\n");
        exit(0);
    }
    h=0;
    ch=fgetc(FP);
    while(ch!=EOF)
    {
        if(flag%2!=0)
            lr(ch,q);
        else
            lr(ch,*(w+h));
        insert2(ch,q);
        if(flag!=1)
            insert2(ch,*(w+h));
        h=ch;
        ch=fgetc(FP);
        flag++;
    }
    max=flag;
    fclose(FP);
    if(b!=0)
    {
        fwrite(&result,sizeof(result),1,FT);
    }
    b=30;
    fclose(FT);
    /*译码阶段 */
    printf("\n\n\n");
    flag=1;
    q=NULL;
    create1(q);
    for(i=0;i<256;i++)
    {
        ch=i;
        insert1(ch,q);
    }
    w=NULL;
    w=(list**)malloc(256*sizeof(list *));
    for(h=0;h<256;h++)
    {
        *(w+h)=NULL;
        create2(h,*(w+h));
        for(i=0;i<256;i++)
        {
            ch=i;
            insert1(ch,*(w+h));
        }
    }
    if((FP=fopen("E:\\file2.txt","rb"))==NULL)
    {
        printf("cannot open this file\n");
        exit(0);
    }
    if((FT=fopen("E:\\file3.txt","wb"))==NULL)
    {
        printf("cannot open this file\n");
        exit(0);
    }
    int total=0;
    while(flag<max)
    {
        if(flag%2!=0)
            jiem(q,ch);
        else
            jiem(*(w+h),ch);
        insert2(ch,q);
        printf("%c\n",ch);
        printf("%d",total);
        if(flag!=1)
            insert2(ch,*(w+h));
        h=ch;
        flag++;
    }
    fclose(FT);
    fclose(FP);
    return 0;
}



我不知道为什么能进行较小的txt的转压,,但不能进行大的,,,谢谢帮助,,现在很急
搜索更多相关主题的帖子: 压缩 example include return result 
2011-12-19 10:45



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-358109-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.254986 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved