📄 hdlg.cpp
字号:
// hDlg.cpp : implementation file
//
#include "stdafx.h"
#include "h.h"
#include "hDlg.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
char *code;
char code1[100];
char code2[100];
char code3[100];
char code4[100];
char code5[100];
char code6[100];
char code7[100];
char code8[100];
char code9[100];
char code10[100];
int r=0,g=0,t=0;
//int a1[100],b1[100];
#define MAXLEN 100
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CHDlg dialog
CHDlg::CHDlg(CWnd* pParent /*=NULL*/)
: CDialog(CHDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CHDlg)
m_x = _T("");
m_y = _T("");
m_z = _T("");
m_p = _T("");
m_q = _T("");
m_r = _T("");
m_s = _T("");
m_t = _T("");
m_u = 0.0;
m_v = 0.0;
m_w = 0.0;
m_o = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CHDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CHDlg)
DDX_Text(pDX, IDC_EDIT1, m_x);
DDX_Text(pDX, IDC_EDIT2, m_y);
DDX_Text(pDX, IDC_EDIT3, m_z);
DDX_Text(pDX, IDC_EDIT4, m_p);
DDX_Text(pDX, IDC_EDIT5, m_q);
DDX_Text(pDX, IDC_EDIT6, m_r);
DDX_Text(pDX, IDC_EDIT7, m_s);
DDX_Text(pDX, IDC_EDIT8, m_t);
DDX_Text(pDX, IDC_EDIT11, m_u);
DDX_Text(pDX, IDC_EDIT10, m_v);
DDX_Text(pDX, IDC_EDIT9, m_w);
DDX_Text(pDX, IDC_EDIT12, m_o);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CHDlg, CDialog)
//{{AFX_MSG_MAP(CHDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
ON_CBN_SELENDCANCEL(IDC_COMBO1, OnSelendcancelCombo1)
ON_CBN_SELENDCANCEL(IDC_COMBO2, OnSelendcancelCombo2)
ON_CBN_SELENDCANCEL(IDC_COMBO3, OnSelendcancelCombo3)
ON_CBN_SELENDCANCEL(IDC_COMBO4, OnSelendcancelCombo4)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHDlg message handlers
BOOL CHDlg::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
//m_list.Dir();
//m_list.SetCurSel(0);
return TRUE; // return TRUE unless you set the focus to a control
}
void CHDlg::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 CHDlg::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 CHDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/*整理输入的字符串,合并相同的项,并求出每个字符在数组中出现的次数 */
linktree CHDlg::initialize(char character[])
{
int i=0;
linktree tree,ptr,beforeptr,node; /*链式 ,tree为头结点,beforeptr为ptr的前一结点,node为新申请的结点*/
tree=(linktree)malloc(sizeof(Hftree));/*创建单链表的头结点*/
if(!tree)
return NULL;
tree->next=NULL; /* 头结点为空,且后续结点为空*/
for(i=0;character[i]!='\0'&&character[i]!='\n';i++) /*遍历直到字符串结束为止*/
{
ptr=tree;
beforeptr=tree;
node=(linktree)malloc(sizeof(Hftree)); /*新申请结点node*/
if(!node)return NULL;
node->next=NULL;
node->parent=NULL;
node->lchild=NULL;
node->rchild=NULL; /*置空*/
node->mark=0;
node->ch=character[i];
node->weight=1;
if(tree->next==NULL)
tree->next=node; /*头结点的下一结点为空,连接node*/
else
{
ptr=tree->next;
while(ptr&&ptr->ch!=node->ch) /*将指针移至链表尾*/
{
ptr=ptr->next;
beforeptr=beforeptr->next; /*后移*/
}
if(ptr&&ptr->ch==node->ch) /*如果链表中某结点的字符与新结点的字符相同*/
{
ptr->weight=ptr->weight+1; /*将该结点的权加一*/
}
else /*新结点与表中结点不相同,将新结点插入链表后*/
{
node->next=beforeptr->next;
beforeptr->next=node; /*node连接在beforeptr之后*/
}
}
}
return tree;
}
/*将整理完的字符串按出现次数从小到大的顺序排列 */
linktree CHDlg::Order(linktree tree)
{
linktree head,ph,pt,beforeph; /*head为新链表的表头结点*/
head=(linktree)malloc(sizeof(Hftree));/*创建新链表的头结点*/
if(!head)
return NULL;
head->next=NULL; /*新结点的头结点为空,后续结点也为空*/
ph=head;
beforeph=head;
while(tree->next)
{
pt=tree->next;/*取被操作链表的首元结点*/
tree->next=pt->next;
pt->next=NULL; /*取出pt所指向的结点*/
ph=head->next;
beforeph=head;
if(head->next==NULL)
head->next=pt;/*创建当前操作链表首元结点*/
else
{
while(ph&&ph->weight<pt->weight) /*将被操作结点插入相应位置*/
{
ph=ph->next;
beforeph=beforeph->next;
}
pt->next=beforeph->next;
beforeph->next=pt;
}
}
return head;
}
/*用排完序的字符串建立霍夫曼树 */
linktree CHDlg::createHftree(linktree tree)
{
linktree p,q,newnode,beforep;
for(p=tree->next,q=p->next;p!=NULL&&q!=NULL;p=tree->next,q=p->next) //p,q结点的权值最小为目标结点
{
tree->next=q->next; //将tree引到后面第三个结点处
q->next=NULL;
p->next=NULL; //q,p的后继置空
newnode=(linktree)malloc(sizeof(Hftree));/*申请新结点作为霍夫曼树的中间结点*/
if(!newnode)
return NULL;
newnode->next=NULL;
newnode->mark=0;
newnode->lchild=p;/*取链表头结点后的两个结点作为新结点的左、右儿子*/
newnode->rchild=q;
p->parent=newnode;
q->parent=newnode;
newnode->weight=p->weight+q->weight; //对newnode进行相关赋值
p=tree->next;
beforep=tree;
if(p!=NULL&&p->weight>=newnode->weight) /*将新结点插入原链表的相应位置*/
{
newnode->next=beforep->next;
beforep->next=newnode;
}
else
{
while(p!=NULL&&p->weight<newnode->weight)
{
p=p->next;
beforep=beforep->next;
}
newnode->next=beforep->next;
beforep->next=newnode;
}
}
return (tree->next);
}
/*对霍夫曼树进行编码 */
char *CHDlg::Huffmancoding(linktree tree)
{
int index=0;
linktree ptr=tree;
code=(char *)malloc(10*sizeof(char));/*此数组用于统计霍夫曼编码*/
while(ptr->lchild&&ptr->rchild&&ptr->mark==0)
{
while(ptr->lchild&&ptr->lchild->mark==0)
{
ptr=ptr->lchild;
code[index++]='0';
if(!ptr->rchild&&!ptr->lchild)
{
ptr->mark=1;
code[index++]='\0';
strcat(code1,code);
strcpy(code2,code1);
ptr=tree;
index=0;
}
}
if(ptr->rchild&&ptr->rchild->mark==0)
{
ptr=ptr->rchild;
code[index++]='1';
}
if(!ptr->lchild&&!ptr->rchild)
{
ptr->mark=1;
code[index++]='\0';
strcat(code1,code);
strcpy(code2,code1);
ptr=tree;
index=0;
}
if(ptr->lchild->mark==1&&ptr->rchild->mark==1)
{
ptr->mark=1;
ptr=tree;
index=0;
}
}
return code2;
}
//计算信源的熵
double c1=0.0;
double CHDlg::Huffmancoding1(linktree tree)
{
int index=0,k1=0,k2=0,k3,k4,k5=0;
int a1[100],b1[100];
float c2[3];
linktree ptr=tree;
code=(char *)malloc(10*sizeof(char));
while(ptr->lchild&&ptr->rchild&&ptr->mark==0)
{
while(ptr->lchild&&ptr->lchild->mark==0)
{
ptr=ptr->lchild;
code[index++]='0';
if(!ptr->rchild&&!ptr->lchild)
{
ptr->mark=1;
code[index++]='\0';
a1[k1++]=ptr->weight;
ptr=tree;
index=0;
}
}
if(ptr->rchild&&ptr->rchild->mark==0)
{
ptr=ptr->rchild;
code[index++]='1';
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -