| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 685 人关注过本帖
标题:谁帮忙改一下这个程序谢了
只看楼主 加入收藏
niulang
Rank: 1
来 自:河北-石家庄
等 级:新手上路
帖 子:75
专家分:0
注 册:2009-4-16
结帖率:100%
收藏
 问题点数:0 回复次数:2 
谁帮忙改一下这个程序谢了
#include<iostream>
using namespace std;
#define maxbit 10
#define maxvalue 1000
#define maxleaf 4
#define maxnode maxleaf*2-1
int n;
typedef struct
{
    int weight;
    int parent;
    int lchild;
    int rchild;
}hnodetype;
typedef struct
{
    int bit[maxbit];
    int start;
}hcodetype;
hnodetype *haffmantree()//建立哈夫曼树
{
    hnodetype huffnode[maxnode];
    int i,j,m1,m2,x1,x2;
    cout<<"请输入叶子结点数:";
    cin>>n;
    for(i=0;i<2*n-1;i++)
    {
        huffnode[i].weight=0;
        huffnode[i].parent=-1;
        huffnode[i].lchild=-1;
        huffnode[i].rchild=-1;
    }
    cout<<"请输入各叶子结点的权重值:";
    for(i=0;i<n;i++)
        cin>>huffnode[i].weight;
    for(i=0;i<n-1;i++)
    {
        m1=m2=maxvalue;
        x1=x2=0;
        for(j=0;j<n+i;j++)
        {
            if(huffnode[j].parent==-1&&huffnode[j].weight<m1)
            {
                m2=m1;
                x2=x1;
                m1=huffnode[j].weight;
                x1=j;
            }
            else
              if(huffnode[j].parent==-1&&huffnode[j].weight<m2)
              {
                 m2=huffnode[j].weight;
                 x2=j;
              }
        }
        huffnode[x1].parent=n+1;
        huffnode[x2].parent=n+1;
        huffnode[n+1].weight=huffnode[x1].weight+huffnode[x2].weight;
        huffnode[n+1].lchild=x1;
        huffnode[n+2].rchild=x2;
    }
    return huffnode;
}
void haffmancode()
{
    hnodetype *node;
    hcodetype huffcode[maxleaf],cd;
    int i,j,c,p;
    node=haffmantree();
    for(i=0;i<n;i++)
    {
        cd.start=n-1;
        c=i;
        p=node[c].parent;
        while(p!=-1)
        {
            if(node[p].lchild==c)
                cd.bit[cd.start]=0;
            else
                cd.bit[cd.start]=1;
            cd.start--;
            c=p;
            p=node[c].parent;
        }
        for(j=cd.start;j<n;j++)
            huffcode[i].bit[j]=cd.bit[j];
        huffcode[i].start=cd.start;
    }
    for(i=0;i<n;i++)
    {
        for(j=huffcode[i].start+1;j<n;j++)
            cout<<huffcode[i].bit[j];
        cout<<endl;
    }
}
int main()
{
    haffmancode();
    return 0;
}




我就是想实现哈夫曼编码,程序似乎没什么问题,但就是运行不正确……那位高手帮一下……谢谢了
2009-11-25 08:25
zhuyunshen
Rank: 6Rank: 6
等 级:侠之大者
威 望:2
帖 子:212
专家分:455
注 册:2009-11-22
收藏
得分:0 
#include<iostream>
using namespace std;
#define maxbit 10
#define maxvalue 1000
#define maxleaf 4
#define maxnode maxleaf*2-1
int n;
typedef struct
{
    int weight;
    int parent;
    int lchild;
    int rchild;
}hnodetype;
typedef struct
{
    int bit[maxbit];
    int start;
}hcodetype;
hnodetype *haffmantree()//建立哈夫曼树
{
    hnodetype huffnode[maxnode];
    int i,j,m1,m2,x1,x2;
    cout<<"请输入叶子结点数:";
    cin>>n;
    for(i=0;i<2*n-1;i++)
    {
        huffnode[i].weight=0;
        huffnode[i].parent=-1;
        huffnode[i].lchild=-1;
        huffnode[i].rchild=-1;
    }
    cout<<"请输入各叶子结点的权重值";
    for(i=0;i<n;i++)
        cin>>huffnode[i].weight;
    for(i=0;i<n-1;i++)
    {
        m1=m2=maxvalue;
        x1=x2=0;
        for(j=0;j<n+i;j++)
        {
            if(huffnode[j].parent==-1&&huffnode[j].weight<m1)
            {
                m2=m1;
                x2=x1;
                m1=huffnode[j].weight;
                x1=j;
            }
            else
              if(huffnode[j].parent==-1&&huffnode[j].weight<m2)
              {
                 m2=huffnode[j].weight;
                 x2=j;
              }
        }
        huffnode[x1].parent=n+1;
        huffnode[x2].parent=n+1;
        huffnode[n+1].weight=huffnode[x1].weight+huffnode[x2].weight;
        huffnode[n+1].lchild=x1;
        huffnode[n+2].rchild=x2;
    }
    return huffnode;
}
void haffmancode()
{
    hnodetype *node;
    hcodetype huffcode[maxleaf],cd;
    int i,j,c,p;
    node=haffmantree();
    for(i=0;i<n;i++)
    {
        cd.start=n-1;
        c=i;
        p=node[c].parent;
        while(p!=-1)
        {
            if(node[p].lchild==c)
                cd.bit[cd.start]=0;
            else
                cd.bit[cd.start]=1;
            cd.start--;
            c=p;
            p=node[c].parent;
        }
        for(j=cd.start;j<n;j++)
            huffcode[i].bit[j]=cd.bit[j];
        huffcode[i].start=cd.start;
    }
    for(i=0;i<n;i++)
    {
        for(j=huffcode[i].start+1;j<n;j++)
            cout<<huffcode[i].bit[j];
        cout<<endl;
    }
}
int main()
{
    haffmancode();
    return 0;
}
2009-11-25 08:47
niulang
Rank: 1
来 自:河北-石家庄
等 级:新手上路
帖 子:75
专家分:0
注 册:2009-4-16
收藏
得分:0 
回复 2楼 zhuyunshen
你什么意思啊,一点没改就回复我

牛郎不爱织女
2009-11-25 08:56
快速回复:谁帮忙改一下这个程序谢了
数据加载中...
 
   



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

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