| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 956 人关注过本帖
标题:送20分。。麻烦高手帮忙改错。。。
只看楼主 加入收藏
Vi_No
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2009-5-15
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:7 
送20分。。麻烦高手帮忙改错。。。
应该是蛮白痴的错误。。但是最近大脑故障了。麻烦帮忙看看。

//HuffmanCode

#include <iostream>
#include <string.h>

#define MAXSIZE 15

using namespace std;

typedef struct
{
    unsigned int weight;
    unsigned int parent, lchild, rchild;
}HTNode, *HuffmanTree;

typedef char **HuffmanCode;

void Select(HuffmanTree &HT, int n, int &s1, int &s2)
{
    //选择HT[1..n]中parent为0且weight最小的两个结点
    //序号分别赋值给s1,s2
    int tmp;
    for (int i = 1; i <= n; i++)
        if (HT[i].parent == 0)
        {
            s1 = i;
            break;
        }
    for (; i <= n; i++)
        if (HT[i].parent == 0)
        {
            s2 = i;
            break;
        }
    if (HT[s1].weight > HT[s2].weight)
    {
        tmp = s1;
        s1 = s2;
        s2 = tmp;
    }
    for (; i <= n; i++)
        if (HT[i].parent == 0 && HT[i].weight < HT[s2].weight)
            if (HT[i].weight < HT[s1].weight)
            {
                tmp = s1;
                s1 = i;
                s2 = tmp;
            }
            else
                s2 = i;
}

void HuffmanCoding(HuffmanTree &HT, HuffmanCode &HC, int *w, int n)
{
    //建立哈夫曼树,求哈夫曼编码
    HuffmanTree p;
    char *cd;
    int i, s1, s2, start;
    unsigned int c, f;
    if (n <= 1)
        return;
    int m = 2 * n - 1;

    HT = (HuffmanTree) malloc((m + 1) * sizeof(HTNode));
    for (p = HT, i = 1; i <= n; ++i, ++p, ++w)
    {
        p->weight = *w;
        p->parent = 0;
        p->lchild = 0;
        p->rchild = 0;
    }
    for (; i <= m; ++i, ++p)
    {
        p->weight = 0;
        p->parent = 0;
        p->lchild = 0;
        p->rchild = 0;
    }
    for (i = n + 1; i <= m; ++i)
    {
        Select(HT, i-1, s1, s2);
        HT[s1].parent = i;
        HT[s2].parent = i;
        HT[i].lchild = s1;
        HT[i].rchild = s2;
        HT[i].weight = HT[s1].weight + HT[s2].weight;
    }

    HC = (HuffmanCode) malloc((n+1) * sizeof(char *));
    cd = (char *) malloc(n * sizeof(char));
    cd[n-1] = '\0';
    for (i = 1; i <= n; ++i)
    {
        start = n - 1;
        for (c = i, f = HT[c].parent; f != 0; c = f, f = HT[f].parent)
            if (HT[f].lchild == c)
                cd[--start] = '0';
            else
                cd[--start] = '1';
        HC[i] = (char *) malloc((n - start) * sizeof(char));
        strcpy(HC[i], &cd[start]);
        printf("%s\n", HC[i]);
    }
    free(cd);
}

int main()
{
    HuffmanTree HT;
    HuffmanCode HC;
    int w[MAXSIZE], n;

    cout << "请输入字符数目\n";
    cin >> n;
    cout << "请输入各字符的权值\n";
    for (int i = 0; i < n; i++)
        cin >> w[i];

    HuffmanCoding(HT, HC, w, n);

    return 0;
}
搜索更多相关主题的帖子: 改错 麻烦 
2010-05-02 17:27
LegendofMine
该用户已被删除
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2010-05-02 17:48
qq8801103
Rank: 5Rank: 5
来 自:苏州中科大软件学院
等 级:职业侠客
威 望:1
帖 子:422
专家分:340
注 册:2009-10-8
收藏
得分:0 
楼主总要说下大概的功能吧

Discuz!  
好好学习  天天向上
2010-05-02 21:36
Vi_No
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2009-5-15
收藏
得分:0 
回复 3楼 qq8801103
Huffman编码呀。不是说了么。
2010-05-02 22:05
Vi_No
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2009-5-15
收藏
得分:0 
怎么没人帮忙改改类。。拜托了。。。
2010-05-07 18:13
hzh512
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:6
帖 子:234
专家分:1333
注 册:2009-6-5
收藏
得分:20 
你 select 函数写错了!
下面是正确的源码:
程序代码:
#include <iostream>
#include <string.h>
#define MAXSIZE 15
using namespace std;
typedef struct
{
    unsigned int weight;
    unsigned int parent, lchild, rchild;
}HTNode, *HuffmanTree;
typedef char **HuffmanCode;
void Select(HuffmanTree &HT, int n, int &s1, int &s2);
void HuffmanCoding(HuffmanTree &HT, HuffmanCode &HC, int *w, int n);
int main()
{
    HuffmanTree HT;
    HuffmanCode HC; //typedef char **HuffmanCode
    int w[MAXSIZE]={5,29,7,8,14,23,3,11}, n=8;


 /* cout << "请输入字符数目\n";
    cin >> n;
    cout << "请输入各字符的权值\n";
    for (int i = 0; i < n; i++)

 cin >> w[i];

 */
    HuffmanCoding(HT, HC, w, n);

    return 0;
}

void HuffmanCoding(HuffmanTree &HT, HuffmanCode &HC, int *w, int n)
{
    //建立哈夫曼树,求哈夫曼编码
    HuffmanTree p;
    char *cd;
    int i, s1, s2, start;
    unsigned int c, f;
    if (n <= 1)
        return;
    int m = 2 * n - 1;

    HT = (HuffmanTree) malloc((m + 1) * sizeof(HTNode));

 p=HT;
    for (++p, i = 1; i <= n; ++i, ++p, ++w)
    {
        p->weight = *w;
        p->parent = 0;
        p->lchild = 0;
        p->rchild = 0;
    }
    for (; i <= m; ++i, ++p)
    {
        p->weight = 0;
        p->parent = 0;
        p->lchild = 0;
        p->rchild = 0;
    }
    for (i = n + 1; i <= m; ++i)
    {
        Select(HT, i-1, s1, s2);
        HT[s1].parent = i;
        HT[s2].parent = i;
  if(s1<s2)
  {
   HT[i].lchild = s1;
   HT[i].rchild = s2;
  }
  else
  {
   HT[i].lchild = s2;
   HT[i].rchild = s1;
  }
        HT[i].weight = HT[s1].weight + HT[s2].weight;
    }

    HC = (HuffmanCode) malloc((n+1) * sizeof(char *));
    cd = (char *) malloc(n * sizeof(char));
    cd[n-1] = '\0';
    for (i = 1; i <= n; ++i)
    {
        start = n - 1;
        for (c = i, f = HT[c].parent; f != 0; c = f, f = HT[f].parent)
            if (HT[f].lchild == c)
                cd[--start] = '0';
            else
                cd[--start] = '1';
   HC[i] = (char *) malloc((n - start) * sizeof(char));
   strcpy(HC[i], &cd[start]);
   printf("%s\n", HC[i]);
    }
    free(cd);
}
void Select(HuffmanTree &HT, int n, int &s1, int &s2)
{
    //选择HT[1..n]中parent为0且weight最小的两个结点
    //序号分别赋值给s1,s2
    for (int i = 1; i <= n; i++)

 {
  if (HT[i].parent == 0)
        {
            s1 = i;
            break;
        }

 }

 for (; i <= n; i++)

 {
  if (HT[i].weight < HT[s1].weight && HT[i].parent == 0)
   s1 = i;

 }

 for (i=1; i <= n; i++)

 {
  if (HT[i].parent == 0 && i != s1)
  {
   s2 = i;
   break;
  }

 }

 for (; i <= n; i++)

 {
  if (HT[i].weight < HT[s2].weight && HT[i].parent == 0 && i != s1)
   s2 = i;

 }
}


编程=用几种语言在某个或几个平台上通过抽象思维运用一系列算法来解决现实中问题的手段
2010-05-07 21:57
南国利剑
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:29
帖 子:1165
专家分:3536
注 册:2010-4-12
收藏
得分:0 
呵呵,顶楼上!

南国利剑
2010-05-07 22:02
Vi_No
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2009-5-15
收藏
得分:0 
回复 6楼 hzh512
谢谢。

[ 本帖最后由 Vi_No 于 2010-5-8 13:24 编辑 ]
2010-05-08 13:13
快速回复:送20分。。麻烦高手帮忙改错。。。
数据加载中...
 
   



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

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