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

📄 graytobinarydlg.cpp

📁 二进制格雷码与自然二进制码的互换
💻 CPP
字号:
// GraytoBinaryDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GraytoBinary.h"
#include "GraytoBinaryDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

static char * num2bin(unsigned int x);
static unsigned int bin2num(char *s);

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

/////////////////////////////////////////////////////////////////////////////
// CGraytoBinaryDlg dialog

CGraytoBinaryDlg::CGraytoBinaryDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGraytoBinaryDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGraytoBinaryDlg)
	m_BtoGD = 0;
	m_BtoGG = 0;
	m_BtoGB = _T("");
	m_GtoBD = 0;
	m_GtoBG = 0;
	m_GtoBB = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGraytoBinaryDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGraytoBinaryDlg)
	DDX_Text(pDX, IDC_EDIT1, m_BtoGD);
	DDX_Text(pDX, IDC_EDIT3, m_BtoGG);
	DDX_Text(pDX, IDC_EDIT2, m_BtoGB);
	DDX_Text(pDX, IDC_EDIT4, m_GtoBD);
	DDX_Text(pDX, IDC_EDIT6, m_GtoBG);
	DDX_Text(pDX, IDC_EDIT8, m_GtoBB);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGraytoBinaryDlg, CDialog)
	//{{AFX_MSG_MAP(CGraytoBinaryDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_BTOG, OnButtonBtog)
	ON_BN_CLICKED(IDC_BUTTON_GTOB, OnButtonGtob)
	ON_BN_CLICKED(IDC_BUTTON_GTOB2, OnButtonGtob2)
	ON_BN_CLICKED(IDC_BUTTON_GTOB3, OnButtonGtob3)
	ON_BN_CLICKED(IDC_BUTTON_GTOB4, OnButtonGtob4)
	ON_BN_CLICKED(IDC_BUTTON_ABOUT, OnButtonAbout)
	ON_BN_CLICKED(IDC_BUTTON_BTOG2, OnButtonBtog2)
	ON_BN_CLICKED(IDC_BUTTON_GTOB5, OnButtonGtob5)
	ON_BN_CLICKED(IDC_BUTTON_GTOB6, OnButtonGtob6)
	ON_BN_CLICKED(IDC_BUTTON_GTOB7, OnButtonGtob7)
	ON_BN_CLICKED(IDC_BUTTON_GTOB8, OnButtonGtob8)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGraytoBinaryDlg message handlers

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

static char * num2bin(unsigned int x)
{
	static char s[66];
	char *p=s+66;
	int m=1;
	*--p='\0';
	do *--p=(x & m)? '1':'0';
	while (m<<=1);
	return p;
}

static unsigned int bin2num(char *s)
{
	unsigned int x=0;
	while(*s)
	{
		x<<=1;
		if(*s++ !='0')
			x|=1;
	}
	return x;	
}

void CGraytoBinaryDlg::OnButtonBtog() 
{
	char buffer[32];
	CString str=_T("");
	UpdateData(true);
	_itoa(m_BtoGD,buffer,2);
	str=buffer;
	m_BtoGG=(m_BtoGD^(m_BtoGD>>1)); //number to gray
	UpdateData(false);
	SetDlgItemText(IDC_EDIT2,str);
	_itoa(m_BtoGG,buffer,2);
	str=buffer;
	SetDlgItemText(IDC_EDIT7,str);
}

void CGraytoBinaryDlg::OnButtonGtob() 
{
	char buffer[32];
	CString str;
	UpdateData(true);
	_itoa(m_GtoBG,buffer,2);
	str=buffer;
	unsigned int temp1=m_GtoBG;
	unsigned int temp2=m_GtoBG;
	while (temp1>>=1) 
		temp2^=temp1;
	m_GtoBD=temp2;
	UpdateData(false);
	SetDlgItemText(IDC_EDIT8,str);
	_itoa(m_GtoBD,buffer,2);
	str=buffer;
	SetDlgItemText(IDC_EDIT5,str);
}

void CGraytoBinaryDlg::OnButtonGtob2() 
{
	char buffer[32];
	CString str;
	UpdateData(true);
	_itoa(m_GtoBG,buffer,2);
	str=buffer;
	unsigned int temp=m_GtoBG;
	int i;
	for(i=0;(1<<i)<sizeof(temp)*CHAR_BIT;i++)
	{
		temp^=temp>>(1<<i);
	}
	m_GtoBD=temp;
	UpdateData(false);
	SetDlgItemText(IDC_EDIT8,str);
	_itoa(m_GtoBD,buffer,2);
	str=buffer;
	SetDlgItemText(IDC_EDIT5,str);
}

void CGraytoBinaryDlg::OnButtonGtob3() 
{
	char buffer[32];
	CString str;
	UpdateData(true);
	_itoa(m_GtoBG,buffer,2);
	str=buffer;
	unsigned int temp=m_GtoBG;
	temp^=temp>>16;
    temp^=temp>>8;
	temp^=temp>>4;
	temp^=temp>>2;
	temp^=temp>>1;
	m_GtoBD=temp;
	UpdateData(false);
	SetDlgItemText(IDC_EDIT8,str);
	_itoa(m_GtoBD,buffer,2);
	str=buffer;
	SetDlgItemText(IDC_EDIT5,str);
}

void CGraytoBinaryDlg::OnButtonGtob4() 
{
	char buffer[32];
	CString str;
	UpdateData(true);
	_itoa(m_GtoBG,buffer,2);
	str=buffer;
	unsigned int temp=m_GtoBG;
	temp^=temp>>1;
    temp^=temp>>2;
	temp^=temp>>4;
	temp^=temp>>8;
	temp^=temp>>16;
	m_GtoBD=temp;
	UpdateData(false);
	SetDlgItemText(IDC_EDIT8,str);
	_itoa(m_GtoBD,buffer,2);
	str=buffer;
	SetDlgItemText(IDC_EDIT5,str);
}

void CGraytoBinaryDlg::OnButtonAbout() 
{
	CAboutDlg dlg;
	dlg.DoModal();
}

unsigned int CGraytoBinaryDlg::atoibase2(const char *string)
{
	unsigned int nRet=0;
	const char *temp=string;
	unsigned int len=strlen(string);
	
	while((*temp)!=0)
	{
		nRet+=(((*temp)-48)<<(len-1-(temp-string)));
		temp+=1;
	}
	return nRet;
}

void CGraytoBinaryDlg::OnButtonBtog2() 
{
	char buffer[32];
	CString str;
	UpdateData(true);
	const char *buf;
	buf=(LPSTR)(LPCTSTR)m_BtoGB;
	m_BtoGD=atoibase2(buf);
	m_BtoGG=(m_BtoGD^(m_BtoGD>>1)); //number to gray
	UpdateData(false);
	_itoa(m_BtoGG,buffer,2);
	str=buffer;
	SetDlgItemText(IDC_EDIT7,str);
}

void CGraytoBinaryDlg::OnButtonGtob5() 
{
	char buffer[32];
	CString str;
	UpdateData(true);
	const char *buf;
	buf=(LPSTR)(LPCTSTR)m_GtoBB;
	m_GtoBG=atoibase2(buf);
	unsigned int temp1=m_GtoBG;
	unsigned int temp2=m_GtoBG;
	while (temp1>>=1) 
		temp2^=temp1;
	m_GtoBD=temp2;
	UpdateData(false);
	_itoa(m_GtoBD,buffer,2);
	str=buffer;
	SetDlgItemText(IDC_EDIT5,str);
}

void CGraytoBinaryDlg::OnButtonGtob6() 
{
	char buffer[32];
	CString str;
	UpdateData(true);
	const char *buf;
	buf=(LPSTR)(LPCTSTR)m_GtoBB;
	m_GtoBG=atoibase2(buf);
	unsigned int temp=m_GtoBG;
	temp^=temp>>16;
    temp^=temp>>8;
	temp^=temp>>4;
	temp^=temp>>2;
	temp^=temp>>1;
	m_GtoBD=temp;
	UpdateData(false);
	_itoa(m_GtoBD,buffer,2);
	str=buffer;
	SetDlgItemText(IDC_EDIT5,str);
}

void CGraytoBinaryDlg::OnButtonGtob7() 
{
	char buffer[32];
	CString str;
	UpdateData(true);
	const char *buf;
	buf=(LPSTR)(LPCTSTR)m_GtoBB;
	m_GtoBG=atoibase2(buf);
	unsigned int temp=m_GtoBG;
	int i;
	for(i=0;(1<<i)<sizeof(temp)*CHAR_BIT;i++)
	{
		temp^=temp>>(1<<i);
	}
	m_GtoBD=temp;
	UpdateData(false);
	_itoa(m_GtoBD,buffer,2);
	str=buffer;
	SetDlgItemText(IDC_EDIT5,str);
}

void CGraytoBinaryDlg::OnButtonGtob8() 
{
	char buffer[32];
	CString str;
	UpdateData(true);
	const char *buf;
	buf=(LPSTR)(LPCTSTR)m_GtoBB;
	m_GtoBG=atoibase2(buf);
	unsigned int temp=m_GtoBG;
	temp^=temp>>1;
    temp^=temp>>2;
	temp^=temp>>4;
	temp^=temp>>8;
	temp^=temp>>16;
	m_GtoBD=temp;
	UpdateData(false);
	_itoa(m_GtoBD,buffer,2);
	str=buffer;
	SetDlgItemText(IDC_EDIT5,str);
}

void CGraytoBinaryDlg::OnButton2() 
{
	UpdateData(true);
	m_BtoGG=(m_BtoGD^(m_BtoGD>>1)); //number to gray
	UpdateData(false);
	str_DtoB=_T("");
	dec_bin(m_BtoGD);
	SetDlgItemText(IDC_EDIT2,str_DtoB);
	str_DtoB=_T("");
	dec_bin(m_BtoGG);
	SetDlgItemText(IDC_EDIT7,str_DtoB);
}

void CGraytoBinaryDlg::dec_bin(unsigned int n)
{
	unsigned int mod;
	mod=n%2;
	if(n>=2)
		dec_bin(n/2); 
	CString str;
	str.Format("%d",mod);
	str_DtoB=str_DtoB+str;//str_DtoB是在*.h文件中定义的CString变量
	return;
}

⌨️ 快捷键说明

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