📄 算术编码.txt
字号:
//以输入 bill~gates为例子
#include <iostream>
using namespace std;
#define N 4//字符的 个数
#define num 4 //不同字符的个数
main()
{int i,j;
long double p[num]={0};
int input[N];
char str[N];
char temp[N];
char flag;
char temp2[N];
int cnt;
int cnt3;
int cnt2;
for(i=0;i<N;i++)
{
cout<<"please input the code sequences to be encoded "<<i<<endl;
cin>>str[i];
};
cout<<"str[5]="<<str[5]<<endl;
for(i=0;i<N;i++)
temp[i]=str[i];
flag=temp[0];
for(i=0;i<N-1;i++)
for(j=i+1;j<N;j++)
if(temp[i]>temp[j])
{
flag=temp[i];
temp[i]=temp[j];
temp[j]=flag;
}; //对字符进行从小到大排序
cnt=0;
for(i=0;i<N;i++)
{
if(temp[i]==temp[i+1]);
else{temp2[cnt++]=temp[i];}//吃掉相同的字符
};
for(i=0;i<N;i++)
{ j=0;
while(temp2[j]!=str[i]) j++;
input[i]=j; //传递数字化后的字符信息到input[]数组中去
}
cnt3=0;
i=0;
while(i<N)
{
cnt2=1;
while(temp[i]==temp[i+1]) {i++,cnt2++;};
p[cnt3]=double(cnt2)/N;
i++;cnt3++; //概率统计
};
long double pp[num+1];
long double t;
long double low,high; //算术编码
low=0.0;
high=1.0;
pp[0]=0;
for (i=1;i<num+1;i++)
pp[i]=pp[i-1]+p[i-1];
cout<<"编码过程如下"<<endl;
for (i=0;i<N;i++)
{
t=high-low;
low=pp[input[i]]*t+low;
high=pp[input[i]+1]*t+high-t;
cout<<low<<","<<high<<endl;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -