题目概述
设信源可能输出的符号是26个字母,且每个字母出现的概率为:a, b, c, d, e, f 均为0.1,其它是等概的,试编写程序可以对任意字母序列(如presentation)进行固定模式的算术编码,并进行相应的译码。
题目分析
通过对题目分析可知,此题目是对(a~z)26个字母进行固定模式编码,译码。可以分为三部分,输入字符,编码,译码。
输入字符
可以创建一个一维数组(可以选择大小确定的数组,也可以使用指针创建动态数组,为了减小难度,选择大小确定的数组),通过cin输入字符。
循环数组,当为0时(注意不同于‘0’),结束循环,得到数组长度。
保证程序的健壮性,要对输入进行合法性判断,通过if语句依次判断是否在a~z, A-Z之间。
编码
判断每次的字符是什么,然后每次该字符的概率依次叠加,得到该字符串最后的概率pp。
通过公式求得最低位数digit。
并通过一个for循环,每次划分新的区间,得到最后的范围。
选取上界值然后减去pp的0.01,得到近似概率。
不断*2,得到二进制编码,直到达到最低位数digit。
译码
先将二进制编码转为十进制。
依次从0倒1选取区间,判断是否在相应区间内,如果在就输出相应字符。
知道字符数与输入相同。
问题分析:
1.精度问题,当输入过长,long double的精度以及不够用了,所以我选择使用二维数组存储字符,编译译码也对每一组进行编译译码,理论上可以达到无穷精度。
2.方法复杂度是n2,待改进。
3.耦合度过高,所以我将运算封装到函数中,来降低耦合度。
运行图片
代码
#include <iostream>
#include <string>
#include <cstdlib>
#include <math.h>
using namespace std;
/*
created by Yu Zhao;
date:2019.04.30;
this project can decode from zero to 1600 in threoty;
but when have too many 'z', it will has a error in result;
contrast with old one:
this project's code method is sample;
but include the method to function;
increase the length of code from 10 to 1600;
reduce the length of code from 378 to 135;
*/
char str[1000];
char str_end[200][8];
char str_se_end[200][50];
int len_test[200];
int len;
int digit_end[200];
long double pp_end;
//char se_str_start[1000]; //输出显示的长度
long double p_abc[26] = {0.1,0.1,0.1,0.1,0.1,0.1,0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02,0.02};
long double scale[27] = {0.0,0.1,0.2,0.3,0.4,0.5,0.60,0.62,0.64,0.66,0.68,0.70,0.72,0.74,0.76,0.78,0.80,0.82,0.84,0.86,0.88,0.90,0.92,0.94,0.96,0.98,1.00};
char letter[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
bool input(){
cout<<"********************输入********************"<<endl;
int i = 0;
cout <<"please input the liter(from a to z)\n";
cin >> str; //输入目标字符串
while(str[i]!=0 ){ //读取长度——有一个问题
len++;
i++;
}
for(i=0;i<len;i++){ //判断输入是否合理
if(str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z'){
}
else {
cout << "your liter is wrong"<<endl;
exit(1);
return false;
}
}
i = 0;
cout <<"the liters you put is :";
for(int j = 0;j<((len/8)+1);j++){ //转二维数组
for(int k = 0;k<8;k++){
if(j<len/8){
str_end[j][k] = str[i];
i++;
cout<<str_end[j][k];
len_test[j]=8;
}else{
str_end[j][k] = str[i];
i++;
cout<<str_end[j][k];
len_test[j]=len%8;
if(i ==len%8 )
break;
}
}
}
cout<<endl;
cout<<"your liter is right"<<endl;
cout <<"the liters' length is :"<< len<<endl;
return true;
}
void decode(int len,char abc[],int count){
long double top = 0;
long double bottom = 0;
long double pp = 1;
int digit;
int flag;
long double value = 0;
char se_str_start[1000];
for(int i = 0;i<len;i++){ //计算最后概率及区间
flag = (int)(abc[i]-97);
bottom = bottom + scale[flag]*pp;
pp = pp*p_abc[flag];
top = bottom + pp;
}
digit = ceil((log10(1/pp))/0.301); //确定最低位数
pp_end = pp_end*pp;
digit_end[count] = digit;
value = top-0.001*pp; //取值
for(i = 0;i<digit;i++){ //转为二进制
value = value*2;
se_str_start[i] = (char)((int)value+48); //转为字符数组
str_se_end[count][i]=(char)((int)value+48);
if(value>=1)
value = value-(int)value;
cout<<se_str_start[i];
}
}
void record(int se_len,char se_str[],int count){
int i = 0;
long double start = 0;
int j = 0;
long double value = 0;
long double pp = 1;
long double bottom = 0;
long double top = 0;
pp_end = 1;
top = bottom+scale[j+1]*pp;
bottom = bottom+pp*scale[j];
for(i=0;i<se_len;i++) //转为十进制
value = value+((int)(se_str[i])-48)*pow(2,(-(i+1)));
pp_end = pp_end*value;
for(i = 0;i<len_test[count];i++) //搜寻区间并输出————有问题
for(j=0;j<26;j++){
top = start+scale[j+1]*pp;
bottom = start+pp*scale[j];
if((value>=bottom)&&(value<top)){
cout<<letter[j];
pp = top-bottom;
start = bottom;
break;
}
}
}
int main(){
pp_end = 1;
input();
int digit_length=0;
cout<<"********************编码********************"<<endl;
cout<<"the end of jiema is :";
for(int i=0;i<=len/8;i++){
decode(len_test[i],str_end[i],i);
digit_length=digit_length+(int)digit_end[i];
if(len_test[i]==0)
break;
}
cout<<endl;
cout<<"the digit is :"<<" "<<digit_length<<endl;
cout<<"the length of the section is :"<<" "<<pp_end<<endl;
cout<<"********************译码********************"<<endl;
cout<<"the end is :";
for(i = 0;i<=len/8;i++)
record(digit_end[i],str_se_end[i],i);
cout<<endl;
cout<<"the value is :"<<pp_end<<endl;
return 0 ;
}
其他
另:因为代码是自己一个人写的,所以有很多不完善的地方,请谅解。切忌照抄
转载请说明出处