资源简介

分为encode.cpp和decode.cpp两个文件,运行环境为VC++6.0

资源截图

代码片段和文件信息

#include
using namespace std;

#define M 100 //最大消息长度
#define N 4 //最大信源符号个数

class decode
{
private:
char symbol[N]; // 信源符号数组
char s; //用于接收信源符号的输入

long double chance[N]; //信源符号对应概率
long double c; //用于接收概率输入
long double sum; //概率和

long double code; //待译码的字段

char msg[M]; //消息内容
int length; //译码长度(译码停止标志)

int count; //实际信源符号个数
long double LowHigh; //每次区间缩减后得到的新区间
long double lowhigh;
long double wid; //信源符号概率宽度

public:
decode() //构造函数
{
sum=0;
Low=0;High=0;
}
void get_symbol();
void get_code();
void decoding();
~decode(){} //析构函数
};

//获取信源符号极其概率
void decode::get_symbol()
{
cout<<“please input the symbol and its chance:“< for(int i=0;i {
cin>>s>>c;
symbol[i]=s;
chance[i]=c;
sum+=c;
}
count=i; //将实际信源符号个数赋给count

if(sum!=1)
{
cout<<“The probability you input has mistakes!“< exit(-1);
}
}

//获取译码字和译码长度
void decode::get_code()
{
cout<<“Please input the code to decode it:“< cin>>code;
cout<<“Please input the length you want decode:“< cin>>length;
}

//算数译码过程
void decode::decoding()
{
int ij;

//判断code落在第一个区间(首区间确定)
for(i=0;i {
if(code {
msg[0]=symbol[i-1];
break;
}
else
{
Low=High;
High+=chance[i];
wid=chance[i];
}
}

//区间缩减
for(i=1;i for(j=0;j {
low=Low;
high=low+wid*chance[j];
if(high>code)
{
msg[i]=symbol[j];
Low=low; High=high; wid=High-Low;
break;
}
else
Low=high;
}

//结果输出
for(i=0;i cout< cout<}

int main()
{
decode d;
d.get_symbol(); //A 0.1 B 0.4 C 0.2 D 0.3
d.get_code(); //Java=0.5143876 C++=0.514388
d.decoding();
return 0;
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-05-11 16:51  算术编码C++简单实现\
     文件        2050  2017-04-21 17:30  算术编码C++简单实现\decode.cpp
     文件        3122  2017-04-21 16:29  算术编码C++简单实现\encode.cpp

评论

共有 条评论