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

📄 jsq.txt

📁 计算器代码
💻 TXT
字号:

// Mfc_CounterDlg.cpp : implementation file 
// 

#include "stdafx.h" 
#include "Mfc_Counter.h" 
#include "Mfc_CounterDlg.h" 
#include "SkinPlusPlus.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() 

///////////////////////////////////////////////////////////////////////////// 
// CMfc_CounterDlg dialog 

CMfc_CounterDlg::CMfc_CounterDlg(CWnd* pParent /*=NULL*/) 
: CDialog(CMfc_CounterDlg::IDD, pParent) 
{ 
//{{AFX_DATA_INIT(CMfc_CounterDlg) 
m_number = _T(""); 
//}}AFX_DATA_INIT 
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 

void CMfc_CounterDlg::DoDataExchange(CDataExchange* pDX) 
{ 
CDialog::DoDataExchange(pDX); 
//{{AFX_DATA_MAP(CMfc_CounterDlg) 
DDX_Text(pDX, IDC_EDIT1, m_number); 
//}}AFX_DATA_MAP 
} 

BEGIN_MESSAGE_MAP(CMfc_CounterDlg, CDialog) 
//{{AFX_MSG_MAP(CMfc_CounterDlg) 
ON_WM_SYSCOMMAND() 
ON_WM_PAINT() 
ON_WM_QUERYDRAGICON() 
ON_BN_CLICKED(IDC_ADD, OnAdd) 
ON_BN_CLICKED(IDC_MINUS, OnMinus) 
ON_BN_CLICKED(IDC_CHU, OnChu) 
ON_BN_CLICKED(IDC_AMOUNT, OnAmount) 
ON_BN_CLICKED(IDC_RIDE, OnRide) 
ON_BN_CLICKED(IDC_EIGHT, OnEight) 
ON_BN_CLICKED(IDC_FIVE, OnFive) 
ON_BN_CLICKED(IDC_FOUR, OnFour) 
ON_BN_CLICKED(IDC_NINE, OnNine) 
ON_BN_CLICKED(IDC_ONE, OnOne) 
ON_BN_CLICKED(IDC_SEVEN, OnSeven) 
ON_BN_CLICKED(IDC_SIX, OnSix) 
ON_BN_CLICKED(IDC_THREE, OnThree) 
ON_BN_CLICKED(IDC_TWO, OnTwo) 
ON_BN_CLICKED(IDC_ZERO, OnZero) 
ON_WM_CREATE() 
ON_WM_SHOWWINDOW() 
ON_BN_CLICKED(IDC_POINT, OnPoint) 
ON_BN_CLICKED(IDC_CHU2, OnChu2) 
ON_COMMAND(ID_MENUITEM32771, OnMenuitem32771) 
ON_COMMAND(ID_MENUITEM32772, OnMenuitem32772) 
ON_COMMAND(ID_MENUITEM32773, OnMenuitem32773) 
ON_WM_CLOSE() 
//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 

///////////////////////////////////////////////////////////////////////////// 
// CMfc_CounterDlg message handlers 

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

void CMfc_CounterDlg::OnOK() 
{ 
// TODO: Add extra validation here 
} 

void CMfc_CounterDlg::OnAdd() 
{ 
// TODO: Add your control notification handler code here 

m_isBtnDown=TRUE; //判断+号是否按下 
m_countType='+'; 


OnAmount() ; //计算结果 
} 

void CMfc_CounterDlg::OnMinus() 
{ 
// TODO: Add your control notification handler code here 

m_isBtnDown=TRUE; //判断-号是否按下 
m_countType='-'; 


OnAmount() ; //计算结果 
} 

void CMfc_CounterDlg::OnChu() 
{ 
// TODO: Add your control notification handler code here 

m_isBtnDown=TRUE; //判断/号是否按下 
m_countType='/'; 

OnAmount() ; //计算结果 
} 


void CMfc_CounterDlg::OnRide() 
{ 
// TODO: Add your control notification handler code here 

m_isBtnDown=TRUE; //判断*号是否按下 
m_countType='*'; 

OnAmount() ; //计算结果 
} 

void CMfc_CounterDlg::OnEight() 
{ 
OnCount('8'); //是按下那个数字 
} 

void CMfc_CounterDlg::OnFive() 
{ 
OnCount('5'); //是按下那个数字 
} 

void CMfc_CounterDlg::OnFour() 
{ 
OnCount('4'); //是按下那个数字 
} 

void CMfc_CounterDlg::OnNine() 
{ 
OnCount('9'); //是按下那个数字 
} 

void CMfc_CounterDlg::OnOne() 
{ 
// TODO: Add your control notification handler code here 

OnCount('1'); //是按下那个数字 
} 

void CMfc_CounterDlg::OnSeven() 
{ 
// TODO: Add your control notification handler code here 
OnCount('7'); //是按下那个数字 
} 

void CMfc_CounterDlg::OnSix() 
{ 
// TODO: Add your control notification handler code here 
OnCount('6'); //是按下那个数字 
} 

void CMfc_CounterDlg::OnThree() 
{ 
// TODO: Add your control notification handler code here 
OnCount('3'); //是按下那个数字 
} 

void CMfc_CounterDlg::OnTwo() 
{ 
// TODO: Add your control notification handler code here 
OnCount('2'); //是按下那个数字 
} 

void CMfc_CounterDlg::OnZero() 
{ 
// TODO: Add your control notification handler code here 
OnCount('0'); //是按下那个数字 
} 

void CMfc_CounterDlg::OnAmount() 
{ 
// TODO: Add your control notification handler code here 



m_isBtnDown=TRUE; //判断=号是否按下 


m_secondNum=atof(m_number); 

SetDlgItemText(IDC_EDIT1,m_number); 


if(m_countType=='+') 
{ 
m_count.Format("%lf",m_firstNum+m_secondNum); 
} 
else if(m_countType=='-') 
{ 
if(m_firstNum!=0) 
{ 
m_count.Format("%lf",m_firstNum-m_secondNum); 
} 
else 
{m_firstNum=m_secondNum; 
m_count.Format("%lf",m_firstNum); 
} 
} 
else if(m_countType=='*') 
{ 
if(m_firstNum==0) 
{ 
m_firstNum=1; 
} 
m_count.Format("%lf",m_firstNum*m_secondNum); 

} 
else if(m_countType=='/') 
{ if(m_firstNum==0) 
{ 
m_firstNum=m_secondNum; 
m_secondNum=1; 
} 
if(m_secondNum!=0) 
{ 
m_count.Format("%lf",m_firstNum/m_secondNum); 
} 
else 
{ 
m_number="被除数不能为0,重新输入!"; 
UpdateData(FALSE); 
} 
} 

UpdateData(FALSE); 
SetDlgItemText(IDC_EDIT1,m_count); 


} 

void CMfc_CounterDlg::OnCount(char ch) //是按下那个数字 
{ 


if(m_isBtnDown==TRUE ) //如果用户按下了 + - * / 其中一个 
{ 
UpdateData(); 
m_firstNum=atof(m_number); 
m_number.Empty(); 
m_number=m_number+ch; 
SetDlgItemText(IDC_EDIT1,m_number); 
UpdateData(FALSE); 

m_isBtnDown=FALSE; 

} 
else 
{ 
UpdateData(); 

m_number=m_number+ch; 
m_secondNum=atof(m_number); 
SetDlgItemText(IDC_EDIT1,m_number); 

UpdateData(FALSE); 

} 
} 

int CMfc_CounterDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{ 
if (CDialog::OnCreate(lpCreateStruct) == -1) 
return -1; 

// TODO: Add your specialized creation code here 
m_secondNum =0; 
m_firstNum =0; 

m_menu.LoadMenu(IDR_MENU1); 
SetMenu(&m_menu); 
m_menu.Detach(); 

return 0; 
} 

void CMfc_CounterDlg::OnShowWindow(BOOL bShow, UINT nStatus) 
{ 
CDialog::OnShowWindow(bShow, nStatus); 

((CEdit*)GetDlgItem(IDC_EDIT1))->SetLimitText(12); 

SetWindowText("计算器"); 
// TODO: Add your message handler code here 

} 

void CMfc_CounterDlg::OnPoint() 
{ 
// TODO: Add your control notification handler code here 

CString str; 
GetDlgItemText(IDC_EDIT1,str); 

m_isPoint=m_number.Find('.'); 
if( m_isPoint==-1 && (str.Find('.')==-1) ) 
{ 
m_number=m_number+'.'; 
UpdateData(FALSE); 
} 

} 

void CMfc_CounterDlg::OnChu2() 
{ 
// TODO: Add your control notification handler code here 
m_number=""; 
UpdateData(FALSE); 
m_firstNum=m_secondNum=0; 
}

⌨️ 快捷键说明

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