大神能不能帮我看看我这个那里出了BUG?

Craftsman98 2017-09-17 11:48:56
我想要输出huffman编码:
#include<iostream>
#include <string>
using namespace std;

struct huffTree
{
int parent;
int lchild;
int rchild;
int weight;
string flag;
};
struct Lowest_node
{
char ch;
int ch_num;
};

void coding(int length, huffTree tree[], int n, int &a, int &b)
{
int i;
int r, s;
r = s = length;
for (i = 0; i < n; i++)
{
if ((tree[i].weight < r) && (tree[i].parent == -1))
{
r = tree[i].weight;
a = i;
}
}
for (i = 0; i < n; i++)
{
if ((tree[i].weight < s) && (i != a) && (tree[i].parent == -1))
{
s = tree[i].weight;
b = i;
}
}
}

void frequency(string str)
{
int length = str.length();
Lowest_node *node = new Lowest_node[length];

int i, j;
for (i = 0; i < length; i++)
node[i].ch_num = 0;

int char_type_num = 0;
for (i = 0; i < length; i++)
{
for (j = 0; j < char_type_num; j++)
if (str[i] == node[j].ch || (node[j].ch >= 'a'&&node[j].ch <= 'z'&&str[i] + 32 == node[j].ch))
break;
if (j < char_type_num)
node[j].ch_num++;
else
{
if (str[i] >= 'A'&&str[i] <= 'Z')
node[j].ch = str[i] + 32;
else
node[j].ch = str[i];
node[j].ch_num++;
char_type_num++;
}
}
for (i = 0; i < char_type_num; i++)
{
for (j = i; j < char_type_num; i++)
{
if (node[j].ch_num < node[j + 1].ch_num)
{
int temp;
char ch_temp;
temp = node[j].ch_num;
ch_temp = node[j].ch;
node[j].ch_num = node[j + 1].ch_num;
node[j].ch = node[j + 1].ch;
node[j + 1].ch_num = temp;
node[j + 1].ch = ch_temp;
}
}
}
for (i = 0; i < char_type_num; i++)
cout << "字符" << node[i].ch << "出现了" << node[i].ch_num << "次" << endl;
huffTree *huff = new huffTree[2 * char_type_num - 1];
huffTree temp;
string *code = new string[2 * char_type_num - 1];

for (i = 0; i < 2 * char_type_num - 1; i++)
{
huff[i].lchild = -1;
huff[i].parent = -1;
huff[i].rchild = -1;
huff[i].flag = -1;
}
for (j = 0; j < char_type_num; j++)
{
huff[j].weight = node[j].ch_num;
}
int min1, min2;
for (int k = char_type_num; k < 2 * char_type_num - 1; k++)
{
coding(length, huff, k, min1, min2);
huff[min1].parent = k;
huff[min2].parent = k;
huff[min1].flag = "0";
huff[min2].flag = "1";
huff[k].lchild = min1;
huff[k].rchild = min2;
huff[k].weight = huff[min1].weight + huff[min2].weight;
}
for (int i = 0; i < char_type_num; i++)
{
temp = huff[i];
while (1)
{
code[i] = temp.flag + code[i];
temp = huff[temp.parent];
if (temp.parent == -1)
break;
}
}
cout << "字符串的每个字符huffman编码为:" << endl;
for (i = 0; i < char_type_num; i++)
cout << node[i].ch << " " << code[i] << endl;

cout << "整个字符串的huffman编码为:" << endl;
for (i = 0; i < length; i++)
{ //S?
for (j = 0; j < char_type_num; j++)
{
if (str[i] == node[j].ch)
cout << code[j];
}
}
delete[] node;
node = NULL;
delete[] huff;
huff = NULL;
delete[] code;
code = NULL;
}

int main()
{
int length = 0;
string str;
cout << "请输入一个字符串:";
cin >> str;
frequency(str);

return 0;
}

输入字符串以后没有反应,不能实现我想要的功能。
...全文
364 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
似缘非缘 2017-09-26
  • 打赏
  • 举报
回复
程序出现死循环。
#include<iostream>
#include <string>
using namespace std;

struct huffTree
{
int parent;
int lchild;
int rchild;
int weight;
string flag;
};
struct Lowest_node
{
char ch;
int ch_num;
};

void coding(int length, huffTree tree[], int n, int &a, int &b)
{
int i;
int r, s;
r = s = length;
for (i = 0; i < n; i++)
{
if ((tree[i].weight < r) && (tree[i].parent == -1))
{
r = tree[i].weight;
a = i;
}
}
for (i = 0; i < n; i++)
{
if ((tree[i].weight < s) && (i != a) && (tree[i].parent == -1))
{
s = tree[i].weight;
b = i;
}
}
}

void frequency(string str)
{
int length = str.length();
Lowest_node *node = new Lowest_node[length];

int i, j;
for (i = 0; i < length; i++)
node[i].ch_num = 0;

int char_type_num = 0;
for (i = 0; i < length; i++)
{
for (j = 0; j < char_type_num; j++)
if (str[i] == node[j].ch || (node[j].ch >= 'a'&&node[j].ch <= 'z'&&str[i] + 32 == node[j].ch))
break;
if (j < char_type_num)
node[j].ch_num++;
else
{
if (str[i] >= 'A'&&str[i] <= 'Z')
node[j].ch = str[i] + 32;
else
node[j].ch = str[i];
node[j].ch_num++;
char_type_num++;
}
}
for (i = 0; i < char_type_num; i++)
{
// 发生死循环,将j++误写为i++
for (j = i; j < char_type_num; j++)
{
if (node[j].ch_num < node[j + 1].ch_num)
{
int temp;
char ch_temp;
temp = node[j].ch_num;
ch_temp = node[j].ch;
node[j].ch_num = node[j + 1].ch_num;
node[j].ch = node[j + 1].ch;
node[j + 1].ch_num = temp;
node[j + 1].ch = ch_temp;
}
}
}
for (i = 0; i < char_type_num; i++)
cout << "字符" << node[i].ch << "出现了" << node[i].ch_num << "次" << endl;
huffTree *huff = new huffTree[2 * char_type_num - 1];
huffTree temp;
string *code = new string[2 * char_type_num - 1];

for (i = 0; i < 2 * char_type_num - 1; i++)
{
huff[i].lchild = -1;
huff[i].parent = -1;
huff[i].rchild = -1;
huff[i].flag = -1;
}
for (j = 0; j < char_type_num; j++)
{
huff[j].weight = node[j].ch_num;
}
int min1, min2;
for (int k = char_type_num; k < 2 * char_type_num - 1; k++)
{
coding(length, huff, k, min1, min2);
huff[min1].parent = k;
huff[min2].parent = k;
huff[min1].flag = "0";
huff[min2].flag = "1";
huff[k].lchild = min1;
huff[k].rchild = min2;
huff[k].weight = huff[min1].weight + huff[min2].weight;
}
for (int i = 0; i < char_type_num; i++)
{
temp = huff[i];
while (1)
{
code[i] = temp.flag + code[i];
temp = huff[temp.parent];
if (temp.parent == -1)
break;
}
}
cout << "字符串的每个字符huffman编码为:" << endl;
for (i = 0; i < char_type_num; i++)
cout << node[i].ch << " " << code[i] << endl;

cout << "整个字符串的huffman编码为:" << endl;
for (i = 0; i < length; i++)
{ //S?
for (j = 0; j < char_type_num; j++)
{
if (str[i] == node[j].ch)
cout << code[j];
}
}
delete[] node;
node = NULL;
delete[] huff;
huff = NULL;
delete[] code;
code = NULL;
}

int main()
{
int length = 0;
string str;
cout << "请输入一个字符串:";
cin >> str;
frequency(str);

cin >> length;
return 0;
}

附上结果:
赵4老师 2017-09-18
  • 打赏
  • 举报
回复
百度搜相关关键字。
paschen 2017-09-18
  • 打赏
  • 举报
回复
单步跟踪程序运行,看是哪一步结果与你期望的不一致,然后分析原因

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧