导航:首页 > 文件处理 > 算术编码压缩的c语言代码实现

算术编码压缩的c语言代码实现

发布时间:2023-04-21 13:34:23

‘壹’ 如何用C语言实现数据压缩

首先选择一个压缩算法

然后按照算法实现压缩代码,调用接口就可以
常见的 可以使用哈夫曼编码压缩,或者使用开源的压缩代码,比如lzo, gzip, lzma等等。

‘贰’ 用C语言指针实现字符串压缩

#include<stdio.h>
#define MAX_NUM 32int main()
{ char *p,str[60];
int i,j=0;
static int num[26];
p=(char*)malloc(MAX_NUM*sizeof(char));
gets(str);
for(i=0;str[i];i++)
{ if(str[i]==' ') p[j++]=str[i];
else {
if(num[str[i]-97]==0||num[str[i]-97]==2||num[str[i]-97]==5)
{ p[j++]=str[i]; num[str[i]-97]++;}
else num[str[i]-97]++;
} }
for(i=0;i<j;i++)
putchar(p[i]);
getch();
return 0;
}注:输入的为小写字母,而且句子长度不超过60个字符,保存字数不超过32个。。。。。在win_tc中通过……

‘叁’ (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;
}

‘肆’ 求:C语言的游程编码,要求将大量的二进制的数据压缩

游程数就多,变化少的部分游程数就少,原始栅格类型越简单,压缩效率就越高

‘伍’ 使用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;
}

‘陆’ 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);

‘柒’ C语言求助:请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。

用下面的代码给你提供个思路。这代码连续字符不得超过9个……
#include "stdio.h"
void main(void){
char a[1000]="gcccddecc";
int i,j,k,n;
printf("Type an integer(a~z)...\nStr=");
gets(a);
for(k=i=0;a[i];i++){
for(j=i+1,n=0;a[i]==a[j];j++) n++;
if(n){
a[k++]=n+'1';
a[k++]=a[--j];
i+=n;
}
else a[k++]=a[i];
}
a[k]='\0';
printf("The result = %s.\n",a);
}

‘捌’ 用c语言实现算术编码和解码

Turbo c 2.0编译通过

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#defineLENGTH100 /*字符串(编码前或编码后)的最大长度*/

/*编码*/
voidencode(char*strsource);
/*解码*/
voiddecode(char*strcode);

voidmain()
{裤和
橡帆charcode[LENGTH]="BILLGATES";
encode(code);
printf("\nencodedstringis:%s\n",code);
decode(code);
printf("\ndecodedstringis:%s\n",code);
getch();
}
voidencode(char*strsource){
char*p=strsource,tmp[LENGTH]={'\0'},buffer[3];
while(*p){
itoa(*p++,buffer,10);
strcat(tmp,"%");
strcat(tmp,buffer);
}
梁纯雹strcpy(strsource,tmp);
}

voiddecode(char*strcode){
inti=0;
char*p,*s=strcode,tmp[LENGTH]={'\0'};
char*cSplit="%";
p=strtok(s,cSplit);
while(p)
{
tmp[i++]=atoi(p);
p=strtok(NULL,cSplit);
}
strcpy(strcode,tmp);
}

阅读全文

与算术编码压缩的c语言代码实现相关的资料

热点内容
程序员试用期汇报问题协助怎么写 浏览:127
抖音算法到底是什么 浏览:126
哪个vlan技术对报文加密 浏览:570
单片机定时电路 浏览:672
山西平台服务器云主机 浏览:700
按摩肚脐解压视频 浏览:989
php55安装教程 浏览:137
云服务器怎么查找本机域名 浏览:22
qd123y压缩机参数 浏览:385
程序员妈妈怀孕 浏览:490
金普国际编程 浏览:537
java什么是引用类型 浏览:944
这是命令吗txt 浏览:314
支付宝android包名 浏览:154
eclipsemaven命令 浏览:68
24路服务器配什么cpu 浏览:466
压缩文件和解压文件哪个快 浏览:675
亚马逊云服务器视频通话 浏览:912
金融知识app哪个好 浏览:978
农行理财app收益在哪里 浏览:969