#include <iostream>
#include <string>
#define MAX 100
using namespace std;
class HTNode
{
public:
int weight;
string data;
int f;
HTNode* father;
HTNode* lchild;
HTNode* rchild;
string code;
HTNode()
{
weight=0;
data='0';
code='0';
f=0;
father=NULL;
lchild=NULL;
rchild=NULL;
}
void SetChild (HTNode* lchild, HTNode* rchild) {
this->lchild = lchild;
this->rchild = rchild;
}
void SetF(int f) {
this->f = f;
}
void SetFather(HTNode* father) {
this->father = father;
}
};
void SelectMin(HTNode*ht[], int*a1,int*a2)
{
int min1=0;
int min2=0;
for(int i=0;i<2*MAX-1;i++)
{
if(ht[min1]->weight>ht[i]->weight)
min1=i;
}
min2=min1+1;
for(int i=0;i<2*MAX-1;i++)
{
if(ht[min2]->weight>ht[i]->weight)
min2=i;
}
*a1=min1;
*a2=min2;
}
void Printtree(HTNode* root, int n) {
if (root==NULL)
return;
for (int i=0;i<n;i++) {
cout<<" ";
}
if (root->f==1) {
cout<<"|_";
} else {
cout<<" ";
}
cout<<root->data<<endl;
Printtree(root->lchild, n+2);
Printtree(root->rchild, n+2);
}
void Bianma(HTNode*ht[]) {
HTNode* curr;
string* code = new string[MAX];
for (int i = 0; i < MAX ; i++) {
code[i] = '2';
}
for (int i = 0; i < MAX; i++) {
int j = 1;
cout<<ht[i]->data<<"的哈弗曼编码为:";
curr = ht[i];
while (curr->f==1) {
curr=curr->father;
if (curr->f==1){
code[j++]=curr->code;
}
}
code[0] = ht[i]->code;
string c[j];
int count = 0;
for (int l = j-1; l >=0; l--) {
if (code[l]!= '2') {
c[count++] = code[l];
}
}
for (int x = 0; x < j; x++) {
ht[i]->code += c[x];
}
cout<<ht[i]->code<<endl;
cout<<endl;
}
}
void FanYi(HTNode* ht[]) {
string code;
string test = "";
HTNode* curr;
HTNode* root;
cout<<"请输入编码:"<<endl;
cin>>code;
int n = code.length();
string* cs = new string[n];
code.copy(cs, n, 0);
for (int i = 0; i < MAX*2-1; i++) {
if (ht[i]->f == 0) {
root = ht[i];
}
}
curr = root;
for (int i = 0; i < n; i++) {
if (cs[i] == '0' && curr->lchild != NULL) {
curr = curr->lchild;
test += '0';
} else if (cs[i] == '1') {
curr = curr->rchild;
test += '1';
}
if (curr->lchild == NULL || curr->rchild == NULL) {
curr = root;
for (int i = 0; i < MAX; i++) {
if (ht[i]->code == test) {
cout<<ht[i]->data;
test = "";
}
}
}
}
cout<<endl;
}
void GouJian(string* s, int* n) {
int a1, a2, m;
m = 2*MAX-1;
HTNode* ht[2*MAX-1];
for (int i=0;i<MAX;i++) {
ht[i] = new HTNode(s[i], n[i]);
}
for (int i=MAX;i<2*MAX-1;i++) {
ht[i] = new HTNode('0');
}
for (int i = 0; i < MAX-1; i++) {
SelectMin(ht, &a1, &a2);
ht[MAX+i]->weight = ht[a1]->weight + ht[a2]->weight;
if (ht[a1]->weight <= ht[a2]->weight) {
ht[MAX+i]->SetChild(ht[a1], ht[a2]);
ht[a1]->code = '0';
ht[a2]->code = '1';
} else {
ht[MAX+i]->SetChild(ht[a2], ht[a1]);
ht[a1]->code = '1';
ht[a2]->code = '0';
}
ht[a1]->SetF(1);
ht[a2]->SetF(1);
ht[a1]->SetFather(ht[MAX+i]);
ht[a2]->SetFather(ht[MAX+i]);
for (int i=0;i<2*MAX-1;i++) {
cout<<ht[i]->weight<<" ";
}
cout<<endl;
}
cout<<"您输入的哈夫曼树为:"<<endl<<endl;
for (int i = 0;i<2*MAX-1;i++) {
if (ht[i]->father == 0) {
Printtree(ht[i], MAX);
cout<<endl;
}
}
cout<<"根据系统分析,有以下结论:"<<endl;
Bianma(ht);
cout<<endl;
FanYi(ht);
}
错误信息
error: no matching function for call to std::basic_string<char, std::char_traits<char>, std::allocator<char> >::copy(std::string*&, int&, int)'
HTNode::HTNode(char)'
error: no match for 'operator!=' in '*((+(((unsigned int)l) * 4u)) + code) != '2'
error: no matching function for call to
错误太多了,先把你的string的赋值和比较都写成双引号,别用单引号!
还有string c[j],数组是不能用不定值,除非你是new的