Today, when I was bored at school, I made a calculator, and I wanted to go to bed on network theory. So I used VC to write a calculator. It was not perfect, but the function was implemented ..
// Calculator DLG. cpp: implementation file
//
# Include "stdafx. H"
# Include "Calculator. H"
# Include "Calculator DLG. H"
# Ifdef _ debug
# Define new debug_new
# Endif
// Used for the caboutdlg dialog box of the "about" menu item of the application
Class caboutdlg: Public cdialog
{
Public:
Caboutdlg ();
// Dialog box data
Enum {IDD = idd_aboutbox };
Protected:
Virtual void dodataexchange (cdataexchange * PDX); // supported by DDX/DDV
// Implementation
Protected:
Declare_message_map ()
};
Caboutdlg: caboutdlg (): cdialog (caboutdlg: IDD)
{
}
Void caboutdlg: dodataexchange (cdataexchange * PDX)
{
Cdialog: dodataexchange (PDX );
}
Begin_message_map (caboutdlg, cdialog)
End_message_map ()
// C calculator DLG dialog box
C calculator DLG: C calculator DLG (cwnd * pparent/* = NULL */)
: Cdialog (C calculator DLG: IDD, pparent)
, Text (_ T (""))
{
M_hicon = afxgetapp ()-> loadicon (idr_mainframe );
}
Void C calculator DLG: dodataexchange (cdataexchange * PDX)
{
Cdialog: dodataexchange (PDX );
Ddx_text (PDX, idc_edit1, text );
}
Begin_message_map (C calculator DLG, cdialog)
On_wm_syscommand ()
On_wm_paint ()
On_wm_querydragicon ()
//} Afx_msg_map
On_bn_clicked (idc_button1, & C calculator DLG: onbnclickedbutton1)
On_bn_clicked (idok, & C calculator DLG: onbnclickedok)
On_bn_clicked (idc_button2, & C calculator DLG: onbnclickedbutton2)
On_bn_clicked (idc_button3, & C calculator DLG: onbnclickedbutton3)
On_bn_clicked (idc_button4, & C calculator DLG: onbnclickedbutton4)
On_bn_clicked (idc_button10, & C calculator DLG: onbnclickedbutton10)
On_bn_clicked (idc_button5, & C calculator DLG: onbnclickedbutton5)
On_bn_clicked (idc_button6, & C calculator DLG: onbnclickedbutton6)
On_bn_clicked (idc_button7, & C calculator DLG: onbnclickedbutton7)
On_bn_clicked (idc_button8, & C calculator DLG: onbnclickedbutton8)
On_bn_clicked (idc_button9, & C calculator DLG: onbnclickedbutton9)
On_bn_clicked (idc_button11, & C calculator DLG: onbnclickedbutton11)
On_bn_clicked (idc_button12, & C calculator DLG: onbnclickedbutton12)
On_bn_clicked (idc_button13, & C calculator DLG: onbnclickedbutton13)
On_bn_clicked (idc_button14, & C calculator DLG: onbnclickedbutton14)
On_bn_clicked (idc_button15, & C calculator DLG: onbnclickedbutton15)
On_bn_clicked (idc_button16, & C calculator DLG: onbnclickedbutton16)
On_bn_clicked (idc_button17, & C calculator DLG: onbnclickedbutton17)
On_bn_clicked (idc_button18, & C calculator DLG: onbnclickedbutton18)
On_bn_clicked (idc_button19, & C calculator DLG: onbnclickedbutton19)
End_message_map ()
// C calculator DLG Message Processing Program
Bool C calculator DLG: oninitdialog ()
{
Cdialog: oninitdialog ();
// Add the "about..." menu to the system menu.
// Idm_aboutbox must be in the range of system commands.
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 in this dialog box. When the application main window is not a dialog box, the framework will automatically
// Perform this operation
Seticon (m_hicon, true); // you can specify a large icon.
Seticon (m_hicon, false); // you can specify a small icon.
// Todo: add additional initialization code here
Return true; // return true unless focus is set to the control.
}
Void C calculator DLG: onsyscommand (uint NID, lparam)
{
If (NID & 0xfff0) = idm_aboutbox)
{
Caboutdlg dlgabout;
Dlgabout. domodal ();
}
Else
{
Cdialog: onsyscommand (NID, lparam );
}
}
// If you add the minimization button to the dialog box, the following code is required:
// To draw the icon. For MFC applications that use document/view models,
// This will be automatically completed by the framework.
Void C calculator DLG: onpaint ()
{
If (isiconic ())
{
Cpaintdc DC (this); // device context used for plotting
Sendmessage (wm_iconerasebkgnd, reinterpret_cast <wparam> (DC. getsafehdc (), 0 );
// Center the icon in the work 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 ();
}
}
// When you drag the minimize window, the system calls this function to display the cursor.
//
Hcursor C calculator DLG: onquerydragicon ()
{
Return static_cast }
Void C calculator DLG: onbnclickedbutton1 ()
{
Updatedata (true );
TEXT = text + '1 ';
Updatedata (false );
// Todo: add the control notification handler code here
}
Void C calculator DLG: onbnclickedok ()
{
// Todo: add the control notification handler code here
Onok ();
}
Void C calculator DLG: onbnclickedbutton2 ()
{
Updatedata (true );
TEXT = text + '2 ';
Updatedata (false );
// Todo: add the control notification handler code here
}
Void C calculator DLG: onbnclickedbutton3 ()
{
Updatedata (true );
TEXT = text + '3 ';
Updatedata (false );
// Todo: add the control notification handler code here
}
Void C calculator DLG: onbnclickedbutton4 ()
{
Updatedata (true );
TEXT = text + '4 ';
Updatedata (false );
// Todo: add the control notification handler code here
}
Void C calculator DLG: onbnclickedbutton10 ()
{
Updatedata (true );
TEXT = text + '0 ';
Updatedata (false );
// Todo: add the control notification handler code here
}
Void C calculator DLG: onbnclickedbutton5 ()
{
Updatedata (true );
TEXT = text + '5 ';
Updatedata (false );
// Todo: add the control notification handler code here
}
Void C calculator DLG: onbnclickedbutton6 ()
{
Updatedata (true );
TEXT = text + '6 ';
Updatedata (false );
// Todo: add the control notification handler code here
}
Void C calculator DLG: onbnclickedbutton7 ()
{
Updatedata (true );
TEXT = text + '7 ';
Updatedata (false );
// Todo: add the control notification handler code here
}
Void C calculator DLG: onbnclickedbutton8 ()
{
Updatedata (true );
TEXT = text + '8 ';
Updatedata (false );
// Todo: add the control notification handler code here
}
Void C calculator DLG: onbnclickedbutton9 ()
{
Updatedata (true );
TEXT = text + '9 ';
Updatedata (false );
// Todo: add the control notification handler code here
}
Void C calculator DLG: onbnclickedbutton11 ()
{
Updatedata (true );
A = text;
B = "+ ";
TEXT = "";
Updatedata (false );
// Todo: add the control notification handler code here
}
Void C calculator DLG: onbnclickedbutton12 ()
{
Updatedata (true );
A = text;
B = "-";
TEXT = "";
Updatedata (false );
// Todo: add the control notification handler code here
}
Void C calculator DLG: onbnclickedbutton13 ()
{
Updatedata (true );
A = text;
B = "*";
TEXT = "";
Updatedata (false );
// Todo: add the control notification handler code here
}
Void C calculator DLG: onbnclickedbutton14 ()
{
Updatedata (true );
A = text;
B = "/";
TEXT = "";
Updatedata (false );
// Todo: add the control notification handler code here
}
Void C calculator DLG: onbnclickedbutton15 ()
{
// Todo: add the control notification handler code here
Updatedata (true );
C = text;
If (B = "+ ")
{
D = _ tstof (lpctstr ());
E = _ tstof (lpctstr (c ));
Dd = d + E;
Text. Format (_ T ("% F"), DD );
// Text. Delete (text. getlength ()-6, text. getlength ());
Text. Delete (text. getlength ()-4, text. getlength ()-6 );
}
Else if (B = "-")
{
D = _ tstof (lpctstr ());
E = _ tstof (lpctstr (c ));
Dd = D-E;
Text. Format (_ T ("% F"), DD );
Text. Delete (text. getlength ()-4, text. getlength ()-6 );
}
Else if (B = "*")
{
D = _ tstof (lpctstr ());
E = _ tstof (lpctstr (c ));
Dd = D * E;
Text. Format (_ T ("% F"), DD );
Text. Delete (text. getlength ()-4, text. getlength ()-6 );
}
Else if (B = "/")
{
D = _ tstof (lpctstr ());
E = _ tstof (lpctstr (c ));
Dd = D/E;
Text. Format (_ T ("% F"), DD );
Text. Delete (text. getlength ()-4, text. getlength ()-6 );
}
Updatedata (false );
}
Void C calculator DLG: onbnclickedbutton16 ()
{
Updatedata (true );
TEXT = "";
A = "";
C = "";
Updatedata (false );
// Todo: add the control notification handler code here
}
Void C calculator DLG: onbnclickedbutton17 ()
{
// Todo: add the control notification handler code here
Updatedata (true );
TEXT = text + '.';
Updatedata (false );
}
Void C calculator DLG: onbnclickedbutton18 ()
{
Updatedata (true );
Text. Delete (text. getlength ()-1, 1 );
Updatedata (false );
// Todo: add the control notification handler code here
}
Void C calculator DLG: onbnclickedbutton19 ()
{
Updatedata (true );
Static int I =-1;
I ++;
Do {
If (I = 0)
{
TEXT = '-' + text;
Break;
}
Else if (I = 1)
{
Text. Delete (0, 1 );
Break;
}
I = I % 2;
} While (true );
If (text = "")
{
TEXT = '-' + text;
}
Updatedata (false );
// Todo: add the control notification handler code here
}