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

📄 huffman.cpp

📁 哈弗曼编码
💻 CPP
字号:
#include "windows.h"
#include "file_dealing.h"
#include "string.h"
#include "define.h"
#include "compress.h"

NODE *compile(char *);
NODE *translate(char *);

NODE *compile(char *_file_to_compile)
{
	NODE *head=NULL;
	if ((fullfill_the_weight_array_and_create_weight_file(_file_to_compile))==false)
		return NULL;
	else
	{
		head=createhuffman();
		cout<<"Huffman tree created !"<<endl;
		put_into_code(head,_file_to_compile);
		char anwser;
		cout<<"Have a look at the file (y or n) ?"<<endl;
		cin>>anwser;
		if (anwser=='y')
			system("notepad.exe code.txt");
		cout<<"\n";
	}
	return head;
}

NODE *translate(char *_file_to_translate)
{
	NODE *head=NULL;
	int i;
	for (i=0;i<95;i++)
	{
		buffer[i].chrctr=-1;
		buffer[i].freq=0;
	}
	if ((fullfill_the_weight_array_by_reading_weight_file())==false)
		return false;
	head=createhuffman();
	put_into_ascii(head,_file_to_translate);
	char anwser;
	cout<<"Have a look at the file (y or n) ?"<<endl;
	cin>>anwser;
	if (anwser=='y')
	system("notepad.exe translated.txt");
	return head;
}

bool main(int argc, char* argv[])
{
	NODE *head=NULL;
	char cmdline[COMMAND_LINE],val;
	bool go=true;
	cout<<"Data Structure Course for Huffman Program Designing [版本1.0]"<<endl;
	if (argc==1)
		cout<<"To get help,please input \"h\" command !"<<endl;
	while(go)
	{
		cout<<">>";
		cin>>cmdline;
		if ((strcmp(cmdline,"h"))==0)
		{
			cout<<"These commands can be used in the program :"<<endl;
			cout<<"c	To compile a TXT file using huffman code"<<endl;
			cout<<"t	To translate a compiled file into TXT file"<<endl;
			cout<<"p	To compress the code file"<<endl;
			cout<<"ex	To extract the compressed file"<<endl;
			cout<<"s	To search for huffman code for a spicifed charactor"<<endl;
			cout<<"e	To exit the program"<<endl;
			cout<<"h	To get help of how to use this program"<<endl;
			cout<<"cls	To clear the screen"<<endl;
		}
		else if ((strcmp(cmdline,"c"))==0)
		{	
			char _file_to_compile[FILE_NAME_LENGTH];
			cout<<"Input the file to compile (full name) :"<<endl;
			cin>>_file_to_compile;
			head=compile(_file_to_compile);
		}
		else if ((strcmp(cmdline,"t"))==0)
		{
			char _file_to_translate[FILE_NAME_LENGTH];
			cout<<"Please input the file to translate :"<<endl;
			cin>>_file_to_translate;
			head=translate(_file_to_translate);
		}
		else if ((strcmp(cmdline,"p"))==0)
		{
			char _file_to_compress[FILE_NAME_LENGTH];
			cout<<"Please input the file to compress :"<<endl;
			cin>>_file_to_compress;
			if ((compress(_file_to_compress))==false)
			{
				cout<<"Compress failed !"<<endl;
				continue;
			}
		}
		else if ((strcmp(cmdline,"ex"))==0)
		{
			char _file_to_extract[FILE_NAME_LENGTH];
			cout<<"Please input the file to extract :"<<endl;
			cin>>_file_to_extract;
			if ((extract(_file_to_extract))==false)
			{
				cout<<"Extract failed !"<<endl;
				continue;
			}
		}
		else if ((strcmp(cmdline,"s"))==0)
		{
			if (!head)
			{
				cout<<"The huffman tree doesn't exist !"<<endl;
				continue;
			}
			cout<<"Input the character to compile :"<<endl;
			cin>>val;
			while(r!=-1)
				pop();
			found=false;
			compile_each_character(head,val);
			if (found==false)
			{
				cout<<"The huffman tree doesn't contain this node !"<<endl;
				cout<<"Here are all the characters :"<<endl;
				countleaves(head);
			}
			else
			{
				cout<<"\""<<val<<"\"   ";
				r=0;
				while(stack[r])
				{
					cout<<stack[r];
					r++;
				}
			}
			cout<<"\n";
		}
		else if ((strcmp(cmdline,"e"))==0)
			go=false;
		else if ((strcmp(cmdline,"cls"))==0)
			system("cls");
		else 
			cout<<"\""<<cmdline<<"\""<<"	Invalid command !"<<endl;
	}
	del_the_tree(head);
	return true;
}

⌨️ 快捷键说明

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