1.c语言字符串如何压缩
2.(20分)用C语言编译的语言压缩源码语言压缩源码文件压缩解压缩程序
c语言字符串如何压缩
C语言中的字符串压缩可以通过观察连续字符的重复次数实现。以下代码示例展示了如何实现这个功能:
在C语言中,程序程序字符串压缩通常涉及检测连续字符并记录其出现次数。多少模拟攒机网页源码以下是语言压缩源码语言压缩源码一个简单的函数,用于压缩输入字符串a,程序程序防cc跳转源码将其结果存储在数组b中:
cpp
#include
#include
#include
using namespace std;
void compressString(char a[],多少源码资本投资保险 char b[]) {
int count = 1, p = 0;
for (int i = 0; i < strlen(a); i++) {
if (a[i] == a[i + 1]) {
count++;
} else {
if (count > 2) {
b[p++] = (char)(count + '0');
b[p++] = a[i];
count = 1;
} else if (count == 2) {
b[p++] = a[i];
b[p++] = a[i];
count = 1;
} else {
b[p++] = a[i];
}
}
}
}
void printB(char b[]) {
cout << b << endl;
}
void decompressB(char b[]) {
for (int i = 0; i < strlen(b); i++) {
if (b[i] >= '3' && b[i] <= '9') {
for (int j = 0; j < (int)(b[i] - '0'); j++) {
cout << b[i + 1];
i++;
}
} else {
cout << b[i];
}
}
cout << endl;
}
int main() {
char a[] = { 0}, b[] = { 0};
gets(a);
compressString(a, b);
printB(b);
decompressB(b);
}
这段代码首先定义了一个压缩函数`compressString`,它会遍历输入字符串a,语言压缩源码语言压缩源码检测连续字符的程序程序重复次数。如果连续字符超过两次,多少它会将数字和字符合并存储到b数组中。语言压缩源码语言压缩源码最后,程序程序`decompressB`函数用于恢复原始字符串的多少结构。
在`main`函数中,语言压缩源码语言压缩源码图书公众号源码我们读取一个字符串,程序程序调用压缩函数,多少股价X线源码然后输出压缩后的字符串并进行解压,以验证压缩效果。
(分)用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[]; /*the haffuman code*/
}
header[],tmp;
void compress()
{
char filename[],outputfile[],buf[];
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<;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<;i++)
{
for(j=i+1;j<;j++)
{
if(header[i].count<header[j].count)
{
tmp=header[i];
header[i]=header[j];
header[j]=tmp;
}
}
}
for(i=0;i<;i++) if(header[i].count==0) break;
n=i;
m=2*n-1;
for(i=n;i<m;i++)
{
min1=;
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=;
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,"");
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[],outputfile[],buf[],bx[];
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;
}