赫夫曼压缩文件作业

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef char *String;
static String thr[256];    //每个字符的编码
typedef struct{
    char ch;        
    long weight,parent,lchild,rchild;
}HTNode;
typedef struct{
    HTNode data[512];    //0不用 
    int root;    //根 
}HTree;
	HTree T;
	long charNum[256];    //每个字符出现的次数
	int oneChar=0,emptyFile=0;
	char fileName[1000];
void getCharNum(FILE *&fp)
{              
    fseek(fp,0L,SEEK_SET);    //指针移到文件开头
    fseek(fp,0L,SEEK_CUR);    //切换读写指针 
    char ch;    
    while(1){ 
        fread(&ch,1,1,fp);    //读内容
        if(feof(fp))break;    //文件结束 
        charNum[ch]++;        
    }
}
//获得有两个最小权重的字符 返回0表示找到根节点并给root赋值 否则返回结束位置
int getSmallestWeight(int &a,int &b){         
    int i;
    a=b=0;
    for(i=1;i<512&&T.data[i].weight;i++)    
		if(T.data[i].parent==0)
		{
			a=i;   
			break;
		}        
    for(i++;i<512&&T.data[i].weight;i++)    
		if(T.data[i].parent==0)
		{
			b=i;   
			break;
		}        
    if(b==0){    //第一次true时找到a(root)但是没有b 
        T.root=a;
        return 0;     //只有根 
    }
    for(i++;i<512&&T.data[i].weight;i++){
        if(T.data[i].parent==0&&T.data[i].weight<T.data[a].weight)a=i;        
        else if(T.data[i].parent==0&&T.data[i].weight<T.data[b].weight)b=i;
    }    //获取权重最小的两个
    return i; 
}
//生成赫夫曼树
void createHTree(){        
    int j=1,a,b,pos,nnum=0; 
    for(int i=0;i<=255;i++){
        if(charNum[i]==0)continue;       
        nnum++;
        T.data[j].rchild=T.data[j].lchild=T.data[j].parent=0;
        T.data[j].weight=charNum[i];
        T.data[j++].ch=i;         
    } 
    if(nnum==0)    {    emptyFile=1;    return;}        //空文件 
    else if(nnum==1){    oneChar=1;    return;}        //只有一个字符 
    while(pos=getSmallestWeight(a,b)){    //找两个最小的
        T.data[pos].lchild=a;
        T.data[pos].rchild=b;
        T.data[pos].parent=0;
        T.data[pos].weight=T.data[a].weight+T.data[b].weight;
        T.data[a].parent=T.data[b].parent=pos;
    }
}
//生成每个字符的赫夫曼编码
void createHuffmanCode(){    
    if(emptyFile==1||oneChar==1)return;
    for(int i=1;!T.data[i].lchild;i++){    //遍历所有叶子 
        int j=i,p=0;
        char temp[300]={0}; 
        while(T.data[j].parent){        //没到根的时候 
            temp[p++]=(T.data[T.data[j].parent].rchild==j)+'0';    //右子树时为1 左子树为0 
            j=T.data[j].parent;
        }
        thr[T.data[i].ch]=(String) malloc(sizeof(char)*(p+1));
        strcpy(thr[T.data[i].ch],strrev(temp));    //逆置 
    }
	
}
//生成压缩文件
int writeCompFile(FILE *&fp1){         
	char chh;
	int a=1;
	FILE *fp3=fopen(fileName,"r");
	FILE *fp2=fopen("压缩后文件.txt","w");
	while ((chh=fgetc(fp3))!=EOF)
	{
	fputs(thr[chh],fp2);
	}
	fclose(fp3);
    fclose(fp2);
	printf("下面是编码表:\n");
	while(T.data[a].ch)
	{
		printf("%c    %s\n",T.data[a].ch,thr[T.data[a].ch]);
		a++;
	}//查看编码表
    return 0;
} 
void comp(FILE *&fp1){   
    printf("请耐心等待...\n");
    getCharNum(fp1);    //统计文件中每个字符出现的次数
    createHTree();        //生成赫夫曼树
    createHuffmanCode();    //生成赫夫曼编码 
	writeCompFile(fp1);         //生成压缩文件
	printf("压缩完成,压缩后文件已保存至vc++6.0对应文件中\n");
}
int main ()
{
	printf("下面输入文件路径(拖放入.txt文件即可,注意文件中不能含有中文字符)\n");
	gets(fileName);    //读入文件名 
	FILE *fp1=fopen(fileName,"r");    //打开文件 
	printf("开始压缩\n");
	comp(fp1);        //压缩
	fclose(fp1);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值