📄 haffmandlg.cpp
字号:
// HaffmanDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Haffman.h"
#include "HaffmanDlg.h"
#include<fstream>
#include<iostream>
#include<String>
#include<cmath>
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHaffmanDlg dialog
CHaffmanDlg::CHaffmanDlg(CWnd* pParent /*=NULL*/)
: CDialog(CHaffmanDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CHaffmanDlg)
m_Addr1 = _T("");
m_Addr = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CHaffmanDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CHaffmanDlg)
DDX_Text(pDX, IDC_ADDR1, m_Addr1);
DDX_Text(pDX, IDC_ADDR, m_Addr);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CHaffmanDlg, CDialog)
//{{AFX_MSG_MAP(CHaffmanDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_COMPRESS, OnCompress)
ON_BN_CLICKED(IDC_UNCOMPRESS, OnUncompress)
ON_BN_CLICKED(IDC_FILE_OPEN, OnFileOpen)
ON_BN_CLICKED(IDC_FILE_OPEN1, OnFileOpen1)
ON_EN_CHANGE(IDC_ADDR, OnChangeAddr)
ON_EN_CHANGE(IDC_ADDR1, OnChangeAddr1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHaffmanDlg message handlers
BOOL CHaffmanDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CHaffmanDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CHaffmanDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CHaffmanDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/******************************************************************************************************/
////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////
//////// 我的代码 ///////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////
/******************************************************************************************************/
struct HTNode
{
char data;
int weight; //权值;
int parent;
int lchild;
int rchild;
};
typedef char* HCode;
HTNode *ht;
HCode *hcd;
int maplist[256]; //建立字符与哈夫曼编码的映射
int nodenum=0; //哈夫曼树结点数
int rearnum=0; //哈夫曼编码尾补码
int textlen=0; //需压缩的文件长度
int codelen=0; //压缩后的文件的哈夫曼编码总长度
int const bufferlen=1024; //设置读取缓冲区长度
class function
{private:
int clean();
void Dec2Bin(int num,int bin[8]); //十进制转二进制函数
bool InitFromFile(string fileadd); //打开文件
void HTCreat(HTNode ht[],int n); //构造哈夫曼树
void HCCreat(HTNode ht[],HCode hcd[],int n);//构造哈夫曼编码
void WriteFile(char *tmp);
unsigned char ConvertBinary(char *tmp); //压缩 写入文件
void ConvertFile(HCode hcd[],string fileadd,string fileadd2);
void DecompressionFile(string fileadd2,string fileadd3);
public:
string Compression(string fileadd);
string Decompression(string fileadd2);
};
int function::clean()
{
delete ht;
delete hcd;
return 1;
}
void function::Dec2Bin(int num,int bin[8])
{
int i=0;
for(i=0;i<8;i++)
{
bin[i]=0;
}
i=0;
while(num>0)
{
bin[8-1-i]=num%2;
num=num/2;
i++;
}
}
//////////////打开文件//////////////
bool function::InitFromFile(string fileadd)
{
fstream infile(fileadd.c_str(),ios::binary|ios::in);
if(!infile)
{
MessageBox(NULL,"error!","error!",MB_OK);
return 0;
}
int table[256];
int i,j;
int len=0,num=0;
unsigned char ch;
for(i=0;i<256;i++) {table[i]=0;maplist[i]=-1;}
int readlen=0;
char buffer[bufferlen]; //设置读取缓冲区
do
{
infile.read(buffer,bufferlen);
i=0;
readlen=infile.gcount();
while(i<readlen)
{
ch=(unsigned char)buffer[i];
table[ch]++;
len++;
i++;
}
}
while(readlen==bufferlen);
for(i=0;i<256;i++)
{
if(table[i]!=0) num++;
}
ht=new HTNode[2*num-1];
hcd=new HCode[num];
for(i=0,j=0;i<256;i++)
{
if(table[i]!=0)
{
ht[j].data=i;
ht[j].weight=table[i];
maplist[i]=j; //建立字符与哈夫曼编码的映射
j++;
}
}
nodenum=num;
textlen=len;
infile.clear();
infile.close();
return 1;
}
//////////////构造哈夫曼树////////////
void function::HTCreat(HTNode ht[],int n)
{
int i,k,lnode,rnode;
int min1,min2;
for(i=0;i<2*n-1;i++)
{
ht[i].parent=ht[i].rchild=ht[i].lchild=-1;
}
for(i=n;i<2*n-1;i++)
{
min1=min2=2147483647;
lnode=rnode=-1;
for(k=0;k<=i-1;k++)
{
if(ht[k].parent==-1)
{
if(ht[k].weight<min1)
{
min2=min1;
min1=ht[k].weight;
rnode=lnode;
lnode=k;
}
else if(ht[k].weight<min2)
{
min2=ht[k].weight;
rnode=k;
}
}
}
ht[lnode].parent=i;
ht[rnode].parent=i;
ht[i].weight=ht[lnode].weight+ht[rnode].weight;
ht[i].lchild=lnode;
ht[i].rchild=rnode;
}
}
///////////构造哈夫曼编码/////////////
void function::HCCreat(HTNode ht[],HCode hcd[],int n)
{
int i,p,c;
HCode hc;
hc=new char[n];
int start,tmplen;
for(i=0;i<n;i++)
{
tmplen=0;
start=n-1;
hc[start]='\0';
c=i;
p=ht[i].parent;
while(p!=-1)
{
if(ht[p].lchild==c) //是左孩子结点
{
hc[--start]='0';
tmplen++;
}
else
{
hc[--start]='1';
tmplen++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -