回 帖 发 新 帖 刷新版面

主题:帮我改一下程序吧,我写得太烂了



#include <stdio.h> 
#include <malloc.h> 


//下面是码表存储的函数
struct BinTree 
{
    int value1;
    int value2;
    struct BinTree *lchild;
    struct BinTree *rchild;
};


struct BinTree *creat_table(int *lentab,int *codtab,int tabwidth,int tabheight) 
{
    

    int n=0;
    int i,j;
    int len,cod;
    struct BinTree *p,*root;

    

    
    for(j=0;j<tabheight;j++)
    {
        for(i=0;i<tabwidth;i++)
        {
            cod=codtab[i];  //读取一个码字
            len=lentab[i];  //设置len为码字长度

            //设置当前节点为根节点
            if(n==0) 
            {
                root=NULL;  //p为新节点地址,但虚节点地址为NULL
                root=(struct BinTree *)malloc(sizeof(struct BinTree)); //为根节点开辟一个单元
                root->lchild=root->rchild=NULL;
            }
            p=root; //
            

            while(len)  //如果当前码字长度不为零
            {
                int bit;  
                bit=(cod>>(len-1))&0x01;  //取码字的第len位至bit,具体做法是将当前码字
                                                   //右移(len-1)位,把第len位移到最低位,然后与
                n=n+1;  
                                                    //00……0001按位与就可以取到第len位了,此处
                                                  
                //if(n==1)  root=p1;
                //else 
                //{
                    if(bit==0)
                    {
                        
                        if(p->lchild=NULL)  //如果当前节点的左孩子不存在
                        {
                            p->lchild=(struct BinTree *)malloc(sizeof(struct BinTree)); //建立左孩子节点
                            
                            p->value1=p->value2=-1;  //设置左孩子节点的value1和value2,
                                                
                        }
                         p=p->lchild;  //移动当前节点指针到左孩子节点
                        //p->lchild=p->rchild=NULL;
                    }
                    else
                    {
                        
                        if(p->rchild=NULL)  //如果当前节点的右孩子不存在
                        {
                            p->rchild=(struct BinTree *)malloc(sizeof(struct BinTree)); //建立右孩子节点
                            
                            p->value1=p->value2=-1;  //设置右孩子节点的value1和value2,
                                                 
                        }
                        p=p->rchild;  //移动当前节点指针到右孩子节点
                        //p->lchild=p->rchild=NULL;
                    }
                    len-=1;
                //}
                //len-=1;
            }
            
            p->value1=i;
            p->value2=j;
        }
    }
    return root;  

回复列表 (共1个回复)

沙发

int code_from_tab(struct BinTree *proot)
{
    int bit;
    int cod=7;
    int len=6;
    //int a[6]={0,0,0,1,1,1};
    //p=a;
    while(proot->lchild!=NULL||proot->rchild!=NULL)
    {
        
        while(len){
        bit=(cod>>(len-1))&0x01;
        if(bit==0 && proot->lchild!=NULL)
        {
            proot=proot->lchild;
        }
        else if(bit==1 && proot->rchild!=NULL)
        {
            proot=proot->rchild;
        }
        len-=1;
        }
        if(proot->value1!=-1&&proot->value2!=-1)
        {
            int a=proot->value1;
            //int c=proot->rchild;
            
            //int a[2]={b,c};
            return a;
        }
        else
            return 1;
    }
}

main()
{
    struct BinTree 
    {
        int value1;
        int value2;
        struct BinTree *lchild;
        struct BinTree *rchild;
    };
    int *ct,*lt,res;


    int lentab[2][2] = 
    {
        { 2, 6/*, 6, 6, 6 */},          
        { 0, 1/*, 6, 7, 8 */}, 
        //{ 0, 0, 3, 7, 8 }, 
        //{ 0, 0, 0, 6, 7 },
    };
    int codtab[2][2] = 
    {
        { 1, 7/*, 4, 3, 2*/ },
        { 0, 1/*, 6, 3, 3*/ },
        //{ 0, 0, 1, 2, 2 },
        //{ 0, 0, 0, 5, 0 },
    }; 

    struct BinTree *ppp;
    
    lt=&lentab[0][0];
    ct=&codtab[0][0];

    
    ppp=creat_table(lt,ct,2,2);
    res=code_from_tab(ppp);

    printf("%d",res);
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册