一个用VC写的简单的任意进制数值转换器

飞天赤狐 2007-07-16 10:09:50
// NumTranDlg.cpp : implementation file
//

#include "stdafx.h"
#include "NumTran.h"
#include "NumTranDlg.h"
#include "math.h"
#include "String.h"

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

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

/////////////////////////////////////////////////////////////////////////////
// CNumTranDlg dialog

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

void CNumTranDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CNumTranDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CNumTranDlg, CDialog)
//{{AFX_MSG_MAP(CNumTranDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNumTranDlg message handlers

BOOL CNumTranDlg::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
int Sum=0;

return TRUE; // return TRUE unless you set the focus to a control
}

void CNumTranDlg::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 CNumTranDlg::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 CNumTranDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
int Sum;

int CharToInt(char ch)
{
return (int(ch)-'0');
}

void CNumTranDlg::OnOK()
{
int i=0,m;
char temp[200];
CString str1,str2,str3;
CEdit *pEdit1=(CEdit*)GetDlgItem(IDC_EDIT1);
pEdit1->GetWindowText(str1);
strcpy(temp,str1);
m=atoi(temp);
CEdit *pEdit2=(CEdit*)GetDlgItem(IDC_EDIT2);
pEdit2->GetWindowText(str2);
char *chVal=str2.GetBuffer(str2.GetLength());
for(i=0;chVal[i]!=NULL;i++)
{
Sum=Sum+CharToInt(chVal[i])*int(pow((double)m,(double)(str2.GetLength()-i-1)));
//调用函数pow(x,y)计算x的Y次方
}
str3.Format("%d",Sum);
CEdit *pEdit3=(CEdit*)GetDlgItem(IDC_EDIT3);
pEdit3->SetWindowText(str3);

}




void CNumTranDlg::OnCancel()
{
// TODO: Add extra cleanup here

CDialog::OnCancel();
}

void CNumTranDlg::OnButton1()
{
CString strN,str3,str5,strTemp;
int n;
//int Sum=0,
int j=0;
char Temp1[200],Temp2[200];//定义三个字符串数组,作为字符串转整型的缓冲
CEdit *pEditN=(CEdit*)GetDlgItem(IDC_EDITN);
pEditN->GetWindowText(strN);
strcpy(Temp1,strN);
n=atoi(Temp1);
CEdit *pEdit3=(CEdit*)GetDlgItem(IDC_EDIT3);
pEdit3->GetWindowText(str3);
strcpy(Temp2,str3);
Sum=atoi(Temp2);
while(Sum>(int)(pow((double)n,(double)j)))//通过这个循环求出最高位,这样求出的j比最高位大1,j-1即为最高次方。
{
j++;
}
while(j>=1)
{
int itemp=(int)pow((double)n,(double)(j-1));//定义一个新的变量作为中间变量
if(Sum/itemp>=10)
{
AfxMessageBox("你输入的数不能用阿拉伯数字表示");
}
else
{
strTemp.Format("%d",Sum/itemp);
str5=str5+strTemp;
Sum=Sum%itemp;
j--;//逐位递减,若能整除返回值,不能则补0
}

}

CEdit *pEdit5=(CEdit*)GetDlgItem(IDC_EDIT5);
pEdit5->SetWindowText(str5);



}
...全文
183 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

15,440

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧