| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5306 人关注过本帖
标题:mfc编写的简单计算器程序
只看楼主 加入收藏
忆泪
Rank: 2
等 级:论坛游民
帖 子:42
专家分:49
注 册:2011-10-22
结帖率:75%
收藏
已结贴  问题点数:10 回复次数:4 
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();
}

搜索更多相关主题的帖子: file 源代码 
2012-09-13 20:06
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:3 
工程打包传上来
2012-09-14 15:09
唱片
Rank: 1
等 级:新手上路
帖 子:2
专家分:2
注 册:2012-7-3
收藏
得分:3 
新手,,求指导,,,为什么直接复制到VC  6.没有办法直接工作
2012-09-14 21:52
maofuhui
Rank: 1
等 级:新手上路
帖 子:1
专家分:2
注 册:2012-9-18
收藏
得分:3 
坐等高手
2012-09-18 20:53
cqg657293550
Rank: 1
等 级:新手上路
帖 子:1
专家分:2
注 册:2012-9-18
收藏
得分:3 
会不会是 你列表框的 编译后 加减乘除的顺序和你switch里面的值  顺序 不一致
2012-09-18 21:14
快速回复:mfc编写的简单计算器程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023884 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved