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

📄 convertdlg.cpp

📁 这是一个用VC++.NET编写的关于进制转换的程序,程序有点简单
💻 CPP
字号:
// ConvertDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "Convert.h"
#include "ConvertDlg.h"
#include ".\convertdlg.h"
#include "math.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CConvertDlg 对话框



CConvertDlg::CConvertDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CConvertDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CConvertDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_CONVERTBF, m_convertbf);
	DDX_Control(pDX, IDC_CONVERTAF, m_convertaf);
}

BEGIN_MESSAGE_MAP(CConvertDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_DEC, OnBnClickedDec)
	ON_BN_CLICKED(IDC_RADIOBIN, OnBnClickedRadiobin)
	ON_BN_CLICKED(IDC_RADIOOCT, OnBnClickedRadiooct)
	ON_BN_CLICKED(IDC_RADIODEC, OnBnClickedRadiodec)
	ON_BN_CLICKED(IDC_RADIOHEX, OnBnClickedRadiohex)
END_MESSAGE_MAP()


// CConvertDlg 消息处理程序

BOOL CConvertDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 将\“关于...\”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	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);
		}
	}

	// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	
	return TRUE;  // 除非设置了控件的焦点,否则返回 TRUE
}

void CConvertDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void CConvertDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作矩形中居中
		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;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标显示。
HCURSOR CConvertDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CConvertDlg::OnBnClickedDec()
{
	CString strbf;
	CString straf;
	m_convertbf.GetWindowText(strbf);
	straf=MainPro(strbf);
	m_convertaf.SetWindowText(straf);
	// TODO: 在此添加控件通知处理程序代码
}

CString CConvertDlg::MainPro(CString strExp)
{
	OctToDec(&strExp); //将表达式中的八进制数转换成十进制
	HexToDec(&strExp); //将表达式中的十六进制数转换成十进制
    BinToDec(&strExp); //将表达式中的二进制数转换成十进制
	return strExp;
}

void CConvertDlg::OctToDec(CString* strExp)//八进制转换为十进制函数
{
int len,i,index,pos=strExp->Find("xo");
	CString strTmp,strDF;
	char ch;
	double dx;
	while(pos!=-1)
	{
		dx=0;strTmp="";strDF="";
		strExp->Delete(pos,2);
		for(i=pos-1;i>=0;i--)
		{
			ch=strExp->GetAt(i);
			if(ch==56 || ch==57 || ch>=97 && ch<=102) 
			{
				*strExp="ERROR_八进制数越界_";
				return;
			}
			if(ch>=48 && ch<=55 ||ch==46)
			{
				strTmp.Insert(0,strExp->Mid(i,1));
				strExp->Delete(i);
			}
			else break;
		}
		if(i==pos-1) {*strExp="ERROR_缺少二元运算符_";return;}
		index=i;
		pos=strTmp.Find(".");
		if(pos!=-1)
		{
			strDF=strTmp.Right(strTmp.GetLength()-pos-1);
			strTmp.Delete(pos,strTmp.GetLength()-pos);
		}
		strTmp.MakeReverse();
		len=strTmp.GetLength();
		for(i=0;i<len;i++)
		{
			ch=strTmp.GetAt(i);
			dx+=(ch-48)*pow(8,i);
		}
		len=strDF.GetLength();
		for(i=1;i<=len;i++)
		{
			ch=strDF.GetAt(i-1);
			dx+=(ch-48)*(1/pow(8,i));
		}
		strTmp=NtoS(dx);
		strExp->Insert(index+1,strTmp);
		pos=strExp->Find("xo");
	}
}

CString CConvertDlg::NtoS(double d)
{
	int  decimal, sign;
	char *buffer;
	buffer = _ecvt( d, 16, &decimal, &sign );
	CString str=buffer;
	if(decimal>=0 && decimal<=16) str.Insert(decimal,".");
	else if(decimal>16)
	{
		for(int i=str.GetLength();i<decimal;i++) str+="0";
		str+=".0";
	}
	else 
	{
		for(int i=0;i<-decimal;i++)	str.Insert(0,"0");
		str.Insert(0,".");
	}
	if(sign==1) str.Insert(0,"-");
	return str;
	//return CString();
}

void CConvertDlg::HexToDec(CString* strExp)//十六进制转换为十进制函数
{
	int len,i,index,pos=strExp->Find("xh");
	CString strTmp,strDF;
	char ch;
	double dx;
	while(pos!=-1)
	{
		dx=0;strTmp="";strDF="";
		strExp->Delete(pos,2);
		for(i=pos-1;i>=0;i--)
		{
			ch=strExp->GetAt(i);
			if(ch>=48 && ch<=57 || ch>=97 && ch<=102 ||ch==46)
			{
				strTmp.Insert(0,strExp->Mid(i,1));
				strExp->Delete(i);
			}
			else break;
		}
		if(i==pos-1) {*strExp="ERROR_缺少二元运算符_";return;}
		index=i;
		pos=0;
		for(i=0;i<strTmp.GetLength();i++)
		{
			if(strTmp.GetAt(i)=='.') pos++;
			if(pos>1) {*strExp="ERROR_缺少二元运算符_";return;}
		}
		pos=strTmp.Find(".");
		if(pos!=-1)
		{
			strDF=strTmp.Right(strTmp.GetLength()-pos-1);
			strTmp.Delete(pos,strTmp.GetLength()-pos);
		}
		strTmp.MakeReverse();
		len=strTmp.GetLength();
		for(i=0;i<len;i++)
		{
			ch=strTmp.GetAt(i);
			if(ch>=48 && ch<=57)//该数在0~9之间
			{
				dx+=(ch-48)*pow(16,i);
			}
			else if(ch>=97 && ch<=102)//该数在a~f之间
			{
				dx+=(ch-87)*pow(16,i);
			}
		}
		len=strDF.GetLength();
		for(i=1;i<=len;i++)
		{
			ch=strDF.GetAt(i-1);
			if(ch>=48 && ch<=57)//该数在0~9之间
			{
				dx+=(ch-48)*(1/pow(16,i));
			}
			else if(ch>=97 && ch<=102)//该数在a~f之间
			{
				dx+=(ch-87)*(1/pow(16,i));
			}
		}
		strTmp=NtoS(dx);
		strExp->Insert(index+1,strTmp);
		pos=strExp->Find("xh");
	}
}

void CConvertDlg::BinToDec(CString* strExp)//二进制转换为十进制函数
{
	int len,i,index,pos=strExp->Find("xb");
	CString strTmp,strDF;
	char ch;
	double dx;
	while(pos!=-1)
	{
		dx=0;strTmp="";strDF="";
		strExp->Delete(pos,2);
		for(i=pos-1;i>=0;i--)
		{
			ch=strExp->GetAt(i);
			if(ch>=50 && ch<=57 || ch>=97 && ch<=102) 
			{
				*strExp="ERROR_二进制数越界_";
				return;
			}
			if(ch=='0' || ch=='1' ||ch==46)
			{
				strTmp.Insert(0,strExp->Mid(i,1));
				strExp->Delete(i);
			}
			else break;
		}
		if(i==pos-1) {*strExp="ERROR_缺少二元运算符_";return;}
		index=i;
		pos=strTmp.Find(".");
		if(pos!=-1)
		{
			strDF=strTmp.Right(strTmp.GetLength()-pos-1);
			strTmp.Delete(pos,strTmp.GetLength()-pos);
		}
		strTmp.MakeReverse();
		len=strTmp.GetLength();
		for(i=0;i<len;i++)
		{
			ch=strTmp.GetAt(i);
			dx+=(ch-48)*pow(2,i);
		}
		len=strDF.GetLength();
		for(i=1;i<=len;i++)
		{
			ch=strDF.GetAt(i-1);
			dx+=(ch-48)*(1/pow(2,i));
		}
		strTmp=NtoS(dx);
		strExp->Insert(index+1,strTmp);
		pos=strExp->Find("xb");
	}
}

void CConvertDlg::OnBnClickedRadiobin()//转换为二进制
{
	CString straf;
	m_convertaf.GetWindowText(straf);
	DecToBin(&straf);
	// TODO: 在此添加控件通知处理程序代码
}

void CConvertDlg::OnBnClickedRadiooct()//转换为八进制
{
	CString straf;
	m_convertaf.GetWindowText(straf);
	DecToOct(&straf);
	// TODO: 在此添加控件通知处理程序代码
}

void CConvertDlg::OnBnClickedRadiodec()//转换为十进制
{
	CString straf;
	m_convertaf.GetWindowText(straf);
	m_convertaf.SetWindowText(straf);
	// TODO: 在此添加控件通知处理程序代码
}

void CConvertDlg::OnBnClickedRadiohex()//转换为十六进制
{
	CString straf;
	m_convertaf.GetWindowText(straf);
	DecToHex(&straf);
	// TODO: 在此添加控件通知处理程序代码
}

void CConvertDlg::DecToHex(CString* strExp)
{
	bool bMinus=0;
	if(strExp->GetAt(0)=='-') 
	{
		bMinus=1;
		strExp->Delete(0);
	}
	int pos=strExp->Find('.');
	CString str,strDec;
	int nDecInt;
	double dDec;
	if(pos!=-1) 
	{
		strDec=strExp->Left(pos);
		nDecInt=atoi(strDec.GetBuffer(0));
		strDec=strExp->Right(strExp->GetLength()-pos);	
	}
	else
	{
		nDecInt=atoi(strExp->GetBuffer(0));
	}
	strExp->Empty();
	while(nDecInt!=0)
	{
		int nTmp=nDecInt%16;
		if(nTmp==10) str="a";
		else if(nTmp==11) str="b";
		else if(nTmp==12) str="c";
		else if(nTmp==13) str="d";
		else if(nTmp==14) str="e";
		else if(nTmp==15) str="f";
		else str.Format("%d",nTmp);
		nDecInt/=16;
		strExp->Insert(0,str);
	}
	*strExp+=".";
	if(pos!=-1)
	{
		dDec=StoN(strDec);
		int nCount=0;
		while(dDec!=0)
		{
			dDec*=16;
			int nTmp=dDec;
			if(nTmp==10) str="a";
			else if(nTmp==11) str="b";
			else if(nTmp==12) str="c";
			else if(nTmp==13) str="d";
			else if(nTmp==14) str="e";
			else if(nTmp==15) str="f";
			else str.Format("%d",nTmp);
			*strExp+=str.Left(pos);
			dDec-=nTmp;
			if(++nCount==17) break;
		}
	}
	if(bMinus) strExp->Insert(0,"-");
	if(strExp->Find("-1")!=-1 && bMinus!=1) *strExp="太大无法表示";
	m_convertaf.SetWindowText(*strExp);
}

double CConvertDlg::StoN(CString str)
{
	char   *stopstring;
	double x;
	CString m_strTmp;
    x = strtod( str.GetBuffer(0), &stopstring );
	m_strTmp=stopstring;
	return x;
}

void CConvertDlg::DecToBin(CString* strExp)
{
	bool bMinus=0;
	if(strExp->GetAt(0)=='-') 
	{
		bMinus=1;
		strExp->Delete(0);
	}
	int pos=strExp->Find('.');
	CString str,strDec;
	_int64 nDecInt;
	double dDec;
	if(pos!=-1) 
	{
		strDec=strExp->Left(pos);
		if(strDec.Compare("4294967295")>0 && strDec.GetLength()>10 || strDec.GetLength()>10)
		{
			*strExp="太大无法转换";
			return;
		}
		nDecInt=atoi(strDec.GetBuffer(0));
		strDec=strExp->Right(strExp->GetLength()-pos);	
	}
	else 
	{
		if(strExp->Compare("4294967295")>0 && strExp->GetLength()>10 || strExp->GetLength()>10)
		{
			*strExp="太大无法转换";
			return;
		}
		nDecInt=atoi(strExp->GetBuffer(0));
	}
	strExp->Empty();
	while(nDecInt!=0)
	{
		_int64 nTmp=nDecInt%2;
		str.Format("%d",nTmp);
		nDecInt/=2;
		strExp->Insert(0,str);
	}
	*strExp+=".";
	if(pos!=-1)
	{
		dDec=StoN(strDec);
		int nCount=0;
		while(dDec!=0)
		{
			dDec*=2;
			int nTmp=dDec;
			str.Format("%d",nTmp);
			*strExp+=str.Left(pos);
			dDec-=nTmp;
			if(++nCount==17) break;
		}
	}
	if(bMinus) strExp->Insert(0,"-");
	m_convertaf.SetWindowText(*strExp);
}

void CConvertDlg::DecToOct(CString* strExp)
{
	bool bMinus=0;
	if(strExp->GetAt(0)=='-') 
	{
		bMinus=1;
		strExp->Delete(0);
	}
	int pos=strExp->Find('.');
	CString str,strDec;
	int nDecInt;
	double dDec;
	if(pos!=-1) 
	{
		strDec=strExp->Left(pos);
		nDecInt=atoi(strDec.GetBuffer(0));
		strDec=strExp->Right(strExp->GetLength()-pos);	
	}
	else 
	{
		nDecInt=atoi(strExp->GetBuffer(0));
	}
	strExp->Empty();
	while(nDecInt!=0)
	{
		int nTmp=nDecInt%8;
		str.Format("%d",nTmp);
		nDecInt/=8;
		strExp->Insert(0,str);
	}
	*strExp+=".";
	if(pos!=-1)
	{
		dDec=StoN(strDec);
		int nCount=0;
		while(dDec!=0)
		{
			dDec*=8;
			int nTmp=dDec;
			str.Format("%d",nTmp);
			*strExp+=str.Left(pos);
			dDec-=nTmp;
			if(++nCount==17) break;
		}
	}
	if(bMinus) strExp->Insert(0,"-");
	m_convertaf.SetWindowText(*strExp);
}

⌨️ 快捷键说明

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