資源簡(jiǎn)介
本程序是在三種編碼的過(guò)程中提取相應(yīng)的參數(shù),輸出顯示,比較三種編碼。使用文件操作

代碼片段和文件信息
#include?
#include?
#include?
#include?
#include??
#include?
#include?
using?namespace?std;
struct?message{ //存放信源一個(gè)消息及頻率?
char?x;
double?px;
};
void?shannon(message?data[]int?n); //香農(nóng)編碼
void?fano(message?data[]int?n); //費(fèi)諾編碼
void?huff(message?data[]int?n); //赫夫曼編碼
bool?cmp(message?amessage?b); //比較函數(shù),sort調(diào)用?
int?main?()
{
message?data[26];
int?n=0;
ifstream?infile(“d:\\data.txt“);
while(!infile.eof())
{
infile>>data[n].x;
infile>>data[n].px;
n++;
}
infile.close();
sort(datadata+ncmp);
cout<<“香農(nóng)編碼:“< shannon(datan);
cout< cout<<“費(fèi)諾編碼:“< fano(datan);
cout< cout<<“哈夫曼編碼:“< huff(datan);
cout< return?0;
}
void?shannon(message?data[]int?n)
{
int?p[26]; //存放每個(gè)信源編碼后的長(zhǎng)度
double?sum=0; //前i個(gè)概率相加?
double?wordAvg=0.0; //存放平均碼長(zhǎng)?
for(int?i=0;i {
p[i]=-log(data[i].px)/(log(2.0));
wordAvg+=p[i]*data[i].px;
}
cout<<“香農(nóng)編碼后的信源長(zhǎng)度:“< cout<<“編碼后平均碼長(zhǎng):“< }
void?fano(message?data[]int?n)
{
void?function_fano(message?data[]int?mint?nint?p[]); //費(fèi)諾編碼核心函數(shù)?
int?p[26]={0}; //存放每個(gè)信源編碼后的長(zhǎng)度
double?wordAvg=0.0; //存放平均碼長(zhǎng)?
function_fano(data0np);
for(int?i=0;i {
wordAvg+=p[i]*data[i].px;
}
cout<<“費(fèi)諾編碼后信源長(zhǎng)度:“< cout<<“編碼后平均碼長(zhǎng):“< }
void?huff(message?data[]int?n)
{?
int?p[26]={0}; //存放每個(gè)信源編碼后的長(zhǎng)度
int?m[26]={0}; //標(biāo)記變量?
double?wordAvg=0.0; //存放平均碼長(zhǎng)?
for(int?i=n-2;i>=0;i--)
for(int?j=n-1;j>=i;j--)
p[j]++;
for(int?i=0;i wordAvg+=p[i]*data[i].px;
cout<<“哈夫曼編碼后信源長(zhǎng)度:“< cout<<“編碼后平均碼長(zhǎng):“< }
bool?cmp(message?amessage?b)
{
return?(a.px-b.px>0);
}
void?function_fano(message?data[]int?mint?nint?p[]) //費(fèi)諾編碼核心函數(shù)?
{
if((n-m)==1){
p[m]++;
p[n]++;
return?;
}?
double?sum=0.0;
for(int?i=m;i sum+=data[i].px;
double?sum1=0.0;
for(int?i=m;i {
sum1+=data[i].px;
p[i]++;
if(sum1>=sum/2)
{
function_fano(datamip);
function_fano(datai+1np);
break;
}
}
}
?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
?????文件?????????46??2017-06-11?22:41??香農(nóng)費(fèi)諾哈夫曼編碼結(jié)果分析\data.txt
?????文件????????351??2017-06-11?22:48??香農(nóng)費(fèi)諾哈夫曼編碼結(jié)果分析\使用說(shuō)明.txt
?????文件???????2381??2017-05-16?22:12??香農(nóng)費(fèi)諾哈夫曼編碼結(jié)果分析\香農(nóng)費(fèi)諾哈夫曼編碼碼字結(jié)果分析.cpp
?????目錄??????????0??2017-06-11?22:42??香農(nóng)費(fèi)諾哈夫曼編碼結(jié)果分析
-----------?---------??----------?-----??----
?????????????????2778????????????????????4
評(píng)論
共有 條評(píng)論