VC计算器实现

这篇博客介绍了如何使用VC进行计算器的编程实现,详细讲解了各个按钮的响应函数,如OnEqual()、OnButton0()到OnButton9()以及各种运算符的处理。在实现过程中,注意了小数和整数的判断、运算符输入后的界面清零、除数不能为0、运算符状态保存以及小数点和正负号的限制。然而,目前的实现尚不支持大数据运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                                                    VC实现计算器
   第一次完成VC下的小项目,略有所得,分享分享:
   在完成项目之前首先得有个程序的流程图,即程序的实现过程。这个非常的重要,没有一个设计好的流程图,在编码的时候,会发现寸步难行,因为你跟不不知道如何下手,从何处下手,连下手的地方都没有,那么难度可想而知。尽管写了较为详细的流程图,博主在编码过程中还是出现了许多没有考虑进去的问题。具体的思路如下:
  第一. 计算器开始步骤,要么进行输入显示,要么进行按键操作,基于此可以就这两方面展开进行分析。
  第二.要开始输入,那么输入完成后,就要进行按键操作,按键操作分为4种基本的运算符加上%,AC(归零),.(小数点)和=4种。
    输入数字后,进行加减乘除运算,那么需要将输入的数字进行保存,需要定义一个保存第一次输入数字的变量,当选择某一运算符号后,再进行输入,就需要保存第二次输入的变量。以此,当第二次进行运算符点击时,就应该显示两次的计算后的结果,并将这一结果作为第一次的输入,重复以上步骤。
    若选择+/-符号,则在输入的数字前面加上+/-符号。                                                                                                   
    若选择.则在小数点后面加入数字。        
    若选择AC,将显示0。
    若选择%,则将显示的数字除以100。
    若选择=,将运算结果显示出来。

总的来说,就是点击界面上的按钮,根据按钮的不同,选择不同的算法。
具体的代码如下:


`` // CCounterDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CCounter.h"
#include "CCounterDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CCCounterDlg dialog

CCCounterDlg::CCCounterDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CCCounterDlg::IDD, pParent)
{
    //{
   
   {AFX_DATA_INIT(CCCounterDlg)
    m_edit_show = _T("0");
    m_ICount=1;
    /*nSave=(int*)malloc(sizeof(int));*/
    nSave=new long int;
    dSave=new long double;
    m_strShow="";
    m_nResult=0;
    m_Cal.IsFlag=FALSE;
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCCounterDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{
   
   {AFX_DATA_MAP(CCCounterDlg)
    DDX_Text(pDX, IDC_EDIT_SHOW, m_edit_show);
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCCounterDlg, CDialog)
    //{
   
   {AFX_MSG_MAP(CCCounterDlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnClear)
    ON_BN_CLICKED(IDC_BUTTON_EQUAL, OnEqual)
    ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
    ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
    ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
    ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
    ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
    ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
    ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
    ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
    ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
    ON_BN_CLICKED(IDC_BUTTON0, OnButton0)
    ON_BN_CLICKED(IDC_BUTTON_POINT, OnButtonPoint)
    ON_BN_CLICKED(IDC_BUTTON_SYMBOL, OnButtonSymbol)
    ON_BN_CLICKED(IDC_BUTTON_DIVISE, OnButtonDivise)
    ON_BN_CLICKED(IDC_BUTTON_MULTIPLY, OnButtonMultiply)
    ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
    ON_BN_CLICKED(IDC_BUTTON_SUBTRACT, OnButtonSubtract)
    ON_BN_CLICKED(IDC_BUTTON_DIVISEALL, OnButtonDiviseall)
    ON_WM_DESTROY()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCCounterDlg message handlers

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

void CCCounterDlg::OnClear() 
{
    // TODO: Add your control notification handler code here

    GetDlgItem(IDC_EDIT_SHOW)->SetWindowText("0");
    m_Cal.nEvent=N_ZERO;
    m_Cal.IsFlag=FALSE;
}

void CCCounterDlg::OnEqual() 
{
    // TODO: Add your control notification handler code here
    CString strEql;
    double dResult;
    GetDlgItem(IDC_EDIT_SHOW)->GetWindowText(strEql);   
    m_nResult=atoi(strEql);
    dResult=atof(strEql);
    if(strEql.Find(".") != -1)
    {
        switch(m_Cal.nEvent)
        {
        case N_ZERO:
            dSave[0]=dResult;
            break;
        case N_ADD:
            if(!m_Cal.IsDouble)
            {
                dResult=nSave[0]+dResult;
                m_strShow.Format("%.2f",dResult);
            }
            else
            {
                dResult=dSave[0]+dResult;
                m_strShow.Format("%.2f",dResult);
          
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值