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

📄 huftestdlg.cpp

📁 测试哈夫曼编码源程序 软件工程中重要的一部分
💻 CPP
字号:
// huftestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "huftest.h"
#include "huftestDlg.h"
#include <string>
#include "hufAnddehuf.h"

#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()

/////////////////////////////////////////////////////////////////////////////
// CHuftestDlg dialog

CHuftestDlg::CHuftestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CHuftestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CHuftestDlg)
	m_strcom = _T("");
	m_strwcom = _T("");
	m_comnumber = 0;
	m_persent = 0.0f;
	m_rawnumber = 0;
	m_comallnumber = 0;
	m_strdecom = _T("");
	m_strwdecom = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CHuftestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHuftestDlg)
	DDX_Text(pDX, IDC_EDITCOM, m_strcom);
	DDX_Text(pDX, IDC_EDITWCOM, m_strwcom);
	DDX_Text(pDX, IDC_COMNUMBER, m_comnumber);
	DDX_Text(pDX, IDC_PERSENT, m_persent);
	DDX_Text(pDX, IDC_RAWNUMBER, m_rawnumber);
	DDX_Text(pDX, IDC_COMALLNUMBER, m_comallnumber);
	DDX_Text(pDX, IDC_EDITDECOM, m_strdecom);
	DDX_Text(pDX, IDC_EDITWDECOM, m_strwdecom);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CHuftestDlg, CDialog)
	//{{AFX_MSG_MAP(CHuftestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_OPENCOM, OnOpencom)
	ON_BN_CLICKED(IDC_SAVECOM, OnSavecom)
	ON_BN_CLICKED(IDC_COMPRESS, OnCompress)
	ON_BN_CLICKED(IDC_OPENDECOM, OnOpendecom)
	ON_BN_CLICKED(IDC_SAVEDECOM, OnSavedecom)
	ON_BN_CLICKED(IDC_DECOMPRESS, OnDecompress)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHuftestDlg message handlers

BOOL CHuftestDlg::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 CHuftestDlg::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 CHuftestDlg::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 CHuftestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CHuftestDlg::OnOpencom() 
{
	CString strFilter="All Files(*.*)||*.*";
	CFileDialog dlg(TRUE,NULL,NULL,NULL,strFilter);
	if(dlg.DoModal()==IDOK)
	{     	
		m_strwcom = dlg.GetPathName();
		UpdateData(FALSE);
	}		
}

void CHuftestDlg::OnSavecom() 
{
	CString strFilter="All Files(*.*)||*.*";
	CFileDialog dlg(FALSE,NULL,NULL,NULL,strFilter);
	if(dlg.DoModal()==IDOK)
	{     	
		m_strcom = dlg.GetPathName();
		CString strExt = dlg.GetFileExt();
		if(strExt == "")
		{
			MessageBox("请输入保存文件的后缀","提示");
			m_strcom = "";
			return;
		}
		UpdateData(FALSE);
	}	
}

void CHuftestDlg::CompressProcess()
{
	int nLen = m_strwcom.GetLength();
	ifile = fopen(m_strwcom.GetBuffer(nLen),"r");   //open ifile which to be compressed
	if(ifile == NULL)
	{
		MessageBox("压缩文件打开错误!","提示");
		return;
	}
	file_size = get_file_size(ifile);               //get the file length
	fseek(ifile,0L,0);           //move the file pointer to the begin of the file
	get_frequency_count ();
	build_initial_heap ();
	build_code_tree ();
	generate_code_table ();
	fseek(ifile,0L,0);           //move the file pointer to the begin of the file
	int nLencom = m_strcom.GetLength();
	ofile = fopen(m_strcom.GetBuffer(nLencom),"w");
	if(ofile == NULL)
	{
		MessageBox("写文件打开错误!","提示");
		return;
	}
	AddCompressInformation();   // add code[] , code_length[] to the file and file_size 
	                            //to the ofile;
	compress_image ();
	fclose(ifile);
	fclose(ofile);
	MessageBox("压缩完成,请查看压缩报告.","报告");
	m_rawnumber = file_size;
	m_comnumber = compress_charcount;
	m_comallnumber = compress_charcount + 512 + sizeof(file_size);
	m_persent = 100 - ((float) m_comallnumber / (float) file_size) * 100;
	UpdateData(FALSE);
}

void CHuftestDlg::AddCompressInformation()
{
	int i;
	for(i=0;i<256;i++)
	{
		fprintf(ofile,"%d ",code_length[i]);
        fprintf(ofile,"%d ",code[i]);
	}
	fprintf(ofile,"%d ",file_size);
}

void CHuftestDlg::OnCompress() 
{
	UpdateData(TRUE);
	if(m_strwcom == "" || m_strcom == "")
	{
		MessageBox("请正确设置压缩文件路径和保存文件路径!","提示");
		return;
	}
/*	CFile file;
	file.Open(m_strwcom,CFile::modeRead);
	file_size = file.GetLength();
	file.Close();
	CString strlength;
	strlength.Format("%d",file_size);
	MessageBox(strlength);*/
    CompressProcess();
}

void CHuftestDlg::OnOpendecom() 
{
    CString strFilter="All Files(*.*)||*.*";
	CFileDialog dlg(TRUE,NULL,NULL,NULL,strFilter);
	if(dlg.DoModal()==IDOK)
	{     	
		m_strwdecom = dlg.GetPathName();
		UpdateData(FALSE);
	}		
}

void CHuftestDlg::OnSavedecom() 
{
	CString strFilter="All Files(*.*)||*.*";
	CFileDialog dlg(FALSE,NULL,NULL,NULL,strFilter);
	if(dlg.DoModal()==IDOK)
	{     	
		m_strdecom = dlg.GetPathName();
		CString strExt = dlg.GetFileExt();
		if(strExt == "")
		{
			MessageBox("请输入保存文件的后缀","提示");
			m_strdecom = "";
			return;
		}
		UpdateData(FALSE);
	}	
}

void CHuftestDlg::ReadCompressInformation()
{
    int i;
	for(i=0;i<256;i++)
	{
		fscanf(ifile,"%d",&code_length[i]);
		fscanf(ifile,"%d",&code[i]);
	}
	fscanf(ifile,"%d",&file_size);
	getc(ifile);
}

void CHuftestDlg::DecompressProcess()
{
    int nLen = m_strwdecom.GetLength();
	ifile = fopen(m_strwdecom.GetBuffer(nLen),"r");   //open ifile which to be compressed
	if(ifile == NULL)
	{
		MessageBox("解压文件打开错误!","提示");
		return;
	}
	ReadCompressInformation();
	int nLenDecom = m_strdecom.GetLength();
	ofile = fopen(m_strdecom.GetBuffer(nLenDecom),"w");
	if(ofile ==NULL)
	{
		MessageBox("保存文件打开错误","提示");
		return;
	}
	build_decomp_tree ();
	decompress_image ();
	fclose(ifile);
	fclose(ofile);
	MessageBox("文件解压完成!","提示");
}

void CHuftestDlg::OnDecompress() 
{
    UpdateData(TRUE);
	if(m_strwdecom == "" || m_strdecom == "")
	{
		MessageBox("请正确设置压缩文件路径和保存文件路径!","提示");
		return;
	}
/*	CFile file;
	file.Open(m_strwdecom,CFile::modeRead);
	file_size = file.GetLength();
	file.Close();*/
    DecompressProcess();
}

⌨️ 快捷键说明

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