# Demo entry 6781880

haff

Submitted by anonymous on Jan 05, 2019 at 06:16
Language: C++. Code size: 4.9 kB.

#include<iostream>
#include<string>
//#include<windows.h>
using namespace std;
struct Node//定义一个节点struct
{
double weight;//权重
string ch;
string code;
int lchild, rchild, parent;
};
void Select(Node huffTree[], int *a, int *b, int n)//找权值最小的两个a和b
{
int i;
double weight = 0; //找最小的数
for (i = 0; i <n; i++)
{
if (huffTree[i].parent != -1)     //判断节点是否已经选过
continue;
else
{
if (weight == 0)
{
weight = huffTree[i].weight;
*a = i;
}
else
{
if (huffTree[i].weight < weight)//找到权重最小的，存到指针a指向的域
{
weight = huffTree[i].weight;
*a = i;
}
}
}
}
weight = 0; //找第二小的数
for (i = 0; i < n; i++)
{
if (huffTree[i].parent != -1 || (i == *a))//排除已选过的数
continue;
else
{
if (weight == 0)
{
weight = huffTree[i].weight;
*b = i;
}
else
{
if (huffTree[i].weight  < weight)//找到权重第二小的，存到指针b指向的域
{
weight = huffTree[i].weight;
*b = i;
}
}
}
}
int temp;
if (huffTree[*a].lchild < huffTree[*b].lchild)  //比较ab指的域的值，小的数放左边
{
temp = *a;
*a = *b;
*b = temp;
}
}
void Huff_Tree(Node huffTree[], int w[], string ch[], int n)
{
for (int i = 0; i < 2 * n - 1; i++) //初始过程
{
huffTree[i].parent = -1;
huffTree[i].lchild = -1;
huffTree[i].rchild = -1;
huffTree[i].code = "";
}
for (int i = 0; i < n; i++)
{
huffTree[i].weight = w[i];
huffTree[i].ch = ch[i];
}
for (int k = n; k < 2 * n - 1; k++)
{
int i1 = 0;
int i2 = 0;
Select(huffTree, &i1, &i2, k); //将i1，i2节点合成节点k
huffTree[i1].parent = k;
huffTree[i2].parent = k;
huffTree[k].weight = huffTree[i1].weight + huffTree[i2].weight;
huffTree[k].lchild = i1;
huffTree[k].rchild = i2;
}
}
void Huff_Code(Node huffTree[], int n)
{
int i, j, k;
string s = "";
for (i = 0; i < n; i++)
{
s = "";
j = i;
while (huffTree[j].parent != -1) //从叶子往上找到根节点
{
k = huffTree[j].parent;
if (j == huffTree[k].lchild) //如果是根的左孩子，则记为0
{
s = s + "0";
}
else
{
s = s + "1";
}
j = huffTree[j].parent;
}
cout << "字符 " << huffTree[i].ch << " 的编码：";
for (int l = s.size() - 1; l >= 0; l--)
{
cout << s[l];
huffTree[i].code += s[l]; //保存编码
}
cout << endl;
}
}
string Huff_Decode(Node huffTree[], int n,string s)
{
cout << "解码后为：";
string temp = "",str="";//str保存解码后的字符串
for (int i = 0; i < s.size(); i++)
{
temp = temp + s[i];
for (int j = 0; j < n; j++)
{
if (temp == huffTree[j].code)
{
str=str+ huffTree[j].ch;
temp = "";
break;
}
else if (i == s.size()-1&&j==n-1&&temp!="")//全部遍历后没有
{
str= "解码错误！";
}
}
}
return str;
}
int main()
{
string str;
char temp;
cout<<"输入字符串,按回车键结束输入"<<endl;
while((temp=cin.get())!='\n')
{
str +=temp;
}
const int LEN =str.length();
char* dest = new char[LEN];//根据输入字符串的长度，创建字符数组
for(int i = 0;i<LEN;i++) //将字符串保存到字符数组中
{
dest[i]=str[i];
}//cout<<LEN<<endl;/*for(int i=0;i<LEN;i++)cout<<dest[i]<<" ";*/
cout<<endl;
char sample[10];
int flu[10]={1,1,1,1,1,1,1,1,1,1};
int num=0,ok=1;
for(int m=0;m<LEN;m++)
{
for(int j=0;j<10;j++)
{
if(dest[m]==sample[j])
{
flu[j]++;ok=0;break;
}
}
if(ok==0)
{ok=1;
continue;}
else
{
sample[num]=dest[m];
num++;}
}
/*for(int m=0;m<4;m++){ cout<<sample[m]<<" "<<flu[m]<<endl;}*/
//编码过程
int n=num;
Node huffTree[2 * n];
string str1[num];
int w[num];
for(int i=0;i<num;i++)
{str1[i]=sample[i];w[i]=flu[i];}
Huff_Tree(huffTree, w, str1, n);
Huff_Code(huffTree, n);
//解码过程
string s;
cout << "输入编码：";
cin >> s;
cout << Huff_Decode(huffTree, n, s)<< endl;;
system("pause");
return 0;
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.