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;
}
}