完整代码
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <vector>
#include <stack>
#include <algorithm>
#include <windows.h>
using namespace std;
typedef struct Node{
char ch;
int weight = 0;
Node* left = NULL;
Node* right = NULL;
}*node;
struct Data {
char c;
string code;
}data[93];
vector<node> v;
stack<node> s;
int ans = -1;
node root;
bool cmp(node x,node y){
return x->weight > y->weight;
}
void Character_frequency(){
ifstream infile("Character_frequency.txt");
while(!infile.eof()){
node temp;
temp = new Node;
infile >> temp->ch >> temp->weight;
v.push_back(temp);
}
sort(v.begin(),v.end(),cmp);
while(v.size() >= 2){
node p;
p = new Node;
p->weight += (v.back())->weight;
p->left = v.back();
v.pop_back();
p->weight += (v.back())->weight;
p->right = v.back();
v.pop_back();
while(!v.empty() && p->weight > (v.back())->weight){
s.push(v.back());
v.pop_back();
}
v.push_back(p);
while(!s.empty()){
v.push_back(s.top());
s.pop();
}
}
root = v.back();
infile.close();
}
void Preorder(node Root,string str){
if(Root->left == NULL){
ans++;
data[ans].c = Root->ch;
data[ans].code = str;
}
else{
Preorder(Root->left,str+"0");
Preorder(Root->right,str+"1");
}
}
void Coding(){
Preorder(root,"");
cout<<"Please input the data:"<<endl<<endl;
int flag = 1;
string a,result = "";
getline(cin,a);
cout<<endl<<"The coding are:"<<endl<<endl;
for(int i = 0; i < (int)a.length(); i++){
flag = 1;
for(int j = 0; j < 93; j++){
if(a[i] == data[j].c){
result += data[j].code;
flag = 0;
break;
}
}
if(flag){
cout<<"The encoding failed with illegal characters."<<endl;
break;
}
}
if(!flag)
cout<<result<<endl;
}
void Decoding(){
cout<<"Please input the coding:"<<endl<<endl;
int flag = 0;
string a,result = "";
getline(cin,a);
node p;
p = root;
cout<<endl<<"The data are:"<<endl<<endl;
for(int i = 0; i < (int)a.length(); i++){
if(a[i] == '0')
p = p->left;
else if(a[i] == '1')
p = p->right;
else{
cout<<"Decoding failed with illegal message"<<endl<<endl;
return;
}
if(p->left == NULL){
result += p->ch;
p = root;
if(i == (int)a.length()-1)
flag = 1;
}
}
if(flag)
cout<<result<<endl;
else
cout<<"Decoding failed with incorrect encoding information."<<endl<<endl;
}
int main()
{
Character_frequency();
int choose;
loop:
cout<<endl<<"-----------------------------Huffman Code---------------------------"<<endl<<endl;
cout<<"Please choose coding or decoding(0.CODING 1.DECODING)"<<endl;
cin>>choose;cin.ignore();
if(choose == 0)
Coding();
else if(choose == 1)
Decoding();
else
{
cout<<"The input is error!Please check your input again."<<endl;
Sleep(3000);
system("cls");
goto loop;
}
cout<<endl<<endl;
system("pause");
return 0;
}
需要的txt文档(可修改字符频率)=
a 330 b 186 c 272 d 289 e 344 f 253 g 251 h 299 i 321 j 160 k 177 l 284 m 267 n 318 o 326 p 244 q 157 r 296 s 307 t 335 u 270 v 183 w 263 x 159 y 245 z 154 A 230 B 136 C 192 D 200 E 241 F 149 G 145 H 215 I 222 J 112 K 125 L 196 M 184 N 220 O 226 P 140 Q 106 R 208 S 217 T 239 U 189 V 135 W 152 X 111 Y 143 Z 102 0 359 1 374 2 400 3 365 4 396 5 377 6 383 7 350 8 376 9 358 . 87 + 98 - 96 * 94 / 92 ~ 2 ! 70 @ 50 # 75 $ 70 % 65 ^ 52 & 63 ( 82 ) 81 _ 9 = 77 [ 43 ] 40 { 38 } 35 | 29 \ 27 : 43 ; 62 ’ 25 , 85 < 12 > 13 ? 67 " 21