當前位置:首頁 » 編程語言 » 算術編碼壓縮的c語言代碼實現
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

算術編碼壓縮的c語言代碼實現

發布時間: 2022-08-31 07:59:32

1. 如何用c語言實現數據壓縮

首先選擇一個壓縮演算法

然後按照演算法實現壓縮代碼,調用介面就可以
常見的 可以使用哈夫曼編碼壓縮,或者使用開源的壓縮代碼,比如lzo, gzip, lzma等等。

2. 使用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]='';
strcpy(s,t);
returns;
}

intmain(void){
chari,s[][20]={"111225555","333AAAbbbb","ASXDCdddddd"};
for(i=0;i<3;++i){
printf("原串:%s ",s[i]);
printf("壓縮後:%s ",CompressStr(s[i]));
}
return0;
}

3. 如何用c語言壓縮解壓文件夾

壓縮是一種有效的減小數據量的方法,目前已經被廣泛應用於各種類型的信息系統之中。

一種壓縮文本文件的方法如下:

1. 原始文本文件中的非字母的字元,直接拷貝到壓縮文件中;

2.
原始文件中的詞(全部由字母組成),如果是第一次出現,則將該詞加入到一個詞的列表中,並拷貝到壓縮文件中;否則該詞不拷貝到壓縮文件中,而是將該詞在詞的列表中的位置拷貝到壓縮文件中。

3. 詞的列表的起始位置為 1 。 詞的定義為文本中由大小寫字母組成的最大序列。大寫字母和小寫字母認為是不同的字母,即 abc 和 Abc
是不同的詞。詞的例子如下: * x-ray 包括兩個詞 x 和 ray * mary's 包括兩個詞 mary 和 s * a c-Dec 包括三個詞 a 和
c 和 Dec 編寫一個程序,輸入為一組字元串,輸出為壓縮後的文本。

輸入:

輸入為一段文本,你可以假設輸入中不會出現數字、每行的長度不會超過 80 個字元,並且輸入文本的大小不會超過 10M。

輸出:

壓縮後的文本。

輸入:
Please, please do it--it would please Mary very,
very much.

Thanks

輸出:
Please, please do it--4 would 2 Mary very,
7 much.

Thanks

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#defineLEN1<<20
intisArabic(charc){
return('a'<=c&&c<='z')||('A'<=c&&c<='Z');
}
intmain()
{
chardict[LEN];
char*index[100000];
charbuf[82];
intnWord=0;
inti,j;
charc;
char*inFile="G:\in.txt",*outFile="G:\out.txt";
FILE*inp,*outp;

if((inp=fopen(inFile,"r"))==NULL){
printf("cannotopen ");
exit(1);
}
if((outp=fopen(outFile,"w"))==NULL){
printf("outfail ");
}
index[0]=dict;
do{
/*getaword*/
i=0;
do{
c=fgetc(inp);
buf[i++]=c;
}while(isArabic(c));

buf[i-1]=0;
/*putittodict*/
if(i>1){
for(j=0;j<nWord;j++){
if(strcmp(index[j],buf)==0){
break;
}
}
if(j==nWord){
strcpy(index[nWord],buf);
index[nWord+1]=index[nWord]+strlen(buf)+1;
nWord++;
/*printf("new:%s ",buf);*/
}else{
sprintf(buf,"%d",j+1);
/*printf("found:%s ",buf);*/
}
}
/*putittooutputfile*/
if(c!=EOF)
fprintf(outp,"%s%c",buf,c);
else
fprintf(outp,"%s",buf);
}while(c!=EOF);

fclose(inp);
fclose(outp);
/*system("PAUSE");*/
returnEXIT_SUCCESS;
}

4. 跪求C語言進行哈夫曼編碼、算術編碼和LZW編碼,要求源程序要有注釋。

以下是哈夫曼編碼
#include<iostream>
#include<math.h>
#include<string>
#include<iomanip>
using namespace std;

int n;

int isin(string str,char a)
{
int temp=0;
for(int i=0;i<str.length();i++)
{
if(str[i]==a) temp=1;
}
return temp;
}
void bubble(double p[],string sign[])//排序
{
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(p[i]<p[j])
{
double temp=p[i];
p[i]=p[j];
p[j]=temp;
string m=sign[i];
sign[i]=sign[j];
sign[j]=m;
}
}
}
}
void huff(double tempp[],string tempstr[])
{
double p[20][20];
string sign[20][20];
sign[0][i]=tempstr[i]; //符號放在sign數組中
for(int i=0;i<n;i++)
{
p[0][i]=tempp[i]; //p數組放對應的概率(第1列中)
}
for(i=0;i<n-1;i++)
{
bubble(p[i],sign[i]); //第一次排序
for(int j=0;j<n-2-i;j++)
{
p[i+1][j]=p[i][j]; //前n-2-i個概率重新放在p數組中(是數組的第2列中)
sign[i+1][j]=sign[i][j];
}
p[i+1][j]=p[i][j]+p[i][j+1];//第一次兩個最小概率求和
sign[i+1][j]=sign[i][j]+sign[i][j+1];//符號跟隨
for(j=n-1-i;j<n;j++)
{
p[i+1][j]=0;
}
}
string final[20];
for(i=n-2;i>=0;i--)
{
for(int k=0;k<n;k++)
{
if(isin(sign[i][n-2-i],sign[0][k][0])) final[k]+="0";
if(isin(sign[i][n-1-i],sign[0][k][0])) final[k]+="1";
}
}
cout<<setw(9)<<"哈弗曼編碼如下:"<<endl;
for(i=0;i<n;i++)
{
cout<<setw(7)<<sign[0][i]<<setw(7)<<p[0][i]<<setw(10)<<final[i]<<
setw(7)<<final[i].length()<<endl;
}
}
void main()
{
char a[50];
cout<<"該字元串符號為:";
cin>>a;
string s=a;
n=s.length();
char b[20][2];
for(int i=0;i<n;i++)
{
b[i][0]=a[i];
b[i][1]='\0';
}
string str[20];
for(i=0;i<n;i++)
{
str[i]=b[i];
}
double tempp[20];
cout<<"字元概率依次為:";
for(i=0;i<n;i++)
{
cin>>tempp[i];
}
huff(tempp,str);
}

5. 用c語言實現算術編碼和解碼

/*我覺得應該是你相互換吧?*/
void code(char *p)
{
*p+=5;/*相應加5個數字我覺得字母不可能存在大於250的哈*/
}
void codec(char *p)
{*p-=5;}
main()
{
char a[100];
int i;
printf("Enter the string!\n");
scanf("%s",a);/*輸入字元*/
for(i=0;a[i]!='\0';i++)
{
code(a+i);/*加密*/
}
printf("\ncode string is:%s",a);
for(i=0;a[i]!='\0';i++)
{codec(a+i);}
printf("\nenter string is:%s",a);
}

6. 急求lzw演算法的英文文本壓縮C語言源代碼!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>//用來計算壓縮的時間
using namespace std;

//定義常數
const int MAX = 1000003;//最大code數,是一個素數,求模是速度比較快
const int ascii = 256; //ascii代碼的數量
const int ByteSize = 8; //8個位元組

struct Element//hash表中的元素
{
int key;
int code;
Element *next;
}*table[MAX];//hash表

int hashfunction(int key)//hash函數
{
return key%MAX;
}
void hashinit(void)//hash表初始化
{
memset(table,0,sizeof(table));
}
void hashinsert(Element element)//hash表的插入
{
int k = hashfunction(element.key);
if(table[k]!=NULL)
{
Element *e=table[k];
while(e->next!=NULL)
{
e=e->next;
}
e->next=new Element;
e=e->next;
e->key = element.key;
e->code = element.code;
e->next = NULL;
}
else
{
table[k]=new Element;
table[k]->key = element.key;
table[k]->code = element.code;
table[k]->next = NULL;
}
}
bool hashfind(int key,Element &element)//hash表的查找
{
int k = hashfunction(key);
if(table[k]!=NULL)
{
Element *e=table[k];
while(e!=NULL)
{
if(e->key == key)
{
element.key = e->key;
element.code = e->code;
return true;
}
e=e->next;
}
return false;
}
else
{
return false;
}
}
void compress(void)//壓縮程序
{
//打開一個流供寫入
FILE *fp;
fp = fopen("result.dat", "wb");

Element element;
int used;
char c;
int pcode, k;

for(int i=0;i<ascii;i++)
{
element.key = i;
element.code = i;
hashinsert(element);
}
used = ascii;

c = getchar();
pcode = c;
while((c = getchar()) != EOF)
{
k = (pcode << ByteSize) + c;
if(hashfind(k, element))
pcode = element.code;
else
{
//cout<<pcode<<' ';
fwrite(&pcode, sizeof(pcode), 1, fp);
element.code = used++;
element.key = (pcode << ByteSize) | c;
hashinsert(element);
pcode = c;
}
}
//cout<<pcode<<endl;
fwrite(&pcode, sizeof(pcode), 1, fp);

}
int main(void)
{
int t1,t2;

//欲壓縮的文本文件
//freopen("input.txt","r",stdin);
freopen("book5.txt","r",stdin);

t1=time(NULL);
hashinit();
compress();
t2=time(NULL);

cout<<"Compress complete! See result.dat."<<endl;
cout<<endl<<"Total use "<<t2-t1<<" seconds."<<endl;

7. 字元串壓縮與解壓縮

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

#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;
}

8. (20分)用C語言編譯的文件壓縮解壓縮程序

是用霍夫曼樹做的
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
struct head
{
unsigned char b; /*the charactor*/
long count; /*the frequency*/
long parent,lch,rch; /*make a tree*/
char bits[256]; /*the haffuman code*/
}
header[512],tmp;

void compress()
{
char filename[255],outputfile[255],buf[512];
unsigned char c;
long i,j,m,n,f;
long min1,pt1,flength;
FILE *ifp,*ofp;
printf("source filename:");
gets(filename);
ifp=fopen(filename,"rb");
if(ifp==NULL)
{
printf("source file open error!\n");
return;
}
printf("destination filename:");
gets(outputfile);
ofp=fopen(outputfile,"wb");
if(ofp==NULL)
{
printf("destination file open error!\n");
return;
}
flength=0;
while(!feof(ifp))
{
fread(&c,1,1,ifp);
header[c].count++;
flength++;
}
flength--;
header[c].count--;
for(i=0;i<512;i++)
{
if(header[i].count!=0) header[i].b=(unsigned char)i;
else header[i].b=0;
header[i].parent=-1;
header[i].lch=header[i].rch=-1;
}
for(i=0;i<256;i++)
{
for(j=i+1;j<256;j++)
{
if(header[i].count<header[j].count)
{
tmp=header[i];
header[i]=header[j];
header[j]=tmp;
}
}
}
for(i=0;i<256;i++) if(header[i].count==0) break;
n=i;
m=2*n-1;
for(i=n;i<m;i++)
{
min1=999999999;
for(j=0;j<i;j++)
{
if(header[j].parent!=-1) continue;
if(min1>header[j].count)
{
pt1=j;
min1=header[j].count;
continue;
}
}
header[i].count=header[pt1].count;
header[pt1].parent=i;
header[i].lch=pt1;
min1=999999999;
for(j=0;j<i;j++)
{
if(header[j].parent!=-1) continue;
if(min1>header[j].count)
{
pt1=j;
min1=header[j].count;
continue;
}
}
header[i].count+=header[pt1].count;
header[i].rch=pt1;
header[pt1].parent=i;
}
for(i=0;i<n;i++)
{
f=i;
header[i].bits[0]=0;
while(header[f].parent!=-1)
{
j=f;
f=header[f].parent;
if(header[f].lch==j)
{
j=strlen(header[i].bits);
memmove(header[i].bits+1,header[i].bits,j+1);
header[i].bits[0]='0';
}
else
{
j=strlen(header[i].bits);
memmove(header[i].bits+1,header[i].bits,j+1);
header[i].bits[0]='1';
}
}
}
fseek(ifp,0,SEEK_SET);
fwrite(&flength,sizeof(int),1,ofp);
fseek(ofp,8,SEEK_SET);
buf[0]=0;
f=0;
pt1=8;
while(!feof(ifp))
{
c=fgetc(ifp);
f++;
for(i=0;i<n;i++)
{
if(c==header[i].b) break;
}
strcat(buf,header[i].bits);
j=strlen(buf);
c=0;
while(j>=8)
{
for(i=0;i<8;i++)
{
if(buf[i]=='1') c=(c<<1)|1;
else c=c<<1;
}
fwrite(&c,1,1,ofp);
pt1++;
strcpy(buf,buf+8);
j=strlen(buf);
}
if(f==flength) break;
}
if(j>0)
{
strcat(buf,"00000000");
for(i=0;i<8;i++)
{
if(buf[i]=='1') c=(c<<1)|1;
else c=c<<1;
}
fwrite(&c,1,1,ofp);
pt1++;
}
fseek(ofp,4,SEEK_SET);
fwrite(&pt1,sizeof(long),1,ofp);
fseek(ofp,pt1,SEEK_SET);
fwrite(&n,sizeof(long),1,ofp);
for(i=0;i<n;i++)
{
fwrite(&(header[i].b),1,1,ofp);
c=strlen(header[i].bits);
fwrite(&c,1,1,ofp);
j=strlen(header[i].bits);
if(j%8!=0)
{
for(f=j%8;f<8;f++)
strcat(header[i].bits,"0");
}
while(header[i].bits[0]!=0)
{
c=0;
for(j=0;j<8;j++)
{
if(header[i].bits[j]=='1') c=(c<<1)|1;
else c=c<<1;
}
strcpy(header[i].bits,header[i].bits+8);
fwrite(&c,1,1,ofp);
}
}
fclose(ifp);
fclose(ofp);
printf("compress successfully!\n");
return;
}
void uncompress()
{
char filename[255],outputfile[255],buf[255],bx[255];
unsigned char c;
long i,j,m,n,f,p,l;
long flength;
FILE *ifp,*ofp;
printf("source filename:");
gets(filename);
ifp=fopen(filename,"rb");
if(ifp==NULL)
{
printf("source file open error!\n");
return;
}
printf("destination filename:");
gets(outputfile);
ofp=fopen(outputfile,"wb");
if(ofp==NULL)
{
printf("destination file open error!\n");
return;
}
fread(&flength,sizeof(long),1,ifp);
fread(&f,sizeof(long),1,ifp);
fseek(ifp,f,SEEK_SET);
fread(&n,sizeof(long),1,ifp);
for(i=0;i<n;i++)
{
fread(&header[i].b,1,1,ifp);
fread(&c,1,1,ifp);
p=(long)c;
header[i].count=p;
header[i].bits[0]=0;
if(p%8>0) m=p/8+1;
else m=p/8;
for(j=0;j<m;j++)
{
fread(&c,1,1,ifp);
f=c;
itoa(f,buf,2);
f=strlen(buf);
for(l=8;l>f;l--)
{
strcat(header[i].bits,"0");
}
strcat(header[i].bits,buf);
}
header[i].bits[p]=0;
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strlen(header[i].bits)>strlen(header[j].bits))
{
tmp=header[i];
header[i]=header[j];
header[j]=tmp;
}
}
}
p=strlen(header[n-1].bits);
fseek(ifp,8,SEEK_SET);
m=0;
bx[0]=0;
while(1)
{
while(strlen(bx)<(unsigned int)p)
{
fread(&c,1,1,ifp);
f=c;
itoa(f,buf,2);
f=strlen(buf);
for(l=8;l>f;l--)
{
strcat(bx,"0");
}
strcat(bx,buf);
}
for(i=0;i<n;i++)
{
if(memcmp(header[i].bits,bx,header[i].count)==0) break;
}
strcpy(bx,bx+header[i].count);
c=header[i].b;
fwrite(&c,1,1,ofp);
m++;
if(m==flength) break;
}
fclose(ifp);
fclose(ofp);
printf("Uncompress successfully!\n");
return;
}
int main()
{
int c;
printf("1--Compress file\n");
printf("2--Uncompress file\n");
printf("Select 1 or 2:");
c=getch();
printf("%c\n",c);
if(c=='1') compress();
else if(c=='2') uncompress();
return 0;
}

9. 用c語言編譯個簡單壓縮程序

/*
流程是:
1.讀取一個字元,寫入A
2.再讀取一個字元,寫入B
3.判斷AB是否相等,相等轉4,否則轉5
4.一直讀,同時統計數量,直到讀取到和A不相等的字元,將改字元寫入B,轉5
5.將對應數據寫入文件
下面是偽代碼
*/
charA;
charB;
inti;
FilewriteFile;//要寫入的文件
FilereadFile;//要讀的文件
A=readChar(readFile);//讀一個字元
while(文件未讀完){
B=readChar(readFile);//讀一個字元
if(A==B){
i=2;
while((B=readChar(readFile))==A){//一直讀,直到讀取的字元和A不一樣
i++;
}
write(writeFile,A+"$"+i);//i代表重復數量
A=B;
}else{
write(writeFile,A);
A=B;
}
}

10. C語言實現文件壓縮

typedef int (WINAPI ICEPUB_COMPRESSFILE)(char *strFilename, char *strZipFilename);
ICEPUB_COMPRESSFILE *icePub_compressFile = 0;
HINSTANCE hDLLDrv = LoadLibrary("icePubDll.dll");
if(hDLLDrv)
{
icePub_compressFile = (ICEPUB_COMPRESSFILE *)GetProcAddress(hDLLDrv, "icePub_compressFile");
}

if(icePub_compressFile)
icePub_compressFile("a.exe","a.Z");

if(hDLLDrv)
FreeLibrary(hDLLDrv);

typedef int (WINAPI ICEPUB_UNCOMPRESSFILE)(char *strZipFilename,char *strFilename);
ICEPUB_UNCOMPRESSFILE *icePub_uncompressFile = 0;
HINSTANCE hDLLDrv = LoadLibrary("icePubDll.dll");
if(hDLLDrv)
{
icePub_uncompressFile = (ICEPUB_UNCOMPRESSFILE *)GetProcAddress(hDLLDrv, "icePub_uncompressFile");
}

if(icePub_uncompressFile)
icePub_uncompressFile("a.Z","a.exe");

if(hDLLDrv)
FreeLibrary(hDLLDrv);