mfc编写的简单计算器程序
运行大于0 的数加减乘除都可以算对,可是当例如输入0.75-0.85之类的减法运算,却结果成了求和,这是为什么呢,求教,下面是源代码程序代码:
// jsq1Dlg.cpp : implementation file // #include "stdafx.h" #include "jsq1.h" #include "jsq1Dlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif int math_i; //存储运算符 double count=10;//小数位位权 char buffer[10];//数字转换字符串 double input_int=0,input_dec=0;//输入数的整数部位,输入数的小数部位 double input1=0,input2=0;//参与运算的操作数 double value=0;//计算结果 bool decimal=FALSE;//是否是小数 ///////////////////////////////////////////////////////////////////////////// // 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() ///////////////////////////////////////////////////////////////////////////// // CJsq1Dlg dialog CJsq1Dlg::CJsq1Dlg(CWnd* pParent /*=NULL*/) : CDialog(CJsq1Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CJsq1Dlg) m_ResultEdit = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CJsq1Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CJsq1Dlg) DDX_Control(pDX, IDC_OPRETOR_LIST, m_Operatorbox); DDX_Text(pDX, IDC_RESULT_EDIT, m_ResultEdit); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CJsq1Dlg, CDialog) //{{AFX_MSG_MAP(CJsq1Dlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_0_BUTTON, On0Button) ON_BN_CLICKED(IDC_1_BUTTON, On1Button) ON_BN_CLICKED(IDC_3_BUTTON, On3Button) ON_BN_CLICKED(IDC_4_BUTTON, On4Button) ON_BN_CLICKED(IDC_5_BUTTON, On5Button) ON_BN_CLICKED(IDC_6_BUTTON, On6Button) ON_BN_CLICKED(IDC_7_BUTTON, On7Button) ON_BN_CLICKED(IDC_8_BUTTON, On8Button) ON_BN_CLICKED(IDC_9_BUTTON, On9Button) ON_BN_CLICKED(IDC_DECIMAL_BUTTON, OnDecimalButton) ON_BN_CLICKED(IDC_EQUAL_BUTTON, OnEqualButton) ON_BN_CLICKED(IDC_2_BUTTON, On2Button) ON_LBN_SELCHANGE(IDC_OPRETOR_LIST, OnSelchangeOpretorList) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CJsq1Dlg message handlers BOOL CJsq1Dlg::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 m_Operatorbox.AddString("加"); m_Operatorbox.AddString("减"); m_Operatorbox.AddString("乘"); m_Operatorbox.AddString("除"); return TRUE; // return TRUE unless you set the focus to a control } void CJsq1Dlg::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 CJsq1Dlg::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 CJsq1Dlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CJsq1Dlg::On0Button() { // TODO: Add your control notification handler code here if(decimal) { input_dec=input_dec+0/count; count=count*10; } else input_int=input_int*10+0; value=input_int+input_dec; _gcvt(value,10,buffer); m_ResultEdit=(LPCTSTR)buffer; UpdateData(FALSE); } void CJsq1Dlg::On1Button() { // TODO: Add your control notification handler code here if(decimal) { input_dec=input_dec+1/count; count=count*10; } else input_int=input_int*10+1; value=input_int+input_dec; _gcvt(value,10,buffer); m_ResultEdit=(LPCTSTR)buffer; UpdateData(FALSE); } void CJsq1Dlg::On2Button() { // TODO: Add your control notification handler code here if(decimal) { input_dec=input_dec+2/count; count=count*10; } else input_int=input_int*10+2; value=input_int+input_dec; _gcvt(value,10,buffer); m_ResultEdit=(LPCTSTR)buffer; UpdateData(FALSE); } void CJsq1Dlg::On3Button() { // TODO: Add your control notification handler code here if(decimal) { input_dec=input_dec+3/count; count=count*10; } else input_int=input_int*10+3; value=input_int+input_dec; _gcvt(value,10,buffer); m_ResultEdit=(LPCTSTR)buffer; UpdateData(FALSE); } void CJsq1Dlg::On4Button() { // TODO: Add your control notification handler code here if(decimal) { input_dec=input_dec+4/count; count=count*10; } else input_int=input_int*10+4; value=input_int+input_dec; _gcvt(value,10,buffer); m_ResultEdit=(LPCTSTR)buffer; UpdateData(FALSE); } void CJsq1Dlg::On5Button() { // TODO: Add your control notification handler code here if(decimal) { input_dec=input_dec+5/count; count=count*10; } else input_int=input_int*10+5; value=input_int+input_dec; _gcvt(value,10,buffer); m_ResultEdit=(LPCTSTR)buffer; UpdateData(FALSE); } void CJsq1Dlg::On6Button() { // TODO: Add your control notification handler code here if(decimal) { input_dec=input_dec+6/count; count=count*10; } else input_int=input_int*10+6; value=input_int+input_dec; _gcvt(value,10,buffer); m_ResultEdit=(LPCTSTR)buffer; UpdateData(FALSE); } void CJsq1Dlg::On7Button() { // TODO: Add your control notification handler code here if(decimal) { input_dec=input_dec+7/count; count=count*10; } else input_int=input_int*10+7; value=input_int+input_dec; _gcvt(value,10,buffer); m_ResultEdit=(LPCTSTR)buffer; UpdateData(FALSE); } void CJsq1Dlg::On8Button() { // TODO: Add your control notification handler code here if(decimal) { input_dec=input_dec+8/count; count=count*10; } else input_int=input_int*10+8; value=input_int+input_dec; _gcvt(value,10,buffer); m_ResultEdit=(LPCTSTR)buffer; UpdateData(FALSE); } void CJsq1Dlg::On9Button() { // TODO: Add your control notification handler code here if(decimal) { input_dec=input_dec+9/count; count=count*10; } else input_int=input_int*10+9; value=input_int+input_dec; _gcvt(value,10,buffer); m_ResultEdit=(LPCTSTR)buffer; UpdateData(FALSE); } void CJsq1Dlg::OnDecimalButton() { // TODO: Add your control notification handler code here decimal=TRUE; } void CJsq1Dlg::OnEqualButton() { // TODO: Add your control notification handler code here input2=input_int+input_dec; decimal=FALSE; input_int=0; input_dec=0; count=10; switch(math_i) { case 0: value=input1+input2; break; case 1: value=input1-input2; break; case 2: value=input1*input2; break; case 3: value=input1/input2; break; } _gcvt(value,10,buffer); m_ResultEdit=(LPCTSTR)buffer; UpdateData(FALSE); } void CJsq1Dlg::OnSelchangeOpretorList() { // TODO: Add your control notification handler code here input1=input_int+input_dec; decimal=FALSE; input_int=0; input_dec=0; count=10; math_i=m_Operatorbox.GetCurSel(); }