📄 changedlg.cpp
字号:
// ChangeDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Practical.h"
#include "ChangeDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CChangeDlg dialog
CChangeDlg::CChangeDlg(CWnd* pParent /*=NULL*/)
: CDialog(CChangeDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CChangeDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CChangeDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CChangeDlg)
DDX_Control(pDX, IDC_EDIT4, m_Num16);
DDX_Control(pDX, IDC_EDIT3, m_Num8);
DDX_Control(pDX, IDC_EDIT2, m_Num2);
DDX_Control(pDX, IDC_EDIT1, m_Num10);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CChangeDlg, CDialog)
//{{AFX_MSG_MAP(CChangeDlg)
ON_BN_CLICKED(IDC_BUTCHANGE10, OnButchange10)
ON_BN_CLICKED(IDC_BUTCHANGE2, OnButchange2)
ON_BN_CLICKED(IDC_BUTCHANGE8, OnButchange8)
ON_BN_CLICKED(IDC_BUTCHANGE16, OnButchange16)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChangeDlg message handlers
BOOL CChangeDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
m_Num10.SetChange(10);
m_Num2.SetChange(2);
m_Num8.SetChange(8);
m_Num16.SetChange(16);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CChangeDlg::OnButchange10()
{
// TODO: Add your control notification handler code here
CString str10,str2,str8,str16;
m_Num10.GetWindowText(str10);
str2 = AlgorismToBinary(str10);
m_Num2.SetWindowText(str2);
str8 = AlgorismToEight(str10);
m_Num8.SetWindowText(str8);
str16 = AlgorismToHex(str10);
m_Num16.SetWindowText(str16);
}
void CChangeDlg::OnButchange2()
{
// TODO: Add your control notification handler code here
CString str10,str2,str8,str16;
m_Num2.GetWindowText(str2);
str10 = BinaryToAlgorism(str2);
m_Num10.SetWindowText(str10);
str8 = AlgorismToEight(str10);
m_Num8.SetWindowText(str8);
str16 = AlgorismToHex(str10);
m_Num16.SetWindowText(str16);
}
void CChangeDlg::OnButchange8()
{
// TODO: Add your control notification handler code here
CString str10,str2,str8,str16;
m_Num8.GetWindowText(str8);
str10 = EightToAlgorism(str8);
m_Num10.SetWindowText(str10);
str2 = AlgorismToBinary(str10);
m_Num2.SetWindowText(str2);
str16 = AlgorismToHex(str10);
m_Num16.SetWindowText(str16);
}
void CChangeDlg::OnButchange16()
{
// TODO: Add your control notification handler code here
CString str10,str2,str8,str16;
m_Num16.GetWindowText(str16);
str10 = HexToAlgorism(str16);
m_Num10.SetWindowText(str10);
str2 = AlgorismToBinary(str10);
m_Num2.SetWindowText(str2);
str8 = AlgorismToEight(str10);
m_Num8.SetWindowText(str8);
}
CString CChangeDlg::AlgorismToBinary(CString algorism)
{
int nAlgorism = atoi(algorism);
int nYushu; //余数
int nShang; //商
CString Binary = "";
CString str1 = "";
CString str2 = "";
BOOL bContinue = TRUE;
while(bContinue)
{
nYushu = nAlgorism%2;
nShang = nAlgorism/2;
str1.Format("%d",nYushu);
str2 = Binary;
Binary.Format("%s%s",str1,str2);
nAlgorism = nShang;
if(nShang==0)
bContinue = FALSE;
}
return Binary;
}
CString CChangeDlg::AlgorismToHex(CString algorism)
{
int nAlgorism = atoi(algorism.GetBuffer(0));
int nYushu; //余数
int nShang; //商
CString Hex = "";
CString str1 = "";
CString str2 = "";
BOOL bContinue = TRUE;
while(bContinue)
{
nYushu = nAlgorism%16;
nShang = nAlgorism/16;
if(nYushu>9)
{
switch(nYushu)
{
case 10: str1 = "A";
break;
case 11: str1 = "B";
break;
case 12: str1 = "C";
break;
case 13: str1 = "D";
break;
case 14: str1 = "E";
break;
case 15: str1 = "F";
break;
}
}
else
{
str1.Format("%d",nYushu);
}
str2 = Hex;
Hex.Format("%s%s",str1,str2);
nAlgorism = nShang;
if(nShang==0)
bContinue = FALSE;
}
return Hex;
}
CString CChangeDlg::AlgorismToEight(CString algorism)
{
int nAlgorism = atoi(algorism);
int nYushu; //余数
int nShang; //商
CString Eight = "";
CString str1 = "";
CString str2 = "";
BOOL bContinue = TRUE;
while(bContinue)
{
nYushu = nAlgorism%8;
nShang = nAlgorism/8;
str1.Format("%d",nYushu);
str2 = Eight;
Eight.Format("%s%s",str1,str2);
nAlgorism = nShang;
if(nShang==0)
bContinue = FALSE;
}
return Eight;
}
CString CChangeDlg::BinaryToAlgorism(CString binary)
{
int nLenth = binary.GetLength();
char* Binary = new char[nLenth];
Binary = binary.GetBuffer(0);
int nAlgorism = 0;
for(int i=0;i<nLenth;i++)
{
char h = Binary[nLenth-1-i];
int j = atoi(&h);
for(int k=0;k<i;k++)
{
j = j * 2;
}
nAlgorism += j;
}
CString Algorism;
Algorism.Format("%d",nAlgorism);
return Algorism;
}
CString CChangeDlg::EightToAlgorism(CString eight)
{
int nLenth = eight.GetLength();
char* Eight = new char[nLenth];
Eight = eight.GetBuffer(0);
int nAlgorism = 0;
for(int i=0;i<nLenth;i++)
{
int e = Eight[nLenth-1-i] - '0';
for(int k=0;k<i;k++)
{
e = e * 8;
}
nAlgorism += e;
}
CString Algorism;
Algorism.Format("%d",nAlgorism);
return Algorism;
}
CString CChangeDlg::HexToAlgorism(CString hex)
{
int nLenth = hex.GetLength();
char* Hex = new char[nLenth];
Hex = hex.GetBuffer(0);
int nAlgorism = 0;
for(int i=0;i<nLenth;i++)
{
char h = Hex[nLenth-1-i];
int j = Change(h);
for(int k=0;k<i;k++)
{
j = j * 16;
}
nAlgorism += j;
}
CString Algorism;
Algorism.Format("%d",nAlgorism);
return Algorism;
}
int CChangeDlg::Change(char ch)
{
//0-9
if (ch >= '0' && ch <= '9')
return (ch - '0');
//A-F
if (ch >= 'A' && ch <= 'F')
return (ch - 'A' + 0xA);
//a-f
if (ch >= 'a' && ch <= 'f')
return (ch - 'a' + 0xA);
return(255);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -