資源簡(jiǎn)介
分為encode.cpp和decode.cpp兩個(gè)文件,運(yùn)行環(huán)境為VC++6.0

代碼片段和文件信息
#include
using?namespace?std;
#define?M?100?//最大消息長(zhǎng)度
#define?N?4?//最大信源符號(hào)個(gè)數(shù)
class?decode
{
private:
char?symbol[N];?//?信源符號(hào)數(shù)組
char?s;?//用于接收信源符號(hào)的輸入
long?double?chance[N];?//信源符號(hào)對(duì)應(yīng)概率
long?double?c;?//用于接收概率輸入
long?double?sum;?//概率和
long?double?code;?//待譯碼的字段
char?msg[M];?//消息內(nèi)容
int?length;?//譯碼長(zhǎng)度(譯碼停止標(biāo)志)
int?count;?//實(shí)際信源符號(hào)個(gè)數(shù)
long?double?LowHigh;?//每次區(qū)間縮減后得到的新區(qū)間
long?double?lowhigh;
long?double?wid;?//信源符號(hào)概率寬度
public:
decode()?//構(gòu)造函數(shù)
{
sum=0;
Low=0;High=0;
}
void?get_symbol();
void?get_code();
void?decoding();
~decode(){}?//析構(gòu)函數(shù)
};
//獲取信源符號(hào)極其概率
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;?//將實(shí)際信源符號(hào)個(gè)數(shù)賦給count
if(sum!=1)
{
cout<<“The?probability?you?input?has?mistakes!“< exit(-1);
}
}
//獲取譯碼字和譯碼長(zhǎng)度
void?decode::get_code()
{
cout<<“Please?input?the?code?to?decode?it:“< cin>>code;
cout<<“Please?input?the?length?you?want?decode:“< cin>>length;
}
//算數(shù)譯碼過(guò)程
void?decode::decoding()
{
int?ij;
//判斷code落在第一個(gè)區(qū)間(首區(qū)間確定)
for(i=0;i {
if(code {
msg[0]=symbol[i-1];
break;
}
else
{
Low=High;
High+=chance[i];
wid=chance[i];
}
}
//區(qū)間縮減
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;
}
//結(jié)果輸出
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;
}
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2017-05-11?16:51??算術(shù)編碼C++簡(jiǎn)單實(shí)現(xiàn)\
?????文件????????2050??2017-04-21?17:30??算術(shù)編碼C++簡(jiǎn)單實(shí)現(xiàn)\decode.cpp
?????文件????????3122??2017-04-21?16:29??算術(shù)編碼C++簡(jiǎn)單實(shí)現(xiàn)\encode.cpp
評(píng)論
共有 條評(píng)論