#include <iostream.h>
void main(void)
{
int count,length;
int i=0,j=0;
char number[4],n;
long double chance[4],c;
char code[100];
long double range,high=1.0,low=0.0,d;
cout<<"程序功能:算术编码的编码过程"<<endl<<endl;
//输入字符及其概率
cout<<"输入源码中的字符及其概率(4个):"<<endl;
for(i=0;i<4;i++)
{
cin>>n>>c;
number[i]=n;
chance[i]=c;
}
count=i;
//输入字符串长度
cout<<"输入源码的长度(最多100):";
cin>>length;
while(length>=100)
{
cout<<"源码长度过长,请重新输入";
cin>>length;
}
//输入字符串
cout<<"输入源码:";
for(i=0;i<length;i++)
{
cin>>code[i];
}
//编码算法
for(i=0;i<length;i++)
{
d=0;
for(j=0;j<count;j++)
{
d+=chance[j];
if(code[i]==number[j]) break;
}
range=high-low;
high=low+range*d;
low=low+range*(d-chance[j]);
}
cout<<"result:"<<low<<endl;
}