利用MFC編寫計算器

端午節這兩天沒什麼事,就用MFC寫了一個簡單的計算器,界面作的也比較簡單,以下圖1,能夠進行簡單的加、減、乘和除功能,小數點顯示這塊作的不是很好,好比輸入1.2,不會一個個的顯示,而是先顯示「1」,後同時顯示「.2」,還有就是遇到0.00時,顯示的也不夠人性化,哎,就這樣吧。。。ide

                                                                                                              圖1函數

一、創建工程:New -> Projects,選擇Win32 MFC AppWizard(exe),並輸入工程(counter)名字及設置好路徑,點擊OK,選擇「Dialog based」,基於對話框,直接點finish。ui

二、將對話框進行簡單的處理,把無關的按鈕刪掉,添加本身的按鈕及Edit Box,如圖2。spa

                                                                          圖2code

三、對按鈕及Edit Box進行屬性設置,好比按鈕1屬性設置爲「IDC_BUTTON_1」,2設置爲IDC_BUTTON_2,一直類推。Edit Box不只要設置ID(IDC_DISPLAY),還要設置相關連的成員變量,右鍵,選擇ClasssWizard..,在點擊Add Variable..,這裏設置爲double m_display。blog

四、對每一個按鈕添加消息處理函數,只需雙擊該按鈕,按照提示點OK,就進入到了該函數入口處。it

  1設置void CCounterDlg::OnButton1();io

  2設置void CCounterDlg::OnButton2();event

  ....class

  其餘類推。

      加 void CCounterDlg::OnButtonAdd();

  減void CCounterDlg::OnButtonSub();

      乘void CCounterDlg::OnButtonMult();

      除void CCounterDlg::OnButtonDiv();

      = void CCounterDlg::OnButtonEqual();

五、定義成員變量

 int count;  //計數
 BOOL dot_flg; //小數點標誌
 BOOL continue_flg;//沒按運算符,是否一直連續按數字按鈕標誌
 BOOL str_flag;//有沒按運算符標誌
 CString str;//記錄當前按的運算符
 double result; //num1和num2運算的結果
 double num1; //按運算符以前的第一個數

 double num2; //按運算符以後的第二個數

六、定義成員函數,該函數主要是對按下的數字進行疊加,跟蹤num1和num2:

double CCounterDlg::buttonDownNum(int numSize);

七、當按下以後數字要及時顯示出來,須要用到:

UpdateData(FALSE);  //該函數是對Edit Box的內容進行更新,FALSE是有內部數成員的數據值更新到界面上,TRUE是從界面上更新到內部的數據成員上。

GetDlgItem(IDC_DISPLAY)->SetWindowText("小數點");//能夠利用這個語句直接在Edit Box上顯示內容「小數點」。只是提示本身也能夠用這個來顯示,程序中註釋掉了。

八、代碼實現:

//貼出本身添加的代碼,其餘代碼默認有的
#include "math.h"

// CCounterDlg dialog
CCounterDlg::CCounterDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CCounterDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CCounterDlg)
    //初始化
    m_display = 0.0;
    num1 = 0;
    num2 = 0;
    result = 0.0;
    str = "";
    str_flag = FALSE;
    continue_flg = FALSE;
    downDot = FALSE;
    count = 1;

    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

double CCounterDlg::buttonDownNum(int numSize)
{
    if (!str_flag) //運算符沒有被按下,那就是num1
    {
        if (!continue_flg) //按下一次
        {
            num1 = numSize;
            continue_flg =TRUE;
        }
        else
        {
            num1 = num1*pow(10,count) + numSize;
            if (downDot)
            {
                num1 = num1/(pow(10,count));
                count++;
            }
        }
        
        return num1;
    }
    else  //有運算符被按下
    {
        if (!continue_flg) //是否連續按下
        {
            num2 = numSize;
            continue_flg =TRUE;
        }
        else
        {
            num2 = num2*pow(10,count) + numSize;
            if (downDot)
            {
                num2 = num2/(pow(10,count));
                count++;
            }
        }
        
        return num2;
    }
}


void CCounterDlg::OnButton0() 
{
    // TODO: Add your control notification handler code here
    
    m_display = buttonDownNum(0);    
    UpdateData(FALSE);    
}

void CCounterDlg::OnButton1() 
{
    // TODO: Add your control notification handler code here
    m_display = buttonDownNum(1);
    UpdateData(FALSE);
}

void CCounterDlg::OnButton2() 
{
    // TODO: Add your control notification handler code here
    
    m_display = buttonDownNum(2);
    
    UpdateData(FALSE);
}

void CCounterDlg::OnButton3() 
{
    // TODO: Add your control notification handler code here
    m_display = buttonDownNum(3);
    UpdateData(FALSE);
}

void CCounterDlg::OnButton4() 
{
    // TODO: Add your control notification handler code here
    m_display = buttonDownNum(4);
    UpdateData(FALSE);
}

void CCounterDlg::OnButton5() 
{
    // TODO: Add your control notification handler code here
    m_display = buttonDownNum(5);
    UpdateData(FALSE);
}

void CCounterDlg::OnButton6() 
{
    // TODO: Add your control notification handler code here
    m_display = buttonDownNum(6);
    UpdateData(FALSE);
}

void CCounterDlg::OnButton7() 
{
    // TODO: Add your control notification handler code here
    m_display = buttonDownNum(7);
    UpdateData(FALSE);
}

void CCounterDlg::OnButton8() 
{
    // TODO: Add your control notification handler code here
    m_display = buttonDownNum(8);
    UpdateData(FALSE);
}

void CCounterDlg::OnButton9() 
{
    // TODO: Add your control notification handler code here
    m_display = buttonDownNum(9);
    UpdateData(FALSE);
}

void CCounterDlg::OnButtonDot() //小數點
{
    // TODO: Add your control notification handler code here
    downDot = TRUE; 
//    GetDlgItem(IDC_DISPLAY)->SetWindowText("小數點"); //能夠利用該函數在編輯框中顯示信息
}

void CCounterDlg::OnButtonAdd() //
{
    // TODO: Add your control notification handler code here
    str = "+";    
    str_flag = TRUE;
    continue_flg = FALSE;
    downDot = FALSE;
    count = 1;
    UpdateData(FALSE);
}

void CCounterDlg::OnButtonSub()  //
{
    // TODO: Add your control notification handler code here
    str = "-";    
    str_flag = TRUE;
    continue_flg = FALSE;
    downDot = FALSE;
    count = 1;
    UpdateData(FALSE);
}

void CCounterDlg::OnButtonMult()  //
{
    // TODO: Add your control notification handler code here
    str = "*";    
    str_flag = TRUE;
    continue_flg = FALSE;
    downDot = FALSE;
    count = 1;
    UpdateData(FALSE);
}

void CCounterDlg::OnButtonDiv()  //
{
    // TODO: Add your control notification handler code here
    str = "/";    
    str_flag = TRUE;
    continue_flg = FALSE;
    downDot = FALSE;
    count = 1;
    UpdateData(FALSE);
}

void CCounterDlg::OnButtonEqual() 
{
    // TODO: Add your control notification handler code here
    if(str == "+")
        result = num1 + num2;
    else if(str == "-")
        result = num1 - num2;
    else if(str == "*")
        result = num1 * num2;
    else if(str == "/")
    {
        result = double(num1) / double(num2);
    }

    num1 = result;
    m_display = result;
    str_flag = FALSE;
    continue_flg = FALSE;
    downDot = FALSE;
    count = 1;
    UpdateData(FALSE);
}

void CCounterDlg::OnButtonClear() 
{
    // TODO: Add your control notification handler code here

    num1 = 0;
    num2 = 0;
    m_display = 0;
    continue_flg = FALSE;
    str_flag = FALSE;
    downDot = FALSE;
    count = 1;
    UpdateData(FALSE);
}
View Code
相關文章
相關標籤/搜索