压缩的字符串
由于精度问题,该算法的压缩能力有限,字符串长度不能过长,否则会出现溢出,压缩会出错。还有,忘了对空格键处理,所以你一旦输入空格就会结束字符串输入
#include<iostream>
#include<math.h>
#include<string>
#include<vector>
using namespace std;
struct node
{
char elem;
double weigh;
double low;
double high;
double rang;
};
////////////////////
double value(string &code)
{
double res=0;
for(int i=0;i<code.size();i++)
{
if(code[i]=='1')
res=res+pow(2,-(i+1));
}
return res;
}
/////////////////////
int search(vector<node> &array,char &e)
{
for(int i=0;i<array.size();i++)
{
if(array[i].elem==e)
return i;
}
return -1;
}
//////////////////////
void set(string &data,vector<node> &array)
{
cin>>data;
data=data+'$';
node temp;
for(int i=0;i<data.size();i++)
{
int f=0;
for(int j=0;j<array.size();j++)
{
if(array[j].elem==data[i])
{
array[j].weigh++;
f=1;
break;
}
}
if(f==1)continue;
temp.elem=data[i];
temp.weigh=1;
array.push_back(temp);
}
array[0].low=0;
array[0].rang=array[0].weigh/data.size();
array[0].high=array[0].low+array[0].rang;
for(i=1;i<array.size();i++)
{
array[i].low=array[i-1].high;
array[i].rang=array[i].weigh/data.size();
array[i].high=array[i].low+array[i].rang;
}
}
//////////////////////
void output(vector<node> &array)
{
cout<<"elem low high rang"<<endl;
for(int i=0;i<array.size();i++)
{
cout<<array[i].elem;
cout.width(10);
cout<<array[i].low;
cout.width(10);
cout<<array[i].high;
cout.width(10);
cout<<array[i].rang<<endl;
}
}
///////////
void getarith(string &data,vector<node> &array,vector<node> &arith)
{
node temp;
int t=search(array,data[0]);
temp=array[t];
arith.push_back(temp);
for(int i=1;i<data.size();i++)
{
temp.elem=data[i];
int t=search(array,data[i]);
temp.low=arith[i-1].low+array[t].low*arith[i-1].rang;
temp.rang=arith[i-1].rang*array[t].rang;
temp.high=temp.low+temp.rang;
arith.push_back(temp);
}
}
///////////////
void code(double low,double high,string &res)
{
while(value(res)<low)
{
string temp=res+'1';
if(value(temp)>high)
res=res+'0';
else
res=temp;
}
}
////////////
void decode(double math,vector<node> &array)
{
while(1)
{
for(int i=0;;i++)
{
if(math<array[i].high)
break;
}
if(array[i].elem=='$')break;
cout<<array[i].elem;
math=math-array[i].low;
math=math/array[i].rang;
}
}
//////////////////
int main()
{
string data;
vector<node> array;
set(data,array);
string result;
vector<node> arith;
/* array[0].elem='a';
array[0].low=0;
array[0].rang=0.2;
array[0].high=0.2;
array[1].elem='b';
array[1].low=0.2;
array[1].rang=0.1;
array[1].high=0.3;
array[2].elem='c';
array[2].low=0.3;
array[2].rang=0.2;
array[2].high=0.5;
array[3].elem='d';
array[3].low=0.5;
array[3].rang=0.05;
array[3].high=0.55;
array[4].elem='e';
array[4].low=0.55;
array[4].rang=0.3;
array[4].high=0.85;
array[5].elem='f';
array[5].low=0.85;
array[5].rang=0.05;
array[5].high=0.9;
array[6].elem='$';
array[6].low=0.9;
array[6].rang=0.1;
array[6].high=1;*/
getarith(data,array,arith);
cout<<"字符数据表为:"<<endl;
output(array);
cout<<"输入字符串的算术编码数据表为:"<<endl;
output(arith);
string res;
code(arith[arith.size()-1].low,arith[arith.size()-1].high,res);
cout<<"字符串的算术编码为:"<<endl;
cout<<res<<endl;
double math=value(res);
cout<<math<<endl;
cout<<endl<<"解码结果为:"<<endl;
decode(math,array);
cout<<endl;
system("pause");
return 0;
}
B. 字符串解压缩
刚才编成了压缩的程序,不好意思,现在的可以解压了。
程序没有给你编写读写文件的内容,文件读写自已去编,那个相对就简单了,程序只介绍了实现基本功能的内容。你可以输入3A4B7D测试。
void
main()
{int
m=0;int
j=0;
//string
a;
//char
c[111];
char
a[111];
char
b[111];
scanf("%s",a);
for(int
i=0;a[i]!='\0';i++)
{
cout<<"a"<
1&&a[i]-'0'<9)
{
m=a[i]-'0';}
else{b[j]=a[i];j++;}
while(m>1)
{
b[j]=a[i+1];
j++;
m--;
}
}
cout<
评论
0
0
0
加载更多
C. java字符串经过bcd压缩后怎么传输
解决方法:
据我所知,您目前的做法是:
>使用getBytes(“UTF-8”)将String转换为字节数组.
>压缩字节数组
>使用新的String(字节,…,“UTF-8”)将压缩字节数组转换为String.
>传输压缩字符串
>接收压缩字符串
>使用getBytes(“UTF-8”)将压缩字符串转换为字节数组.
>解压缩字节数组
>使用新的String(bytes,…,“UTF-8”)将解压缩的字节数组转换为String.
这种方法的问题在于步骤3.压缩字节数组时,您创建的字节序列可能不再是没汪有效的UTF-8.结果将是步骤3中的例外.
解决方案是使用像Base64这样的“字节到字符”编码方案将压缩字节转换为可传输的字符串.换句话说,用调用Base64编码函数代替步骤3,用调用Base64解码函数代替步骤6.
笔记:
>对于小弦,压缩和
编码很可能实际上
增加传输字符串的大小.
>如果要将压缩的字符串合并到URL中,您可能希望为Base64选择不同的编码,以避免兆察缓需要进行URL转义的字符.
>根据您传输的数据的性质,您可能会发现特定于域的压缩比通用压缩更好.考虑在创建以逗号分隔的字符串之前压缩数据.考虑以逗号分隔的字符串的族模替代方法.
D. 压缩算法进行字符串压缩
Deflater 是同时使用了LZ77算法与哈夫曼编码的一个无损数据压缩算法。
我们可以使用 java 提供的 Deflater 和 Inflater 类对 json 进行压缩和解压缩,下面是工具类
压缩前的字节长度为:1825
压缩后的字节长度为:284
压缩率为63.73%,压缩后体积为原来的36.27%
压缩前的字节长度为:1825
压缩后的字节长度为:307
压缩率为62.04%,压缩后体积为原来的37.95%,也是不错的!
E. 使用C语言实现字符串的压缩。
/*
原串:111225555
压缩后:312245
原串:333AAAbbbb
压缩后:333A4b
原串:ASXDCdddddd
压缩后:1A1S1X1D1C6d
Pressanykeytocontinue
*/
#include<stdio.h>
#include<string.h>
char*CompressStr(chars[]){
chart[255];
inti=0,j,k=0;
while(s[i]){
j=i+1;
while(s[i]==s[j])++j;
t[k++]=j-i+'0';
t[k++]=s[i];
i=j;
}
t[k]='