c语言文件压缩与解压缩实验报告,哈弗曼树的文件压缩和解压实验报告(C语言).doc...
Lab05 樹結構的應用
學號: 姓名: 實驗時間:2011.5.24
1.問題描述
哈弗曼樹的編碼與譯碼
— 功能:實現對任何類型文件的壓縮與解碼
— 輸入:源文件,壓縮文件
— 輸出:解碼正確性判定,統計壓縮率、編碼與解碼速度
— 要求: 使用邊編碼邊統計符號概率的方法(自適應Huffman編碼) 和事先統計概率的方法(靜態Huffman編碼) 。
2.1程序清單
程序書簽:
main函數
壓縮函數
select函數
encode函數
解壓函數
#include
#include
#include
#include
#include
struct node{
long weight; //權值
unsigned char ch;//字符
int parent,lchild,rchild;
char code[256];//編碼的位數最多為256位
int CodeLength;//編碼長度
}hfmnode[512];
void compress();
void uncompress();
//主函數
void main()
{
int choice;
printf("請選擇1~3:\n");
printf("1.壓縮文件\n");
printf("2.解壓文件\n");
printf("3.退出!\n");
scanf("%d",&choice);
if(choice==1)compress();
else if(choice==2)uncompress();
else if(choice==3)return;
else printf("輸入錯誤!");
}
//壓縮函數
void compress()
{
int i,j;
char infile[20],outfile[20];
FILE *ifp,*ofp;
unsigned char c;//
long FileLength,filelength=0;
int n,m;//葉子數和結點數
int s1,s2; //權值最小的兩個結點的標號
char codes[256];
long sumlength=0;
float rate,speed;
int count=0;
clock_t start1, start2,finish1,finish2;
double duration1,duration2;
void encode(struct node *nodep,int n);//編碼函數
int select(struct node *nodep,int pose);//用于建哈弗曼樹中選擇權值最小的結點的函數
printf("請輸入要壓縮的文件名:");
scanf("%s",infile);
ifp=fopen(infile,"rb");
if(ifp==NULL)
{
printf("文件名輸入錯誤,文件不存在!\n");
return;
}
printf("請輸入目標文件名:");
scanf("%s",outfile);
ofp=fopen(outfile,"wb");
if(ofp==NULL)
{
printf("文件名輸入錯誤,文件不存在!\n");
return;
}
start1=clock() ;//開始計時1
//統計文件中字符的種類以及各類字符的個數
//先用字符的ASCII碼值代替結點下標
FileLength=0;
while(!feof(ifp))
{
fread(&c,1,1,ifp);
hfmnode[c].weight++;
FileLength++;
}
FileLength--; //文件中最后一個字符的個數會多統計一次,所以要減一
hfmnode[c].weight--;
//再將ASCII轉換為字符存入到結點的ch成員里,同時給雙親、孩子賦初值-1
n=0;
for(i=0;i<256;i++)
if(hfmnode[i].weight!=0)
{
hfmnode[i].ch=(unsigned
總結
以上是生活随笔為你收集整理的c语言文件压缩与解压缩实验报告,哈弗曼树的文件压缩和解压实验报告(C语言).doc...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构之并查集:路径压缩继续优化并查集
- 下一篇: linux查看登录服务器的ip历史记录,