哈夫曼编码

来源:互联网 发布:js 错误的数量词 编辑:程序博客网 时间:2024/06/08 15:03
#pragma once#include<iostream>#include<fstream>#include<string>using namespace std;void numberoff(char *ma, double* maweight, int& coutnum, int &i){    ifstream fcin;    fcin.open("C:\\Users\\jl\\Desktop\\哈夫曼树编码\\data.txt");    if (!fcin.is_open())  cout << "can not open the file\n";    char m;    int mk ;    fcin.get(m);    while (!fcin.eof())    {        mk = 0;        for (int i1 = 0; i1 < i; i1++)        {            if (m == ma[i1])            {                maweight[i1]++;                mk = 1;                break;            }        }        if (mk == 0)        {            ma[i - 1] = m;            maweight[i - 1] = 1;            i++;        }        coutnum++;        fcin.get(m);    }    for (int i1 = 0; i1 < i; i1++)        maweight[i1] = maweight[i1] / coutnum;}class mod{    double*_mwt;    int *parent;    int *lcd;    int *rcd;public:    mod();    mod(const int i);    ~mod();    void haftree( const double* maweight, const int i);    void compare(int &a,int &b,const int i);    void haftree(const int i);    void inflie(const int i,char *ma, string *s);    void compress(const int i, string *s, char *ma);    void exstract(const int i, char*ma);};mod::mod(){    _mwt = NULL;    parent = NULL;    lcd = NULL;    rcd = NULL;}mod::mod( const int i){    _mwt=new double[2 * i - 1];    parent = new int[2 * i - 1];    lcd = new int[2 * i - 1];    rcd = new int[2 * i - 1];}mod::~mod(){    delete[]_mwt;    delete[]parent;    delete[]lcd;    delete[]rcd;}void mod:: haftree( const double* maweight,const int i){    int i1 = 0;    for (i1 = 0; i1 < i; i1++)    {        _mwt[i1] = maweight[i1];        parent[i1] = -1;        lcd[i1] = -1;        rcd[i1] = -1;    }    for (i1; i1 < 2 * i - 1; i1++)    {        _mwt[i1] = 1;        parent[i1] = -1;        lcd[i1] = -1;        rcd[i1] = -1;    }}void mod::compare(int &a, int &b,const int i){    double a1 = 1, a2 = 1;    for (int i1=0;i1 <2*i-1; i1++)    {        if (parent[i1] == -1)        {            if (!(_mwt[i1] > a1))            {                b = a;                a = i1;                a2 = a1;                a1 = _mwt[i1];            }            else if (!(_mwt[i1] > a2))            {                b = i1;                a2 = _mwt[i1];            }        }    }}void mod::haftree(const int i){    int a, b;    for (int p = i; p <2* i-1; p++)    {            this->compare(a, b, i);            _mwt[p] = _mwt[a] + _mwt[b];            lcd[p] = a;            rcd[p] = b;            parent[a] = parent[b] = p;    }}void mod::inflie(const int i,char *ma, string *s){    fstream fout;    fout.open("C:\\Users\\jl\\Desktop\\哈夫曼树编码\\HufCode.txt", ios_base::out);    ifstream fcin;    fcin.open("C:\\Users\\jl\\Desktop\\哈夫曼树编码\\HufCode.txt");    if (!fcin.is_open())  cout << "can not open the file\n";    int p=0,i2;    int mma[10];    for (int i1 = 0; i1 < i; i1++)    {        p = i1;        int a = 0;        while (this->parent[p] != -1)        {            i2= p;            p = this->parent[p];            if (this->lcd[p] == i2)            {                mma[a] = 0;                a++;            }            else if(this->rcd[p] == i2)            {                mma[a] = 1;                a++;            }        }        fout << ma[i1] << " ";        while (a)        {            a--;            fout << mma[a];        }        fout << endl;        fcin.get();        fcin.get();        fcin >> s[i1];    }}void mod::compress(const int i, string *s, char *ma){    ifstream fcin;    fcin.open("C:\\Users\\jl\\Desktop\\哈夫曼树编码\\data.txt");    if (!fcin.is_open())  cout << "can not open the file\n";    fstream fout;    fout.open("C:\\Users\\jl\\Desktop\\哈夫曼树编码\\CodeFile.txt", ios_base::out);    char m;    int p = 0;    fcin.get(m);    while (!fcin.eof())    {        p = 0;        while (m != ma[p])            p++;        fout << s[p];        fcin.get(m);    }}void mod::exstract(const int i,char*ma){    ifstream fcin;    fcin.open("C:\\Users\\jl\\Desktop\\哈夫曼树编码\\CodeFile.txt");    fstream fout;    fout.open("C:\\Users\\jl\\Desktop\\哈夫曼树编码\\Newdata.txt", ios_base::out);    int root = 2 * i - 1,p,aim;    char m;    fcin.get(m);    while (!fcin.eof())    {        p = root - 1;        while (p != -1)        {            if (m == '0')            {                if (lcd[p] == -1)                {                    fout << ma[p];                    break;                }                p = lcd[p];            }            else if (m == '1')            {                if (lcd[p] == -1)                {                    fout << ma[p];                    break;                }                p = rcd[p];            }            fcin.get(m);        }    }}#include<iostream>#include<fstream>#include<string>#include <cstdlib>using namespace std;int main(){    char Ma[1000] = { 0 };    double Maweight[1000] = { 0 };    int  Mcoutnum = 0, Mi = 1;    string s[1000];    numberoff(Ma, Maweight, Mcoutnum, Mi);    cout << "各个字符的权值为:\n";    for (int i = 0; i < Mi-1; i++)    {        cout << Ma[i] << " " << Maweight[i] << endl;    }    Mi--;    mod tree(Mi);    tree.haftree(Maweight, Mi);    tree.haftree(Mi);    tree.inflie(Mi, Ma,s);    cout << "各个字符的哈夫曼编码为:\n";    for (int i1 = 0; i1 < Mi; i1++)    {        cout << Ma[i1] << " " << s[i1] << endl;    }    tree.compress(Mi,s,Ma);    tree.exstract(Mi, Ma);    cout << endl;    system("PAUSE");}

懒得说了
大致就是这样子吧

原创粉丝点击