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

📄 convertiondlg.cpp

📁 VC版本2,8,16等进制转换,算法全部又C语言标准库编写.
💻 CPP
字号:
// ConvertionDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Convertion.h"
#include "ConvertionDlg.h"
#include <math.h>

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

//USER DEFINE
char upper(char ch)
{
    return ch-32;
}

int BinConvertToDec(int bin)
{
  if(bin<=1)
    return(bin);
  else
    return (bin%10+BinConvertToDec(bin/10)*2); 
}

int OctConvertToDec(char *s)
{
    int i;
    int len = strlen(s);
    int c = 0;
	for(i=0;i<len;i++)
	{
		c+=(s[i]-'0')*pow(8,len-i-1);
	}
	return c;
}

long HexConvertToDec(char *s)
{
    int i;
    int len = strlen(s);
    long c = 0;
    for (i = 0; i < len; ++i) {
        if (s[i] >= 'a' && s[i] <= 'f') 
            s[i] = upper(s[i]);
    }
    for (i = 0; i < len; ++i) {
        if (s[i] >= '0' && s[i] <= '9')  /*从高位依次转化*/
            c += (s[i] - '0') * pow(16,len-i-1);
        else  if (s[i] >= 'A' && s[i] <= 'Z')
            c += (s[i] - 55) * pow(16,len-i-1);
        else        
            return -123456;
	}
	return c;
}

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

/////////////////////////////////////////////////////////////////////////////
// CConvertionDlg dialog

CConvertionDlg::CConvertionDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CConvertionDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CConvertionDlg)

	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CConvertionDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CConvertionDlg)
	DDX_Control(pDX, IDC_EDIT3, m_oct);
	DDX_Control(pDX, IDC_EDIT4, m_hex);
	DDX_Control(pDX, IDC_EDIT2, m_dec);
	DDX_Control(pDX, IDC_EDIT1, m_bin);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CConvertionDlg, CDialog)
	//{{AFX_MSG_MAP(CConvertionDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnBIN)
	ON_BN_CLICKED(IDC_BUTTON2, OnDEC)
	ON_BN_CLICKED(IDC_BUTTON4, OnHEX)
	ON_BN_CLICKED(IDC_BUTTON3, OnOCT)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CConvertionDlg message handlers

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

void CConvertionDlg::OnBIN() 
{
	int len1=m_bin.GetWindowTextLength(),errorno=0;
	CString BinText,DecText,HexText,OctText;
	char *pbintext,pocttext[255],phextext[255];
	int bin=0,dec;
	if(len1>0)
	{
	m_bin.GetWindowText(BinText);
	pbintext=BinText.GetBuffer(BinText.GetLength());
	bin=atoi(pbintext);
	for(int i=0;i<strlen(pbintext);i++)
	{
		if(pbintext[i]>='0'&&pbintext[i]<='1')
		{}
		else
		{
			errorno=-1;
		}
	}
    if(errorno==0)
	{
		dec=BinConvertToDec(bin);
		DecText.Format("%d",dec);
		m_dec.SetWindowText(DecText);
		itoa(dec,pocttext,8);
		itoa(dec,phextext,16);
		OctText.Format("%s",pocttext);
		HexText.Format("%s",phextext);
		m_oct.SetWindowText(OctText);
		m_hex.SetWindowText(HexText);
	}
	else
		AfxMessageBox("请输入正确的二进制数,转化失败");
	}
	else
		AfxMessageBox("错误,需要转换的类型编辑框为空");
}

void CConvertionDlg::OnDEC() 
{
	int len1=m_dec.GetWindowTextLength(),errorno=0;
	CString BinText,DecText,OctText,HexText;
	char pbintext[255],*pdectext,pocttext[255],phextext[255];
	int dec=0;
	if(len1>0)
	{
	m_dec.GetWindowText(DecText);
	pdectext=DecText.GetBuffer(DecText.GetLength());
	for(int i=0;i<strlen(pdectext);i++)
	{
		if(pdectext[i]>='0'&&pdectext[i]<='9')
		{}
		else
		{
			errorno=-1;
		}
	}
    if(errorno==0)
	{
		dec=atoi(pdectext);
		DecText.Format("%d",dec);
		itoa(dec,pbintext,2);
		itoa(dec,pocttext,8);
		itoa(dec,phextext,16);
		BinText.Format("%s",pbintext);
		OctText.Format("%s",pocttext);
		HexText.Format("%s",phextext);
		m_bin.SetWindowText(BinText);
		m_oct.SetWindowText(OctText);
		m_hex.SetWindowText(HexText);
	}
	else
		AfxMessageBox("请输入正确的十进制数,转化失败");
	}
	else
		AfxMessageBox("错误,需要转换的类型编辑框为空");
}

void CConvertionDlg::OnOCT() 
{
	int len1=m_oct.GetWindowTextLength(),errorno=0;
	CString BinText,DecText,OctText,HexText;
	char pbintext[255],pdectext[255],*pocttext,phextext[255];
	int dec=0,oct=0;
	if(len1>0)
	{
	m_oct.GetWindowText(OctText);
	pocttext=OctText.GetBuffer(OctText.GetLength());
	for(int i=0;i<strlen(pocttext);i++)
	{
		if(pocttext[i]>='0'&&pocttext[i]<='7')
		{}
		else
		{
			errorno=-1;
		}
	}
    if(errorno==0)
	{
		oct=atoi(pocttext);
		OctText.Format("%d",oct);
		dec=OctConvertToDec(pocttext);
		itoa(dec,pbintext,2);
		itoa(dec,phextext,16);
		BinText.Format("%s",pbintext);
		DecText.Format("%d",dec);
		HexText.Format("%s",phextext);
		m_bin.SetWindowText(BinText);
		m_dec.SetWindowText(DecText);
		m_hex.SetWindowText(HexText);
	}
	else
		AfxMessageBox("请输入正确的八进制数,转化失败");
	}
	else
		AfxMessageBox("错误,需要转换的类型编辑框为空");
	
}

void CConvertionDlg::OnHEX() 
{
	int len1=m_hex.GetWindowTextLength();
	CString BinText,DecText,OctText,HexText;
	char pbintext[255],pocttext[255],*phextext;
	int dec=0;
	if(len1>0)
	{
	m_hex.GetWindowText(HexText);
	phextext=HexText.GetBuffer(HexText.GetLength());
    dec=HexConvertToDec(phextext);
	if(dec!=-123456)
	{
		DecText.Format("%d",dec);
		itoa(dec,pbintext,2);
		itoa(dec,pocttext,8);
		BinText.Format("%s",pbintext);
		DecText.Format("%d",dec);
		OctText.Format("%s",pocttext);
		m_bin.SetWindowText(BinText);
		m_dec.SetWindowText(DecText);
		m_oct.SetWindowText(OctText);
	}
	else
		AfxMessageBox("请输入正确的十六进制数,转化失败");
	}
	else
		AfxMessageBox("错误,需要转换的类型编辑框为空");
}

⌨️ 快捷键说明

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