《MFC计算器主要代码(C++).docx》由会员分享,可在线阅读,更多相关《MFC计算器主要代码(C++).docx(20页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、/ Calculator_17483Dlg.cpp : implementation file/#include stdafx.h#include Calculator_17483.h#include Calculator_17483Dlg.h#include afxdialogex.h#ifdef _DEBUG#define new DEBUG_NEW#endif/ CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialogExpublic:CAboutDlg();/ Dialog Dataenum IDD = ID
2、D_ABOUTBOX ;protected:virtual void DoDataExchange(CDataExchange* pDX); / DDX/DDV support/ Implementationprotected:DECLARE_MESSAGE_MAP();CAboutDlg:CAboutDlg() : CDialogEx(CAboutDlg:IDD)void CAboutDlg:DoDataExchange(CDataExchange* pDX)CDialogEx:DoDataExchange(pDX);BEGIN_MESSAGE_MAP(CAboutDlg, CDialogE
3、x)END_MESSAGE_MAP()/ CCalculator_17483Dlg dialogCCalculator_17483Dlg:CCalculator_17483Dlg(CWnd* pParent /*=NULL*/): CDialogEx(CCalculator_17483Dlg:IDD, pParent)num1=0;num2=0;operation=0;point=0;nump_1=0;nump_2=0;m_hIcon = AfxGetApp()-LoadIcon(IDR_MAINFRAME);void CCalculator_17483Dlg:DoDataExchange(C
4、DataExchange* pDX)CDialogEx:DoDataExchange(pDX);BEGIN_MESSAGE_MAP(CCalculator_17483Dlg, CDialogEx)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON1, &CCalculator_17483Dlg:OnBnClickedButton1)ON_BN_CLICKED(IDC_BUTTON2, &CCalculator_17483Dlg:OnBnClickedButton2)ON_BN_CLICKED(
5、IDC_BUTTON3, &CCalculator_17483Dlg:OnBnClickedButton3)ON_BN_CLICKED(IDC_BUTTON4, &CCalculator_17483Dlg:OnBnClickedButton4)ON_BN_CLICKED(IDC_BUTTON5, &CCalculator_17483Dlg:OnBnClickedButton5)ON_BN_CLICKED(IDC_BUTTON6, &CCalculator_17483Dlg:OnBnClickedButton6)ON_BN_CLICKED(IDC_BUTTON7, &CCalculator_17
6、483Dlg:OnBnClickedButton7)ON_BN_CLICKED(IDC_BUTTON8, &CCalculator_17483Dlg:OnBnClickedButton8)ON_BN_CLICKED(IDC_BUTTON9, &CCalculator_17483Dlg:OnBnClickedButton9)ON_BN_CLICKED(IDC_BUTTON10, &CCalculator_17483Dlg:OnBnClickedButton10)ON_BN_CLICKED(IDC_BUTTON11, &CCalculator_17483Dlg:OnBnClickedButton1
7、1)ON_BN_CLICKED(IDC_BUTTON12, &CCalculator_17483Dlg:OnBnClickedButton12)ON_BN_CLICKED(IDC_BUTTON13, &CCalculator_17483Dlg:OnBnClickedButton13)ON_BN_CLICKED(IDC_BUTTON14, &CCalculator_17483Dlg:OnBnClickedButton14)ON_BN_CLICKED(IDC_BUTTON15, &CCalculator_17483Dlg:OnBnClickedButton15)ON_BN_CLICKED(IDC_
8、BUTTON16, &CCalculator_17483Dlg:OnBnClickedButton16)ON_BN_CLICKED(IDC_BUTTON17, &CCalculator_17483Dlg:OnBnClickedButton17)END_MESSAGE_MAP()/ CCalculator_17483Dlg message handlersBOOL CCalculator_17483Dlg:OnInitDialog()CDialogEx:OnInitDialog();/ Add About. menu item to system menu./ IDM_ABOUTBOX must
9、 be in the system command range.ASSERT(IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX AppendMenu(MF_SEPARATOR);pSysMenu-AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);/ Set the icon for this dialog. The framework does this automatically/ when the applications main window is not a dial
10、ogSetIcon(m_hIcon, TRUE);/ Set big iconSetIcon(m_hIcon, FALSE);/ Set small icon/ TODO: Add extra initialization herereturn TRUE; / return TRUE unless you set the focus to a controlvoid CCalculator_17483Dlg:OnSysCommand(UINT nID, LPARAM lParam)if (nID & 0xFFF0) = IDM_ABOUTBOX)CAboutDlg dlgAbout;dlgAb
11、out.DoModal();elseCDialogEx: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 CCalculator_17483Dlg:OnPaint()if (IsIconi
12、c()CPaintDC dc(this); / device context for paintingSendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc(), 0);/ Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1
13、) / 2;int y = (rect.Height() - cyIcon + 1) / 2;/ Draw the icondc.DrawIcon(x, y, m_hIcon);elseCDialogEx:OnPaint();/ The system calls this function to obtain the cursor to display while the user drags/ the minimized window.HCURSOR CCalculator_17483Dlg:OnQueryDragIcon()return static_cast(m_hIcon);void
14、CCalculator_17483Dlg:OnBnClickedButton13()/0if(operation=0)num1=num1*10+0;string_num1.Format(_T(%d),num1);SetDlgItemTextW(IDC_EDIT1,string_num1);elsenum2=num2*10+0;string_num2.Format(_T(%d),num2);SetDlgItemTextW(IDC_EDIT2,string_num2);/ TODO: Add your control notification handler code here/0void CCa
15、lculator_17483Dlg:OnBnClickedButton9()/1if(point=0)/无T小?数y点?if(operation=0) /num1num1=num1*10+1;/string_num1.Format(_T(%d),num1);string_num1+=1;SetDlgItemTextW(IDC_EDIT1,string_num1);elsenum2=num2*10+1;/string_num2.Format(_T(%d),num2);string_num2+=1;SetDlgItemTextW(IDC_EDIT2,string_num2);elseif(oper
16、ation=0)float i;nump_1=num1;/for(i=0.7;nump_1(int)nump_1;nump_1*10,i/10)/nump_1=nump_1+i;string_num1+=1;/string_num1.Format(_T(%f),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);else/num2=num2*10+7;/string_num2.Format(_T(%d),num2);string_num2+=1;SetDlgItemTextW(IDC_EDIT2,string_num2);/ TODO: Add you
17、r control notification handler code here/1void CCalculator_17483Dlg:OnBnClickedButton10()/2if(point=0)/无T小?数y点?if(operation=0) /num1num1=num1*10+2;/string_num1.Format(_T(%d),num1);string_num1+=2;SetDlgItemTextW(IDC_EDIT1,string_num1);elsenum2=num2*10+2;/string_num2.Format(_T(%d),num2);string_num2+=2
18、;SetDlgItemTextW(IDC_EDIT2,string_num2);elseif(operation=0)float i;nump_1=num1;/for(i=0.7;nump_1(int)nump_1;nump_1*10,i/10)/nump_1=nump_1+i;string_num1+=2;/string_num1.Format(_T(%f),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);else/num2=num2*10+7;/string_num2.Format(_T(%d),num2);string_num2+=2;Set
19、DlgItemTextW(IDC_EDIT2,string_num2);/ TODO: Add your control notification handler code here/2void CCalculator_17483Dlg:OnBnClickedButton11()/3if(point=0)/无T小?数y点?if(operation=0) /num1num1=num1*10+3;/string_num1.Format(_T(%d),num1);string_num1+=3;SetDlgItemTextW(IDC_EDIT1,string_num1);elsenum2=num2*1
20、0+3;/string_num2.Format(_T(%d),num2);string_num2+=3;SetDlgItemTextW(IDC_EDIT2,string_num2);elseif(operation=0)float i;nump_1=num1;/for(i=0.7;nump_1(int)nump_1;nump_1*10,i/10)/nump_1=nump_1+i;string_num1+=3;/string_num1.Format(_T(%f),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);else/num2=num2*10+7;
21、/string_num2.Format(_T(%d),num2);string_num2+=3;SetDlgItemTextW(IDC_EDIT2,string_num2);/ TODO: Add your control notification handler code here/3void CCalculator_17483Dlg:OnBnClickedButton5()/4if(point=0)/无T小?数y点?if(operation=0) /num1num1=num1*10+4;/string_num1.Format(_T(%d),num1);string_num1+=4;SetD
22、lgItemTextW(IDC_EDIT1,string_num1);elsenum2=num2*10+4;/string_num2.Format(_T(%d),num2);string_num2+=4;SetDlgItemTextW(IDC_EDIT2,string_num2);elseif(operation=0)float i;nump_1=num1;/for(i=0.7;nump_1(int)nump_1;nump_1*10,i/10)/nump_1=nump_1+i;string_num1+=4;/string_num1.Format(_T(%f),nump_1);SetDlgIte
23、mTextW(IDC_EDIT1,string_num1);else/num2=num2*10+7;/string_num2.Format(_T(%d),num2);string_num2+=4;SetDlgItemTextW(IDC_EDIT2,string_num2);/ TODO: Add your control notification handler code here/4void CCalculator_17483Dlg:OnBnClickedButton6()/5if(point=0)/无T小?数y点?if(operation=0) /num1num1=num1*10+5;/s
24、tring_num1.Format(_T(%d),num1);string_num1+=5;SetDlgItemTextW(IDC_EDIT1,string_num1);elsenum2=num2*10+5;/string_num2.Format(_T(%d),num2);string_num2+=5;SetDlgItemTextW(IDC_EDIT2,string_num2);elseif(operation=0)float i;nump_1=num1;/for(i=0.7;nump_1(int)nump_1;nump_1*10,i/10)/nump_1=nump_1+i;string_nu
25、m1+=5;/string_num1.Format(_T(%f),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);else/num2=num2*10+7;/string_num2.Format(_T(%d),num2);string_num2+=5;SetDlgItemTextW(IDC_EDIT2,string_num2);/ TODO: Add your control notification handler code here/5void CCalculator_17483Dlg:OnBnClickedButton7()/6if(point
26、=0)/无T小?数y点?if(operation=0) /num1num1=num1*10+6;/string_num1.Format(_T(%d),num1);string_num1+=6;SetDlgItemTextW(IDC_EDIT1,string_num1);elsenum2=num2*10+6;/string_num2.Format(_T(%d),num2);string_num2+=6;SetDlgItemTextW(IDC_EDIT2,string_num2);elseif(operation=0)float i;nump_1=num1;/for(i=0.7;nump_1(in
27、t)nump_1;nump_1*10,i/10)/nump_1=nump_1+i;string_num1+=6;/string_num1.Format(_T(%f),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);else/num2=num2*10+7;/string_num2.Format(_T(%d),num2);string_num2+=6;SetDlgItemTextW(IDC_EDIT2,string_num2);/ TODO: Add your control notification handler code here/6void C
28、Calculator_17483Dlg:OnBnClickedButton1()/7if(point=0)/无T小?数y点?if(operation=0) /num1num1=num1*10+7;/string_num1.Format(_T(%d),num1);string_num1+=7;SetDlgItemTextW(IDC_EDIT1,string_num1);elsenum2=num2*10+7;/string_num2.Format(_T(%d),num2);string_num2+=7;SetDlgItemTextW(IDC_EDIT2,string_num2);elseif(op
29、eration=0)float i;nump_1=num1;/for(i=0.7;nump_1(int)nump_1;nump_1*10,i/10)/nump_1=nump_1+i;string_num1+=7;/string_num1.Format(_T(%f),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);else/num2=num2*10+7;/string_num2.Format(_T(%d),num2);string_num2+=7;SetDlgItemTextW(IDC_EDIT2,string_num2);/ TODO: Add y
30、our control notification handler code here/7void CCalculator_17483Dlg:OnBnClickedButton2()/8if(point=0)/无T小?数y点?if(operation=0) /num1num1=num1*10+8;/string_num1.Format(_T(%d),num1);string_num1+=8;SetDlgItemTextW(IDC_EDIT1,string_num1);elsenum2=num2*10+8;/string_num2.Format(_T(%d),num2);string_num2+=
31、8;SetDlgItemTextW(IDC_EDIT2,string_num2);elseif(operation=0)float i;nump_1=num1;/for(i=0.7;nump_1(int)nump_1;nump_1*10,i/10)/nump_1=nump_1+i;string_num1+=8;/string_num1.Format(_T(%f),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);else/num2=num2*10+7;/string_num2.Format(_T(%d),num2);string_num2+=8;Se
32、tDlgItemTextW(IDC_EDIT2,string_num2);/ TODO: Add your control notification handler code here/8void CCalculator_17483Dlg:OnBnClickedButton3()/9if(point=0)/无T小?数y点?if(operation=0) /num1num1=num1*10+9;/string_num1.Format(_T(%d),num1);string_num1+=9;SetDlgItemTextW(IDC_EDIT1,string_num1);elsenum2=num2*1
33、0+9;/string_num2.Format(_T(%d),num2);string_num2+=9;SetDlgItemTextW(IDC_EDIT2,string_num2);elseif(operation=0)float i;nump_1=num1;/for(i=0.7;nump_1(int)nump_1;nump_1*10,i/10)/nump_1=nump_1+i;string_num1+=9;/string_num1.Format(_T(%f),nump_1);SetDlgItemTextW(IDC_EDIT1,string_num1);else/num2=num2*10+7;
34、/string_num2.Format(_T(%d),num2);string_num2+=9;SetDlgItemTextW(IDC_EDIT2,string_num2);/ TODO: Add your control notification handler code here/9void CCalculator_17483Dlg:OnBnClickedButton4()/+if(point=0)if(operation=0)SetDlgItemTextW(IDC_EDIT4,_T(+);operation=1;elseswitch(operation)case 1:num1=num1+
35、num2;break;case 2:num1=num1-num2;break;case 3:num1=num1*num2;break;case 4:num1=num1/num2;break;num2=0;operation=1;string_num1.Format(_T(%d),num1);SetDlgItemTextW(IDC_EDIT1,string_num1);string_num2.Format(_T(%d),num2);SetDlgItemTextW(IDC_EDIT2,_T();SetDlgItemTextW(IDC_EDIT4,_T(+);elseoperation=1;poin
36、t=0;/char*p1=(LPSTR)(LPCTSTR)string_num1;/char*p2=(LPSTR)(LPCTSTR)string_num2;SetDlgItemTextW(IDC_EDIT1,string_num1);SetDlgItemTextW(IDC_EDIT2,string_num2);SetDlgItemTextW(IDC_EDIT2,_T();SetDlgItemTextW(IDC_EDIT4,_T(+);/int len=string_num1.GetLength();/int nbyte=WideCharToMultiByte(CP_ACP,0,string_n
37、um1,len,NULL,0,NULL,NULL);/char *p=new charnbyte+1;/memset(p,0,len+1);/WideCharToMultiByte(CP_OEMCP,0,string_num1,len,p,nbyte,NULL,NULL);/pnbyte=0;/nump_1=atof(p);/ TODO: Add your control notification handler code here/+void CCalculator_17483Dlg:OnBnClickedButton8()/-if(point=0)if(operation=0)SetDlg
38、ItemTextW(IDC_EDIT4,_T(-);operation=2;elseswitch(operation)case 1:num1=num1+num2;break;case 2:num1=num1-num2;break;case 3:num1=num1*num2;break;case 4:num1=num1/num2;break;num2=0;operation=2;string_num1.Format(_T(%d),num1);SetDlgItemTextW(IDC_EDIT1,string_num1);string_num2.Format(_T(%d),num2);SetDlgI
39、temTextW(IDC_EDIT2,_T();SetDlgItemTextW(IDC_EDIT4,_T(-);elseoperation=2;point=0;/char*p1=(LPSTR)(LPCTSTR)string_num1;/char*p2=(LPSTR)(LPCTSTR)string_num2;SetDlgItemTextW(IDC_EDIT1,string_num1);SetDlgItemTextW(IDC_EDIT2,string_num2);SetDlgItemTextW(IDC_EDIT2,_T();SetDlgItemTextW(IDC_EDIT4,_T(-);/ TODO: Add