实验三LZW编码.docx

上传人:b****5 文档编号:14963125 上传时间:2023-06-28 格式:DOCX 页数:11 大小:15.96KB
下载 相关 举报
实验三LZW编码.docx_第1页
第1页 / 共11页
实验三LZW编码.docx_第2页
第2页 / 共11页
实验三LZW编码.docx_第3页
第3页 / 共11页
实验三LZW编码.docx_第4页
第4页 / 共11页
实验三LZW编码.docx_第5页
第5页 / 共11页
实验三LZW编码.docx_第6页
第6页 / 共11页
实验三LZW编码.docx_第7页
第7页 / 共11页
实验三LZW编码.docx_第8页
第8页 / 共11页
实验三LZW编码.docx_第9页
第9页 / 共11页
实验三LZW编码.docx_第10页
第10页 / 共11页
实验三LZW编码.docx_第11页
第11页 / 共11页
亲,该文档总共11页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

实验三LZW编码.docx

《实验三LZW编码.docx》由会员分享,可在线阅读,更多相关《实验三LZW编码.docx(11页珍藏版)》请在冰点文库上搜索。

实验三LZW编码.docx

实验三LZW编码

实验三LZW编码

一、实验目的

1、加深对LZW编码的理解;

2、掌握LZW编码的程序设计。

二、原理与说明

LZW编码的步骤:

串表

a

1

aab

7

b

2

bc

8

c

3

cb

9

aa

4

baa

10

ab

5

aaba

11

ba

6

abb

12

aabaabcbaaababbc

1124236758

三、实验内容

利用C、VB或VC语言实现LZW编码的算法,掌握LZW编码的程序设计并调试通过。

四、实验设备

计算机

程序如下:

#include<>

#include""

#include""

#include""

#include""

#defineBIRS12

#defineHASHING_SHIFTBITS-8

#defineMAX_VALUE(1<

#defineMAX_CODEMAX_VALUE-1

#ifBITS==14

#defineTABLE_SIZE18041

#endif

#ifBITS==13

#defineTABLE_SIZE9029

#endif

#ifBITS<=12

#defineTABLE_SIZE5021

int*code_value;

unsignedint*prefix_code;

unsignedchar*append_character;

unsignedchardecode_stack[4000];

charok;

findmatch(inthash_perfix,unsignedinthash_character)

{

intindex;

intoffset;

index=(hash_character<

if(index==0)

offset=1;

else

offset=TABLE_SIZE-index;

while

(1)

{

if(code_value[index]==-1)

return(index);

if(prefix_code[index]==hash_prefix&&append_character[index]==hash_character)

return(index);

index-=offset;

if(index<0)

index+=TABLE_SIZE;

}

}

input_code(FILE*input)

{

unsignedintreturn_value;

staticintinput_bit_count=0;

staticunsignedlonginput_bit_buffer=0L;

while(input_bit_count<=24)

{

input_bit_buffer|=(unsignedlong)getc(input)<<(24-input_bit_count);

input_bit_count+=8;

}

return_value=input_bit_buffer>>(32-BITS);

input_bit_buffer<<=BITS;

input_bit_count-=BITS;

return(return_value);

}

voidoutput_code(FILE*output,unsignedintcode)

{

staticintoutput_bit_count=0;

staticunsignedlongoutput_bit_buffer=0L;

output_bit_buffer|=(unsignedlong)code<<(32-BITS-output_bit_count);

output_bit_count+=BITS;

while(output_bit_count>=8)

{

putc(output_bit_buffer>>24,output);

output_bit_buffer<<=8;

output_bit_count-=8;

}

}

voidcompress(FILE*input,FILE*output)

{

unsignedintnext_code;

unsignedintcharacter;

unsignedintstring_code;

unsignedintindex;

inti;

next_code=256;

for(i=0;i

code_value[i]=-1;

i=0;

printf("\n\nCompressing...\n);

string_code=getc(input);

while((character=getc(input))!

=(unsigned)EOF)

{index=find_match(string_code,character);

if(code_value[index]!

=-1)

string_code=code_value[index];

else

{

if(next_code<=MAX_CODE)

{

code_value[index]=next_code++;

prefix_code[index]=string_code;

append_character[index]=character;

}

output_code(output,string_code);

string_code=character;

}

}

output_code(output,string_code);

output_code(output,MAX_VALUE);

output_code(output,0);

printf("\n");

getchar();

voidexpand(FILE*input,FILE*output)

{

unsignedintnext_code;

unsignedintnex_code;

unsignedintold_code;

intcharacter;

intcounter;

unsignedchar*string;

char*decode_string(unsignedchar*buffer,unsignedintcode)l

next_code=256;

counter=0;

printf("\n\nExpanding...\n");

old_code=input_code(input);

character=old_code;

putc(old_code,output);

while((nex_code=input_code(input))!

=(MAX_VALUE))

{

if(new_code>=next_code)

{

*decode_stack=character;

string=(unsignedchar*)decode_string(decode_stcak+1,old_code);

}

else

string=(unsignedchar*)decode_string(decode_stck,nex_code);

character=*string;

while(string>=decode_stack)

putc(*string--,output);

if(next_code<=MAX_CODE)

{

prefix_code[next_code]=old_code;

append_character[next_code]=character;

next_code++;

}

old_code=nex_code;

}

printf("\n");

getchar();

}

char*decode_string(unsignedchar*buffer,unsignedintcode)

{inti;

i=0;

while(code>255)

{

*buffer++=append_character[code];

code=prefix_code[code];

if(i++>=4094)

{

printf("Fatalerrorduringcodeexpansion.\n");

exit(0);

}

}

*buffer=code;

return((char*)buffer);

intmain(intargc,char*argv[])

{

FILE*input_file;

FILE*output_file;

FILE*lzw_file;

charinput_file_name[81];

intselect;

character=*string;

while(string>=decode_stack)

putc(*string--,output);

if(next_code<=MAX_CODE)

{

prefix_code[next_code]=old_code;

append_character[next_code]=character;

next_code++;

}

old_code=new_code;

}

printf("\n");

getchar();

}

printf("**\n");

printf("*Pleaseselectaopt.*\n");

printf("**********************\n");

scanf("%d",&select);

if(select==1)

{

if(argc>1)

strcpy(input_file_name,argv[1]);

else

{printf("\nInputfilename?

");

scanf("%s",input_file_name);

printf("\nCompressedfilename?

");

scanf("%s",compressed_file_name);

}

input_file=fopen(input_file_name,"rb");

lzw_file=fopen(compressed_filename,"wb");

while(input_file==NULL||lzw_file==NULL)

{

printf("Fatalerroropeningfiles!

\n");

printf("\nInputfilenames?

");

scanf("%s",input_file_name);

printf("\nCompressedfilename?

");

scanf("%s",compressed_file_name);

input_file=fopen(input_file_name,"rb");

lzw_file=fopen(compressed_filename,"wb");

};

compress(input_file,lzw_file);

fclose(input_file);

fclode(lzw_file);

free(code_value);

elseif(select==2)

{printf("\nOnputfilenames?

");

scanf("%s",onput_filename);

printf("\nExpandedfilename?

");

scanf("%s",expanded_filename);

input_file=fopen(onput_filename,"rb");

lzw_file=fopen(expanded_filename,"wb");

while(lzw_file==NULL||output_file==NULL)

{printf("Fatalerroropeningfiles!

\n");

printf("\nOnputfilenames?

");

scanf("%s",onput_filename);

printf("\nExpandedfilename?

");

scanf("%s",expanded_filename);

input_file=fopen(onput_filename,"rb");

lzw_file=fopen(expanded_filename,"wb");

};

expand(lzw_file,output_file);

fclose(lzw_file);

fclose(output_file);

}

else

{exit(0);

}

printf("\nContinueornot(y/n)?

");

scanf("%c",&ok);

getchar();

if(ok=='y')

{gotoloop;

}

else{

printf("Complete......\n\nPressanykeytocontinue");

getchar();

free(prefix_code);

free(append_character);}

return0;

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2