正己-Bryant 2018-11-11 06:44 采纳率: 0%
浏览 528

c语言求赫夫曼树的,求教大佬strcpy_s那里应该如何修改呀?

#include "stdafx.h"
#include "malloc.h"
#include"string.h"
typedef struct
{
int weight;
int parent, lchild, rchild;
}htnode,*huffmantree;

typedef char * *huffmancode;

void select(huffmantree &ht, int n, int *s1, int *s2)
{
int i = 1;
huffmantree p;
p = ht;
*s1 = 1;
*s2 = 2;
int s3, s4;
s3 = p->weight;
s4 = (p++)->weight;
if ((s4) < (s3))
{
s4 = s3;
s3 = p->weight;
}
p++;
while (i <= n)
{
if ((p->parent) != 0)
{
++p;
++i;
}
else
{
if (p->weight > s4)
{
p++;
i++;
}
else
{
*s2 = i;
p++;
i++;
}
}
}
}

void huffmancoding(huffmantree &ht, huffmancode &hc, int w, int n)
{
if (n < 1) return;
int m;
m = 2 * n - 1;
ht = (huffmantree)malloc((m + 1) * sizeof(htnode));
huffmantree p;
int i;
int s2;
int s1;
for (p = ht, i = 1; i <= n; ++i, ++p, ++w) *p = { *w,0,0,0 };
for (; i <= m; ++i, ++p) *p = { 0,0,0,0 };
for (i = n + 1; i <= m; ++i)
{
select(ht, n, &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
));
char cd;
cd = (char
)malloc(n * sizeof(char));
cd[n-1]= '\0';
i = 1;
for (; i <= n; ++i)
{
int start;
start = n - 1;
int c,f;
for (c = i, f = ht[i].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_s(hc[i], cd+start);
}
}
int main()
{
void select(huffmantree &ht, int n, int *s1, int *s2);
void huffmancoding(huffmantree &ht, huffmancode &hc, int *w, int n);
int n;
scanf_s("%d", &n);
int w[100];
int r = 0;
for (r = 0; r <= n - 1; r++)
scanf_s("%d", w[r]);
huffmantree ht;
huffmancode hc;
huffmancoding(ht, hc, w, n);
for (r = 1; r <= n; r++)
printf("%d", hc[r]);
return 0;
}

  • 写回答

1条回答 默认 最新

  • devmiao 2018-11-11 16:14
    关注
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?