电子科技大学-计算机学院-软件开发环境实验-李林教学班-霍夫曼编码压缩解压缩
- 格式:doc
- 大小:139.00 KB
- 文档页数:12
//HuffmanFunction.h #include #define MAX_NODE_NUM 512 typedef struct{ int bitNum; int parent; int lchild; int rchild; int weight; //bool code[1024];//每个字符的编码最大支持1024个单位长度 bool* code; int codeLength; bool isLeaf; int floor; }HuffmanNode;
class HuffmanFunction { private: bool bit(char c,int num); long getFileSize(FILE* stream); bool initializeHuffmanTree(HuffmanNode* huffmanTree,const char* sourceFile); bool destroyHuffmanTree(HuffmanNode* huffmanTree); int statisticsOfHuffmanNodes(HuffmanNode* huffmanTree,char* sourceFile); bool getFirstAndSecondMinNode(int* firstMinNode,int* secondMinNode,const HuffmanNode* huffmanTree,const int* isSigned); bool generateHuffmanCode(HuffmanNode* huffmanTree,const int index); bool setHuffmanFloor(HuffmanNode* huffmanTree,const int index); int generateHuffmanTree(HuffmanNode* huffmanTree,int* isSigned); int generateFileCode(const HuffmanNode* huffmanTree,const char* sourceFile,bool* fileCode); bool createCompressFile(const char* destFile,const bool* fileCode,int fileCodeLength);
public: bool compressFile(char* sourceFile,char* destFile); bool uncompressFile(char* sourceFile,char* destFile); HuffmanFunction(); ~HuffmanFunction(); }; // stdafx.h : 标准系统包含文件的包含文件, // 或是经常使用但不常更改的 // 特定于项目的包含文件 //
#pragma once #include #include #include #include "HuffmanFunction.h"
// TODO: 在此处引用程序需要的其他头文件 // HuffmanClass.cpp : 定义控制台应用程序的入口点。 //
#include "stdafx.h"
int main(int argc,char **argv) {
if(argc!=4) { printf("输入不合法\n"); return 0; } else { HuffmanFunction* function = new HuffmanFunction(); if(!strcmp(argv[1],"-c")) function->compressFile(argv[2],argv[3]); else if(!strcmp(argv[1],"-u")) function->uncompressFile(argv[2],argv[3]); else { printf("输入不合法\n"); return 0; } }
return 1; } #include "HuffmanFunction.h" #include #include HuffmanFunction::HuffmanFunction() { } HuffmanFunction::~HuffmanFunction() { } //获得字符的某一位 bool HuffmanFunction::bit(char c,int num) { return (c&(1<>num; } //获得文件大小 long HuffmanFunction::getFileSize(FILE* stream) { long curpos,length; curpos = ftell(stream); fseek(stream,0,SEEK_END); length = ftell(stream); fseek(stream,curpos,SEEK_SET); return length; } bool HuffmanFunction::initializeHuffmanTree(HuffmanNode* huffmanTree,const char* sourceFile) { FILE* file = fopen(sourceFile,"rb"); if(file == NULL) { return false; } long size = getFileSize(file);//获得source的文件大小 fclose(file);
for(int i=0;i{ huffmanTree[i].code = (bool*)malloc(size);//动态生成编码数组 huffmanTree[i].weight = 0; huffmanTree[i].floor = 0; huffmanTree[i].codeLength = 0; huffmanTree[i].parent = -1; huffmanTree[i].lchild = -1; huffmanTree[i].rchild = -1; huffmanTree[i].isLeaf = false; } return true; } bool HuffmanFunction::destroyHuffmanTree(HuffmanNode* huffmanTree) { for(int i=0;i{ free(huffmanTree[i].code); } return true; } //返回的是文件中字符的数量 int HuffmanFunction::statisticsOfHuffmanNodes(HuffmanNode* huffmanTree,char* sourceFile) { int count = 0; FILE* file = fopen(sourceFile,"rb"); //读取字符,统计词频 int index; while((index = fgetc(file))!=EOF) { huffmanTree[index].weight++; count++; huffmanTree[index].isLeaf = true; } fclose(file); return count; } bool HuffmanFunction::getFirstAndSecondMinNode(int* firstMinNode,int* secondMinNode,const HuffmanNode* huffmanTree,const int* isSigned) { int firstMinNum = 10000; int secondMinNum = 10000;
for(int i=0;i{ if(isSigned[i]==0&&huffmanTree[i].weight>0&&huffmanTree[i].weight{ *secondMinNode = *firstMinNode; secondMinNum = firstMinNum; *firstMinNode = i; firstMinNum = huffmanTree[i].weight;
} else if(isSigned[i]==0&&huffmanTree[i].weight>0&&huffmanTree[i].weight{ *secondMinNode = i; secondMinNum = huffmanTree[i].weight; } } return true; }
bool HuffmanFunction::generateHuffmanCode(HuffmanNode* huffmanTree,const int index) { //如果是根节点,直接返回 if(huffmanTree[index].floor == 0) { return true; } else { int parent = huffmanTree[index].parent; generateHuffmanCode(huffmanTree,parent);//先生成父节点的code
for(int i=0;i{ huffmanTree[index].code[i] = huffmanTree[parent].code[i]; huffmanTree[index].codeLength++; } //根据是左子节点还是右子节点生成code int codeLength = huffmanTree[index].codeLength; if(huffmanTree[parent].lchild == index) { huffmanTree[index].code[codeLength] = 0; } else if(huffmanTree[parent].rchild == index) { huffmanTree[index].code[codeLength] = 1;