1、实验三LZW编码实验三 LZW编码一、实验目的1、加深对LZW编码的理解;2、掌握LZW编码的程序设计。二、原理与说明LZW编码的步骤:串表a1aab7b2bc8c3cb9aa4baa10ab5aaba11ba6abb12a a b aa b c ba aab ab bc1 1 2 4 2 3 6 7 5 8三、实验内容利用C、VB或VC语言实现LZW编码的算法,掌握LZW编码的程序设计并调试通过。四、实验设备计算机程序如下:#include#include#include#include#include#define BIRS 12#define HASHING_SHIFT BITS-8#d
2、efine MAX_VALUE(1BITS)-1#define MAX_CODE MAX_VALUE-1#if BITS=14#define TABLE_SIZE 18041#endif#if BITS=13#define TABLE_SIZE 9029#endif#if BITS=12#define TABLE_SIZE 5021int *code_value;unsigned int *prefix_code;unsigned char *append_character;unsigned char decode_stack4000;char ok;find match(int hash_
3、perfix,unsigned int hash_character) int index; int offset; index=(hash_characterHASHING_SHIFT)hash_prefix; if(index=0) offset=1; else offset=TABLE_SIZE-index; while(1) if(code_valueindex=-1) return(index); if(prefix_codeindex=hash_prefix&append_characterindex=hash_character) return(index); index-=of
4、fset; if(index0) index+=TABLE_SIZE; input_code(FILE*input) unsigned int return_value; static int input_bit_count=0; static unsigned long input_bit_buffer=0L; while(input_bit_count=24) input_bit_buffer|=(unsigned long)getc(input)(32-BITS); input_bit_buffer=BITS; input_bit_count-=BITS; return(return_v
5、alue);void output_code(FILE*output,unsigned int code) static int output_bit_count=0; static unsigned long output_bit_buffer=0L; output_bit_buffer|=(unsigned long)code=8) putc(output_bit_buffer24,output); output_bit_buffer=8; output_bit_count-=8; void compress(FILE *input,FILE *output) unsigned int n
6、ext_code; unsigned int character; unsigned int string_code; unsigned int index; int i; next_code=256; for(i=0;iTABLE_SIZE;i+) code_valuei=-1; i=0; printf(nnCompressing.n); string_code=getc(input); while(character=getc(input)!=(unsigned)EOF) index=find_match(string_code,character); if(code_valueindex
7、!=-1) string_code=code_valueindex; else if(next_code=next_code) *decode_stack=character; string=(unsigned char*)decode_string(decode_stcak+1,old_code); else string=(unsigned char*)decode_string(decode_stck,nex_code); character=*string; while(string=decode_stack) putc(*string-,output); if(next_code25
8、5) *buffer+=append_charactercode; code=prefix_codecode; if(i+=4094) printf(Fatal error during code expansion.n); exit(0); *buffer=code; return(char*)buffer); int main(int argc,char*argv) FILE*input_file; FILE*output_file; FILE*lzw_file; char input_file_name81; int select; character=*string; while(st
9、ring=decode_stack) putc(*string-,output); if(next_code1) strcpy(input_file_name,argv1); else printf(nInput file name?); scanf(%s,input_file_name); printf(nCompressed file name?); scanf(%s,compressed_file_name); input_file=fopen(input_file_name,rb); lzw_file=fopen(compressed_filename,wb); while(input
10、_file=NULL|lzw_file=NULL) printf(Fatal error opening files!n); printf(nInput file names?); scanf(%s,input_file_name); printf(nCompressed file name?); scanf(%s,compressed_file_name); input_file=fopen(input_file_name,rb); lzw_file=fopen(compressed_filename,wb); ; compress(input_file,lzw_file); fclose(
11、input_file); fclode(lzw_file); free(code_value); else if(select=2) printf(nOnput file names?); scanf(%s,onput_filename); printf(nExpanded file name?); scanf(%s,expanded_filename); input_file=fopen(onput_filename,rb); lzw_file=fopen(expanded_filename,wb); while(lzw_file=NULL|output_file=NULL) printf(
12、Fatal error opening files!n); printf(nOnput file names?); scanf(%s,onput_filename); printf(nExpanded file name?); 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(nContinue or not(y/n)?); scanf(%c,&ok); getchar(); if(ok=y) goto loop; else printf(Complete.nnPress any key to continue); getchar(); free(prefix_code); free(append_character); return 0;
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2