MFC Calculator

使用平臺:VS2010windows

語言:C++app

 

先預覽下:less

 

  1. 新建工程:

選擇MFC應用程序,輸入工程名稱,而後肯定ide

  1. 點擊下一步後出現以下對話框,選擇基於對話框,點擊完成

  

  1. MS自動幫咱們建立了一個基礎模板,並打開界面資源編輯窗口:

 

  使用工具箱向界面拖控件,添加一個BUTTON控件,修改其屬性:函數

 

出現屬性對話框:修改其Caption及ID,其餘的屬性就不羅嗦了工具

相似的將其餘的控件拖放到合適位置,並調整好位置,設定相應的屬性。spa

  1. 打開類視圖,有三個類關於三個類的說明就不解釋了

咱們雙擊BUTTON按鈕就會自動幫我建立點擊事件處理代碼:看到在XXXDlg.cpp文件中添加了以下代碼:void CtestDlg::OnBnClickedButton1()操作系統

{指針

    // TODO: 在此添加控件通知處理程序代碼code

}

 

在其頭文件中添加了該函數的聲明:afx_msg void OnBnClickedButton1();

在XXXDlg.cpp 文件中添加了代碼:

這個稱爲消息映射表:咱們知道windows系統能夠相應不少事件,可是隻是相應特定的事件,若是什麼事件都響應的話,會把windows累死。那麼MFC提供了 消息映射的機制,每當事件發生時就在消息映射表中查詢,看有沒有定義這個事件,若是有的話,就進入消息處理程序,若沒有就忽略這個事件。在MFC處理消息的函數是回調函數,咱們看一下回調函數:【來自MSDN】

callback

The [callback] attribute declares a static callback function that exists on the client side of the distributed application. Callback functions provide a way for the server to execute code on the client.

[callback [ , function-attr-list] ] type-specifier [ptr-declarator] function-name(

        [ [attribute-list] ] type-specifier [declarator]

        , ...);

Parameters

function-attr-list

Specifies zero or more attributes that apply to the function. Valid function attributes are [local]; the pointer attribute [ref], [unique], or [ptr]; and the usage attributes [string], [ignore], and [context_handle]. Separate multiple attributes with commas.

type-specifier

Specifies a base_type, struct, union, enum type, or type identifier. An optional storage specification can precede type-specifier.

ptr-declarator

Specifies zero or more pointer declarators. A pointer declarator is the same as the pointer declarator used in C; it is constructed from the * designator, modifiers such as far, and the qualifier const.

function-name

Specifies the name of the remote procedure.

attribute-list

Specifies zero or more directional attributes, field attributes, usage attributes, and pointer attributes appropriate for the specified parameter type. Separate multiple attributes with commas.

declarator

Specifies a standard C declarator such as identifiers, pointer declarators, and array declarators. For more information, see Array and Sized-Pointer Attributes, arrays, and Arrays and Pointers. The parameter-name identifier is optional.

Remarks

The [callback] function is useful when the server must obtain information from the client. If server applications were supported on Windows 3.x, the server could make a call to a remote procedure on the Windows 3.x server to obtain the needed information. The callback function accomplishes the same purpose and lets the server query the client for information in the context of the original call.

Callbacks are special cases of remote calls that execute as part of a single thread. A callback is issued in the context of a remote call. Any remote procedure defined as part of the same interface as the static callback function can call the callback function.

Only the connection-oriented and local-protocol sequences support the callback attribute. If an RPC interface uses a connectionless (datagram) protocol sequence, calls to procedures with the callback attribute will fail.

Handles cannot be used as parameters in callback functions. Because callbacks always execute in the context of a call, the binding handle used by the client to make the call to the server is also used as the binding handle from the server to the client.

Callbacks can nest to any depth.

Example

[callback] HRESULT DisplayString([in, string] char * p1);

對比LabView中的事件處理機制,因爲LabVIEW不依賴於windows操做系統的事件處理機制(有利於程序的移植),那麼LabVIEW使用的是輪詢機制,一個事件結構只能響應一個事件,若要連續響應就要放在循環中。根據個人理解,這個事件結構至關於消息映射表和事件處理代碼,而循環則至關於回調函數。關於事件就說到這裏。

 

手動添加的話  就分別在這三個位置添加相應的代碼便可

  1. 關於屬性:

 

類嚮導中能夠爲各個控件添加各類事件處理代碼,添加變量能夠將控件值和類的屬性值進行綁定,UpdateData(False/true)函數能夠更新界面顯示值和屬性值,固然也可得到該控件的指針後進行修改。

  1. 表達式的計算:使用棧結構。

 

 

 

附:

代碼:

// Expression處理

void CCalculatorDlg::OnBnClickedButton17()//0

{

    // TODO: 在此添加控件通知處理程序代碼

    UpdateData();

    if(m_cal.GetLength())

    if(m_cal.GetAt(m_cal.GetLength()-1)==L'='){

        m_cal.Empty();

    }

    m_result.SetWindowTextW(NULL);

    m_cal=m_cal+'0';

    UpdateData(false);

}

 

 

void CCalculatorDlg::OnBnClickedButton18()//.

{

    // TODO: 在此添加控件通知處理程序代碼

    UpdateData();

    if(m_cal.GetLength())

    if(m_cal.GetAt(m_cal.GetLength()-1)==L'='){

        m_cal.Empty();

    }

    m_result.SetWindowTextW(NULL);

    m_cal=m_cal+'.';

    UpdateData(false);

}

 

 

void CCalculatorDlg::OnBnClickedButton22()//Ans

{

    // TODO: 在此添加控件通知處理程序代碼

    UpdateData();

    if(m_cal.GetLength())

    if(m_cal.GetAt(m_cal.GetLength()-1)==L'='){

        m_cal.Empty();

    }

    m_result.SetWindowTextW(NULL);

    m_cal=m_cal+L"Ans";

    UpdateData(false);

}

 

 

void CCalculatorDlg::OnBnClickedButton12()//1

{

    // TODO: 在此添加控件通知處理程序代碼

    UpdateData();//獲取屬性值

    if(m_cal.GetLength())

    if(m_cal.GetAt(m_cal.GetLength()-1)==L'='){

        m_cal.Empty();

    }

    m_result.SetWindowTextW(NULL);

    m_cal=m_cal+'1';

    UpdateData(false); //更新前面板

}

 

 

void CCalculatorDlg::OnBnClickedButton13()//2

{

    // TODO: 在此添加控件通知處理程序代碼

    UpdateData();

    if(m_cal.GetLength())

    if(m_cal.GetAt(m_cal.GetLength()-1)==L'='){

        m_cal.Empty();

    }

    m_result.SetWindowTextW(NULL);

    m_cal=m_cal+'2';

    UpdateData(false);

}

 

 

void CCalculatorDlg::OnBnClickedButton14()//3

{

    // TODO: 在此添加控件通知處理程序代碼

    UpdateData();

    if(m_cal.GetLength())

    if(m_cal.GetAt(m_cal.GetLength()-1)==L'='){

        m_cal.Empty();

    }

    m_result.SetWindowTextW(NULL);

    m_cal=m_cal+'3';

    UpdateData(false);

}

 

 

void CCalculatorDlg::OnBnClickedButton7()//4

{

    // TODO: 在此添加控件通知處理程序代碼

    UpdateData();

    if(m_cal.GetLength())

    if(m_cal.GetAt(m_cal.GetLength()-1)==L'='){

        m_cal.Empty();

    }

    m_result.SetWindowTextW(NULL);

    m_cal=m_cal+'4';

    UpdateData(false);

}

 

 

void CCalculatorDlg::OnBnClickedButton8()//5

{

    // TODO: 在此添加控件通知處理程序代碼

    UpdateData();

    if(m_cal.GetLength())

    if(m_cal.GetAt(m_cal.GetLength()-1)==L'='){

        m_cal.Empty();

    }

    m_result.SetWindowTextW(NULL);

    m_cal=m_cal+'5';

    UpdateData(false);

}

 

 

void CCalculatorDlg::OnBnClickedButton9()//6

{

    // TODO: 在此添加控件通知處理程序代碼

    UpdateData();

    if(m_cal.GetLength())

    if(m_cal.GetAt(m_cal.GetLength()-1)==L'='){

        m_cal.Empty();

    }

    m_result.SetWindowTextW(NULL);

    m_cal=m_cal+'6';

    UpdateData(false);

}

 

 

 

void CCalculatorDlg::OnBnClickedButton2()//7

{

    // TODO: 在此添加控件通知處理程序代碼

    UpdateData();

    if(m_cal.GetLength())

    if(m_cal.GetAt(m_cal.GetLength()-1)==L'='){

        m_cal.Empty();

    }

    m_result.SetWindowTextW(NULL);

    m_cal=m_cal+'7';

    UpdateData(false);

}

 

 

void CCalculatorDlg::OnBnClickedButton3()//8

{

    // TODO: 在此添加控件通知處理程序代碼

    UpdateData();

    if(m_cal.GetLength())

    if(m_cal.GetAt(m_cal.GetLength()-1)==L'='){

        m_cal.Empty();

    }

    m_result.SetWindowTextW(NULL);

    m_cal=m_cal+'8';

    UpdateData(false);

}

 

 

void CCalculatorDlg::OnBnClickedButton4()//9

{

    // TODO: 在此添加控件通知處理程序代碼

    UpdateData();

    if(m_cal.GetLength())

        if(m_cal.GetAt(m_cal.GetLength()-1)==L'='){

            m_cal.Empty();

        }

    m_result.SetWindowTextW(NULL);

    m_cal=m_cal+'9';

    UpdateData(false);

}

 

 

void CCalculatorDlg::OnBnClickedButton5()//del

{

    // TODO: 在此添加控件通知處理程序代碼

    UpdateData();

    if(m_cal.GetLength())

        m_cal.Delete(m_cal.GetLength()-1);

    m_result.SetWindowTextW(NULL);

    UpdateData(false);

}

 

 

void CCalculatorDlg::OnBnClickedButton6()//AC

{

    // TODO: 在此添加控件通知處理程序代碼

    UpdateData();

   

    m_cal.Empty();

    m_result.SetWindowTextW(NULL);

    UpdateData(false);

}

 

 

void CCalculatorDlg::OnBnClickedButton19()//(

{

    // TODO: 在此添加控件通知處理程序代碼

    UpdateData();

    if(m_cal.GetLength())

    if(m_cal.GetAt(m_cal.GetLength()-1)==L'='){

        m_cal.Empty();

    }

    m_result.SetWindowTextW(NULL);

    m_cal=m_cal+'(';

    UpdateData(false);

}

 

 

void CCalculatorDlg::OnBnClickedButton20()//)

{

    // TODO: 在此添加控件通知處理程序代碼

    UpdateData();

    if(m_cal.GetLength())

    if(m_cal.GetAt(m_cal.GetLength()-1)==L'='){

        m_cal.Empty();

    }

    m_result.SetWindowTextW(NULL);

    m_cal=m_cal+')';

    UpdateData(false);

}

 

 

 

 

void CCalculatorDlg::OnBnClickedButton10()//×

{

    // TODO: 在此添加控件通知處理程序代碼

    UpdateData();

    if(m_cal.GetLength())

    if(m_cal.GetAt(m_cal.GetLength()-1)==L'='){

        m_cal.Empty();

    }

    m_result.SetWindowTextW(NULL);

    if(m_cal.GetLength()){

        m_cal=m_cal+L'×';

    }

    else MessageBox(L"輸入錯誤!");

    UpdateData(false);

}

 

 

void CCalculatorDlg::OnBnClickedButton11()// ÷

{

    // TODO: 在此添加控件通知處理程序代碼

    UpdateData();

    if(m_cal.GetLength())

    if(m_cal.GetAt(m_cal.GetLength()-1)==L'='){

        m_cal.Empty();

    }

    m_result.SetWindowTextW(NULL);

    if(m_cal.GetLength()){

        m_cal=m_cal+L'÷';

    }

    else MessageBox(L"輸入錯誤!");

    UpdateData(false);

}

 

 

void CCalculatorDlg::OnBnClickedButton15()// +

{

    // TODO: 在此添加控件通知處理程序代碼

    UpdateData();

    if(m_cal.GetLength())

    if(m_cal.GetAt(m_cal.GetLength()-1)==L'='){

        m_cal.Empty();

    }

    m_result.SetWindowTextW(NULL);

    m_cal=m_cal+'+';

    UpdateData(false);

}

 

 

void CCalculatorDlg::OnBnClickedButton16()//-

{

    // TODO: 在此添加控件通知處理程序代碼]

    UpdateData();

    if(m_cal.GetLength()){

        if(m_cal.GetAt(m_cal.GetLength()-1)==L'='){

            m_cal.Empty();

        }

    }

    m_result.SetWindowTextW(NULL);

    m_cal=m_cal+'-';

    UpdateData(false);

}

 

 

void CCalculatorDlg::OnBnClickedButton21()// =

{

    // TODO: 在此添加控件通知處理程序代碼

    UpdateData();

 

    m_cal=m_cal+'=';

    UpdateData(false);

 

// 計算表達式的值

    CString str;

    str.Format(L"%lf",GetResult(m_cal,m_store));

// 去掉尾部的零

    if(str.Find(L'.')>=0){

        while(str.GetAt(str.GetLength()-1)==L'0'){

            str.Delete(str.GetLength()-1);

            if(str.GetAt(str.GetLength()-1)==L'.'){

                str.Delete(str.GetLength()-1);

                break;

            }

        }

    }

// 顯示結果

    m_result.SetWindowTextW(str);

// 結果

    m_store.Empty();

    m_result.GetWindowTextW(m_store);

 

}

 

 

///在構造函數中添加定時器:SetTimer(0,1000,0);

///定義響應函數

void CCalculatorDlg::OnTimer(UINT_PTR nIDEvent)

{

    // TODO: 在此添加消息處理程序代碼和/或調用默認值

    CTime t = CTime::GetCurrentTime();

    CString Str;

    if(m_DtoT){

        Str.Format(L"%d:%.2d:%.2d",t.GetHour(),t.GetMinute(),t.GetSecond());

    }

    else

    {

        wchar_t w=L'';

        switch(t.GetDayOfWeek())

        {

        case 2:

            w=L'一';

            break;

        case 3:

            w=L'二';

            break;

        case 4:

            w=L'三';

            break;

        case 5:

            w=L'四';

            break;

        case 6:

            w=L'五';

            break;

        case 7:

            w=L'六';

            break;

        case 1:

            w=L'日';

            break;

        default:

            break;

        }

        Str.Format(L"%d/%d/%d  星期%c",t.GetYear(),t.GetMonth(),t.GetDay(),w);

    }

    m_time_date.SetWindowTextW(Str);

    CDialogEx::OnTimer(nIDEvent);

 

 

}

 

 

void CCalculatorDlg::OnClose()

{

    // TODO: 在此添加消息處理程序代碼和/或調用默認值

    KillTimer(0);//銷燬定時器

    CDialogEx::OnClose();

}

 

 

void CCalculatorDlg::OnBnClickedTime()

{

    // TODO: 在此添加控件通知處理程序代碼

    m_DtoT=~ m_DtoT;

}

 

 

表達式計算代碼:

#include<stack>

using std::stack;

 

 

//計算運算符的優先級

int pp(wchar_t c)

{

    int k=-2;

    switch(c){

    case '*':case '/':

        k=2;

        break;

    case '+':case '-':

        k=1;

        break;

    case '(':case ')':

        k=0;

        break;

    case '=':

        k=-1;

        break;

    }

    return k;

}

 

 

//將字符串轉換成實數

double shishu(CString *s,int *k)

{

    double x,y=1.0;

    int flag=1;

    x=0.0;

    wchar_t c=s->GetAt(*k);

    while(c>='0'&&c<='9'||c=='.')

    {

        *k=*k+1;

        if(c>='0'&&c<='9')

            if(flag==0)

            {

                y*=0.1;

                x+=(y*(c-48));

            }

            else

                x=10*x+(c-48);

        else flag=0;

        c=s->GetAt(*k);

    }

    return x;

 

}

 

 

double GetResult(CString str,CString str1=NULL){

    //格式化str

   

    int i=0;

    while(0!=str.Replace(L"Ans",str1));

    while(str.GetAt(i)==L'-'){

        if(i==0)

        {

            str.Insert(str.Find(L'-'),L'0');

        }

        else if(str.GetAt(i-1)==L'(')

                    str.Insert(i,L'0');

    }

 

    i=0;

    while(str.GetAt(i)!=L'='){

        switch(str.GetAt(i))

        {

        case L'÷':

            str.Delete(i);

            str.Insert(i,'/');

            break;

        case L'×':

            str.Delete(i);

            str.Insert(i,'*');

            break;

        case L' ':

            str.Delete(i);

            break;

        }

 

        i++;

    }

//格式化結束

 

 

    stack<double>st_data;

    stack<wchar_t>st_op;

    st_op.push('=');

   

 

    int flag=1,k;

 

    k=0;

    double x,y;

    wchar_t c=str.GetAt(k);

 

    while(flag)

    {

        if(c>=L'0'&&c<=L'9'||c==L'.')

            st_data.push(shishu(&str,&k));

        else if(c==L'('||pp(c)>pp(st_op.top()))

        {

            st_op.push(c);

            k=k+1;

        }

        else if((c==L'=')&&(st_op.top()==L'='))

            flag=0;

        else if((c==L')')&&(st_op.top()==L'('))

        {

            st_op.pop();

            k++;

        }

        else if(pp(c)<=pp(st_op.top()))

        {

            y=st_data.top();

            st_data.pop();

            x=st_data.top();

            st_data.pop();

            c=st_op.top();

            st_op.pop();

            switch(c)

            {

            case L'+':

                x=x+y;

                break;

            case L'-':

                x=x-y;

                break;

            case L'*':

                x=x*y;

                break;

            case L'/':

                x=x/y;

                break;         

            }

            st_data.push(x);

        }

        c=str.GetAt(k);

    }

    return st_data.top();

}

相關文章
相關標籤/搜索