壓縮的字元串
由於精度問題,該演算法的壓縮能力有限,字元串長度不能過長,否則會出現溢出,壓縮會出錯。還有,忘了對空格鍵處理,所以你一旦輸入空格就會結束字元串輸入
#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]='