Huffman编码,现在的编码节选:
/*********************************************************************************
Huffman_Compression ()
用Huffman编码压缩指定的文件,并将结果存为新的文件
********************************************************************************/
int Huffman_Compression(char * infilename, char * outfilename)
{
if ((ifile = fopen (infilename, "rb")) != NULL)
{
fseek (ifile, 0L, 2);
file_size = (unsigned long) ftell (ifile);//文件的长度
fseek (ifile, 0L, 0);
get_frequency_count ();//本函数对需要进行压缩的数据进行频率统计
build_initial_heap ();
build_code_tree ();
if (!generate_code_table ())
{
printf (" \nERROR: 已超出编码范围,无法进行压缩!\n");
return 0;
}
else
{
if ((ofile = fopen (outfilename, "wb")) != NULL)
{
fwrite (&file_size, sizeof (file_size), 1, ofile);
fwrite (code, 2, 256, ofile);
fwrite (code_length, 1, 256, ofile);
fseek (ifile, 0L, 0);
compress_image ();
fclose (ofile);
}
else
{
printf("\nERROR: 无法创建输出文件 ! %s\n", outfilename);
return 0;
}
}
fclose (ifile);
}
else
{
printf ("\nERROR: %s -- 未找到文件 !\n", infilename);
return 0;
}
return 1;
}
我现在想要的是运行以后用户再自行输入要操作的文件名,而不是在源代码中规定~~~求指导!!!
/* 主程序 */
void main()
{
int choice;
display();
printf("请输入需要执行的操作并按回车(数字序号):\n");
while(choice<1||choice>3){
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n压缩...");
start = clock();
Huffman_Compression("2.bmp","3.hufzip");//压缩
finish = clock();
total_time = finish - start;
printf("\n耗时: %d毫秒\n", total_time);
break;
题目补充了部分,因为字数限制,case2没粘
追答s", file1 );