⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 huffmanenanddecode.cpp

📁 实现了对文本文件的huffman编码与解码过程
💻 CPP
字号:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

#include "QuickSortAlgorithm.h"
#include "GenerateHuffmanCode.h"
#include "HuffmanProcess.h"
#include "Encode.h"
#include "Decode.h"

void main()
{
	char c ;
	int num;
	int totalnum = 0;
	int numforexist = 0;
	int wordnumber[26] = {0};
	float a[2][26] = {0};
	float queue1[26] = {0}; 
	float queue2[3][25] = {0};
	string str1[26];
	string str2[25];
	string foutstr2 = "decode.txt";
	string finstr = "plain.txt";
	string foutstr1 = "encode.txt";


	// read through the file to record how many times for each charactor and compute the possiblity for each charactor
	const char *in = finstr.c_str();
	ifstream infile(in);
	for(num =0;!infile.eof(); num++)
	{
		infile.get(c);
		a[0][c-97] = a[0][c-97] + 1;
		totalnum++;
	}
	a[0][c-97] = a[0][c-97] - 1;
	num = num - 1;
	for(int i=0; i< 26; i++)
	{	
		a[0][i] = a[0][i]/num;
		if(a[0][i]>0)
		{
			queue1[numforexist] = a[0][i];
			wordnumber[numforexist] = i;
			numforexist++;
		}
	}
	infile.close();

	// sort the array
	QuickSortAlg(queue1, 0, numforexist-1 );

	//Huffman encode

	// use two queues to generate the possibilities, store in the second queue
	HuffmanP(queue1, queue2, numforexist);

	// generate the huffman code 
	generateHC(numforexist-2, queue1, queue2, str1, str2, "");
	
	//encode and save in file "encode.txt"
	Encode(finstr, foutstr1, wordnumber, str1, totalnum-1);

	//decode and save in file "decode.txt"
	Decode(foutstr1, foutstr2, wordnumber, str1);

	cout<<"Done!"<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -