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

📄 hfm.cpp

📁 用VC语言实现HAUFMM的编码
💻 CPP
字号:
#include"hfm.h"
#include<iostream.h>
#include<fstream.h>
void main(){
	String s,t,finstr,foutstr,fcodestr,fdecodestr;
	unsigned char c;
	int i, n,last;
	long length=0,len1=0,len2=0;float percent;
    Inchar IC[charnumber];
	Storechar SC[charnumber];
	BinaryTree<Inchar> a;
	cout<<"INPUT A STRING:";
	cin.get(c);
	while(int(c)!=0x0D&&int(c)!=0x0A){s+=c;cin.get(c);}
    frequency(s,IC,n);
	a.HuffmanTree(IC,n);
    coding(a,SC,IC,n);
	outcode(SC,n);
	cout<<"MESSAGE: ";
	coded(s,SC,n);
	cout<<endl;
	for(i=0;i<n;i++)
		length=length+IC[i].key*SC[i].len;
	cout<<"LENGTH: "<<length<<endl;
	cout<<"DECODE: "<<s<<endl;
	cout<<"INPUT A MESSAGE(输入一段二进制编码): ";
	cin.get(c);
	while(int(c)!=0x0D&&int(c)!=0x0A){t+=c;cin.get(c);}
	cout<<"ORIGINAL MESSAGE: "<<t<<endl;
	cout<<"DECODE: ";
	decode(t,SC,n);
	cout<<endl;
	ifstream fin("in.txt",ios::in|ios::binary|ios::nocreate);//实验6.ppt
	if(!fin){
		cerr<<"The inputing file \"实验6.ppt\" cannot be opened!"<<endl;
		exit(1);
	}
	fstream foutin("实验6.HFM",ios::in|ios::binary|ios::trunc|ios::out|ios::nocreate);//
	if(!foutin){
		cerr<<"The outputing file \"实验6.HFM\" cannot be opened!"<<endl;
		exit(1);
	}
	ofstream fout("实验6d.txt",ios::out|ios::binary|ios::nocreate);//ppt实验6d
	if(!fout){
		cerr<<"The outputing file \"实验6d.ppt\" cannot be opened!"<<endl;
		exit(1);
	}
	while(fin.get(c))finstr+=c;
    frequency(finstr,IC,n);
	len1=finstr.Length();
	a.HuffmanTree(IC,n);
    coding(a,SC,IC,n);
    stringcode(foutstr,finstr,SC,n,last);
	len2=foutstr.Length();
	percent=float(len2)/len1;
	cout<<"COMPRESSION RATIO: "<<percent*100<<"%"<<endl;
    for(i=0;i<foutstr.Length();i++)foutin<<foutstr[i];foutin.close();
    foutin.open("实验6.HFM",ios::in|ios::binary|ios::out|ios::nocreate);
	if(!foutin){
		cerr<<"The outputing file \"实验6.HFM\" cannot be opened!"<<endl;
		exit(1);
	}
	while(foutin.get(c))fcodestr+=c;
    stringdecode(fdecodestr,fcodestr,SC,n,last);
    for(i=0;i<fdecodestr.Length();i++)fout<<fdecodestr[i];
	fin.close();foutin.close();fout.close();
}		

⌨️ 快捷键说明

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