用c语言实现倍增原理,RLE算法变体C语言实现

RLE算法:这种压缩编码是一种变长的编码,RLE根据文本不同的具体情况会有不同的压缩编码变体与之相适应,以产生更大的压缩比率。

变体:重复次数+字符

文本字符串:A A A B B B C C C C D D D D,编码后得到:3 A 3 B 4 C 4 D。

/***********************************************************************************************************

RLE.c

本演示程序提供了RLE的压缩和解压缩函数

**********************************************************************************************************/

#include

#include

#include

/* 函数原型 */

int RLE_Compression(char * infile_name, char * outfile_name);

int RLE_Decompression(char * infile_name, char * outfile_name);

/* 主程序 */

void main(int argc, char *argv[])

{

printf("RLE compression and decompression utility/n");

if (4 != argc)

{

printf("/nUsage : rle -c|d sourcefilename targetfilename/n");

exit(0);

}

if (! strcmp(argv[1], "-c"))

{

printf("/nCompress...");

RLE_Compression(argv[2], argv[3]);

}

else if (! strcmp(argv[1], "-d"))

{

printf("/nDecompress...");

RLE_Decompression(argv[2], argv[3]);

}

else

printf("/nUnknow command./n");

}

/**************************************************************************

RLE_Decompression ()

本函数用RLE算法对文件进行解压缩

**************************************************************************/

int RLE_Decompression(char * infile_name, char * outfile_name)

{

register int seq_len, i;

char scratch_space[255],cur_char;

FILE *infile, *outfile;

if ((infile=fopen(infile_name, "rb")) == NULL)

{

strcpy(scratch_space, "Unable to open ");

strcat(scratch_space, infile_name);

puts(scratch_space);

return 1;

}

if ((outfile=fopen(outfile_name, "wb")) == NULL)

{

strcpy(scratch_space, "Unable to open ");

strcat(scratch_space, outfile_name);

puts(scratch_space);

return 1;

}

if ( feof(infile) )

{

return 0;

}

while (!feof(infile))

{

seq_len = (int)fgetc( infile );

cur_char = fgetc( infile );

for ( i = 0; i < seq_len; i++ )

{

fputc( cur_char, outfile );

}

}

fclose(infile);

fclose(outfile);

return 0;

}

/**************************************************************************

RLE_Compression ()

本函数用RLE算法对文件进行压缩

**************************************************************************/

int RLE_Compression(char * infile_name, char * outfile_name)

{

register int seq_len;

char scratch_space[255],cur_char, cur_seq;

FILE *infile, *outfile;

if ((infile=fopen(infile_name, "rb")) == NULL)

{

strcpy(scratch_space, "Unable to open ");

strcat(scratch_space, infile_name);

puts(scratch_space);

return 1;

}

if ((outfile=fopen(outfile_name, "wb")) == NULL)

{

strcpy(scratch_space, "Unable to open ");

strcat(scratch_space, outfile_name);

puts(scratch_space);

return 1;

}

if ( feof(infile) )

{

return 0;

}

cur_char = fgetc(infile);

cur_seq = cur_char;

seq_len = 1;

while (!feof(infile))

{

cur_char = fgetc(infile);

if ( cur_char == cur_seq )

{

seq_len++;

}

else

{

fputc( seq_len, outfile );

fputc( cur_seq, outfile );

cur_seq = cur_char;

seq_len = 1;

}

}

fclose( infile );

fclose( outfile );

return 0;

}

以上算法比较适合重复次数比较多的文件,否则不但不能达到压缩的效果,反而使文件倍增!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值