您的当前位置:首页正文

四则运算计算器源代码(C++)

2021-10-18 来源:客趣旅游网
四则运算计算器源代码(C++)

// calculatorsDlg.h : header file //

#if !defined(AFX_CALCULATORSDLG_H__3146ED92_B203_4223_A987_FF2D22CCBD42__INCLUDED_) #define AFX_CALCULATORSDLG_H__3146ED92_B203_4223_A987_FF2D22CCBD42__INCLUDED_

#if _MSC_VER > 1000 #pragma once

#endif // _MSC_VER > 1000

///////////////////////////////////////////////////////////////////////////// // CCalculatorsDlg dialog

class CCalculatorsDlg : public CDialog {

// Construction public:

// Dialog Data

// Implementation

// ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CCalculatorsDlg) protected:

virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL

//{{AFX_DATA(CCalculatorsDlg)

enum { IDD = IDD_CALCULATORS_DIALOG }; //记?录?输?入?的?字?符? //}}AFX_DATA

CCalculatorsDlg(CWnd* pParent = NULL); // standard constructor

// CString m_shuru;

protected:

// Generated message map functions //{{AFX_MSG(CCalculatorsDlg) virtual BOOL OnInitDialog();

afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint();

afx_msg HCURSOR OnQueryDragIcon(); afx_msg void OnButton0(); afx_msg void OnButton1(); afx_msg void OnButton2(); afx_msg void OnButton3(); afx_msg void OnButton4(); afx_msg void OnButton5(); afx_msg void OnButton6(); afx_msg void OnButton7(); afx_msg void OnButton8(); afx_msg void OnButton9(); afx_msg void OnBUTTONAdd(); afx_msg void OnBUTTONSub(); afx_msg void OnBUTTONMul(); afx_msg void OnBUTTONDiv(); afx_msg void OnBUTTONResult(); afx_msg void OnEDIT(); afx_msg void OnBUTTONClear(); afx_msg void OnBUTTONDelete(); afx_msg void OnBUTTONpoint(); void Result(); //}}AFX_MSG

DECLARE_MESSAGE_MAP() char buffer[100];

int m_bit;//记?录?运?算?符?

bool ele;//用?于?判D断?小?数簓点?的?输?入? int d;//记?录?小?数簓点?后ó的?位?数簓 //记?录?输?入?数簓据Y

//记?录?每?个?符?号?的?运?算?结á果?

HICON m_hIcon; ;

private:

double m_begin;// double m_end;

};

double m_num;

bool ele2;//用?于?判D断?程ì序ò是?否?有瓺错洙?误ó bool ele3;//用?于?判D断?正y负o数簓

CString m_shuru;

afx_msg void OnBnClickedButtonexit(); afx_msg void OnBnClickedButtonsign();

public:

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_CALCULATORSDLG_H__3146ED92_B203_4223_A987_FF2D22CCBD42__INCLUDED_)

// calculatorsDlg.cpp : implementation file //

#include \"stdafx.h\" #include \"calculators.h\" #include \"calculatorsDlg.h\" #include \"math.h\" #include \"string.h\" #include \"stdio.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:

// Dialog Data

CAboutDlg();

//{{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: };

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { }

void CAboutDlg::DoDataExchange(CDataExchange* pDX) { }

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)

///////////////////////////////////////////////////////////////////////////// // CCalculatorsDlg dialog

CCalculatorsDlg::CCalculatorsDlg(CWnd* pParent /*=NULL*/)

: CDialog(CCalculatorsDlg::IDD, pParent) //{{AFX_MSG_MAP(CAboutDlg)

// No message handlers //}}AFX_MSG_MAP

CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP

//{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT //{{AFX_MSG(CAboutDlg) //}}AFX_MSG

DECLARE_MESSAGE_MAP()

END_MESSAGE_MAP()

{ }

void CCalculatorsDlg::DoDataExchange(CDataExchange* pDX) { }

BEGIN_MESSAGE_MAP(CCalculatorsDlg, CDialog)

//{{AFX_MSG_MAP(CCalculatorsDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT()

ON_WM_QUERYDRAGICON()

ON_BN_CLICKED(IDC_BUTTON0, OnButton0) ON_BN_CLICKED(IDC_BUTTON1, OnButton1) ON_BN_CLICKED(IDC_BUTTON2, OnButton2) ON_BN_CLICKED(IDC_BUTTON3, OnButton3) ON_BN_CLICKED(IDC_BUTTON4, OnButton4) ON_BN_CLICKED(IDC_BUTTON5, OnButton5) ON_BN_CLICKED(IDC_BUTTON6, OnButton6) ON_BN_CLICKED(IDC_BUTTON7, OnButton7) ON_BN_CLICKED(IDC_BUTTON8, OnButton8) CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CCalculatorsDlg) // DDX_Text(pDX, IDC_Form1, m_shuru); //}}AFX_DATA_MAP

// DDX_Text(pDX, IDC_Form1, m_shuru); // DDV_MaxChars(pDX, m_shuru, 100); // DDX_Text(pDX, IDC_Form1, m_shuru); // DDX_Text(pDX, IDC_Form1, m_shuru); // DDX_Text(pDX, IDC_Form1, m_shuru); DDX_Text(pDX, IDC_Form1, m_shuru); //变?量?的?初?始?化ˉ

m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); ele=false;//表括?示?开a始?是?没?有瓺小?数簓点? d=0; m_bit=0; m_num=0; m_end=0;

m_shuru = _T(\"\"); ele2=true; ele3=true;

ON_BN_CLICKED(IDC_BUTTON9, OnButton9) ON_BN_CLICKED(IDC_BUTTONAdd, OnBUTTONAdd) ON_BN_CLICKED(IDC_BUTTONSub, OnBUTTONSub) ON_BN_CLICKED(IDC_BUTTONMul, OnBUTTONMul) ON_BN_CLICKED(IDC_BUTTONDiv, OnBUTTONDiv) ON_BN_CLICKED(IDC_BUTTONResult, OnBUTTONResult) ON_EN_CHANGE(IDC_Form1, OnEDIT)

ON_BN_CLICKED(IDC_BUTTONClear, OnBUTTONClear) ON_BN_CLICKED(IDC_BUTTONDelete, OnBUTTONDelete) ON_BN_CLICKED(IDC_BUTTONpoint, OnBUTTONpoint) //}}AFX_MSG_MAP

ON_BN_CLICKED(IDC_BUTTONExit, &CCalculatorsDlg::OnBnClickedButtonexit) ON_BN_CLICKED(IDC_BUTTONSign, &CCalculatorsDlg::OnBnClickedButtonsign)

END_MESSAGE_MAP()

///////////////////////////////////////////////////////////////////////////// // CCalculatorsDlg message handlers

BOOL CCalculatorsDlg::OnInitDialog() {

// Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog 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);

// 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); CDialog::OnInitDialog();

}

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 CCalculatorsDlg::OnSysCommand(UINT nID, LPARAM 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 CCalculatorsDlg::OnPaint() {

// 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;

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); if (IsIconic()) {

CPaintDC dc(this); // device context for painting if ((nID & 0xFFF0) == IDM_ABOUTBOX)//关?联对?话°框ò { } else { }

CDialog::OnSysCommand(nID, lParam); CAboutDlg dlgAbout; dlgAbout.DoModal();

}

}

// 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 CCalculatorsDlg::OnQueryDragIcon() { }

void CCalculatorsDlg::Result()//用?来ぁ?处鋦理え?按恪?下?符?号?键ü时骸?的?处鋦理え?函ˉ数簓 {

UpdateData(FALSE); //数簓据Y刷¢新? switch(m_bit) {

default: //当獭?只?有瓺一?个?输?入?值μ时骸?,?按恪?下?符?号?键ü,?不?做?计?

m_end=m_num; break;

m_end=m_end+m_num; break;

m_end=m_end-m_num; break;

m_end=m_end*m_num; break;

if(m_num==0) //当獭?除y数簓为a0时骸?,?显?示?“°error!”±,?并¢结á束?计?算? {

m_shuru=\"error!\"; ele2=false;

UpdateData(FALSE); //数簓据Y刷¢新?

return (HCURSOR) m_hIcon;

算?处鋦理え?,?等台?待鋣第台?二t个?输?入?值μ键ü入?

case 1: //加ó法ぁ?计?算?

case 2: //减?法ぁ?计?算?

case 3: //乘?法ぁ?计?算?

case 4: //除y法ぁ?计?算?

}

}

}

return;

else { } break;

m_end=m_end/m_num;

m_num=0; d=0; ele=false; ele3=true;

void CCalculatorsDlg::OnButton0() //按恪?下?0键ü时骸?的?处鋦理え?函ˉ数簓 { }

void CCalculatorsDlg::OnButton1() //按恪?下?1键ü时骸?的?处鋦理え?函ˉ数簓 {

m_shuru=m_shuru+\"1\"; if(ele==false) {

if(ele3==true) { }

m_num=m_num*10+1;

UpdateData(FALSE); //数簓据Y刷¢新?

m_shuru=m_shuru+\"0\"; if(ele==false) { } else { }

d++;

if(ele3==true) { }

m_num=m_num*10;

}

}

else

m_num=m_num*10-1;

else { }

UpdateData(FALSE); //数簓据Y刷¢新?

m_begin=1; d++;

for(int i=0;iif(ele3==true) { } else

m_num=m_num-m_begin; m_num=m_begin+m_num; m_begin=m_begin/10;

void CCalculatorsDlg::OnButton2() //按恪?下?2键ü时骸?的?处鋦理え?函ˉ数簓 {

m_shuru=m_shuru+\"2\"; if(ele==false) { } else {

m_begin=2; d++;

for(int i=0;im_begin=m_begin/10; if(ele3==true) { } else

m_num=m_num*10-2; m_num=m_num*10+2;

}

}

}

if(ele3==true) { } else

m_num=m_num-m_begin; m_num=m_begin+m_num;

UpdateData(FALSE); //数簓据Y刷¢新?

void CCalculatorsDlg::OnButton3() //按恪?下?3键ü时骸?的?处鋦理え?函ˉ数簓 { }

m_shuru=m_shuru+\"3\"; if(ele==false) { } else { }

UpdateData(FALSE); //数簓据Y刷¢新?

m_begin=3; d++;

for(int i=0;iif(ele3==true) { } else

m_num=m_num-m_begin; m_num=m_begin+m_num;

m_begin=m_begin/10; if(ele3==true) { } else

m_num=m_num*10-3; m_num=m_num*10+3;

void CCalculatorsDlg::OnButton4() //按恪?下?4键ü时骸?的?处鋦理え?函ˉ数簓 { }

void CCalculatorsDlg::OnButton5() //按恪?下?5键ü时骸?的?处鋦理え?函ˉ数簓 {

m_shuru=m_shuru+\"5\"; if(ele==false) {

if(ele3==true) { }

m_num=m_num*10+5;

m_shuru=m_shuru+\"4\"; if(ele==false) { } else { }

UpdateData(FALSE); //数簓据Y刷¢新?

m_begin=4; d++;

for(int i=0;iif(ele3==true) { } else

m_num=m_num-m_begin; m_num=m_begin+m_num;

m_begin=m_begin/10; if(ele3==true) { } else

m_num=m_num*10-4; m_num=m_num*10+4;

}

}

else

m_num=m_num*10-5;

else { }

UpdateData(FALSE); //数簓据Y刷¢新?

m_begin=5; d++;

for(int i=0;iif(ele3==true) { } else

m_num=m_num-m_begin; m_num=m_begin+m_num;

m_begin=m_begin/10;

void CCalculatorsDlg::OnButton6() //按恪?下?6键ü时骸?的?处鋦理え?函ˉ数簓 {

m_shuru=m_shuru+\"6\"; if(ele==false) { } else {

m_begin=6; d++;

for(int i=0;iif(ele3==true) { } else

m_num=m_num*10-6; m_num=m_num*10+6;

}

}

}

m_begin=m_begin/10;

if(ele3==true) { } else

m_num=m_num-m_begin; m_num=m_begin+m_num;

UpdateData(FALSE); //数簓据Y刷¢新?

void CCalculatorsDlg::OnButton7() //按恪?下?7键ü时骸?的?处鋦理え?函ˉ数簓 {

m_shuru=m_shuru+\"7\"; if(ele==false) { } else { }

UpdateData(FALSE); //数簓据Y刷¢新?

m_begin=7; d++;

for(int i=0;iif(ele3==true) { } else

m_num=m_num-m_begin; m_num=m_begin+m_num;

m_begin=m_begin/10; if(ele3==true) { } else

m_num=m_num*10-7; m_num=m_num*10+7;

}

void CCalculatorsDlg::OnButton8() //按恪?下?8键ü时骸?的?处鋦理え?函ˉ数簓 { }

void CCalculatorsDlg::OnButton9() //按恪?下?9键ü时骸?的?处鋦理え?函ˉ数簓 {

m_shuru=m_shuru+\"9\"; if(ele==false) {

if(ele3==true) {

m_num=m_num*10+9;

m_shuru=m_shuru+\"8\"; if(ele==false) { } else { }

UpdateData(FALSE); //数簓据Y刷¢新?

m_begin=8; d++;

for(int i=0;iif(ele3==true) { } else

m_num=m_num-m_begin; m_num=m_begin+m_num;

m_begin=m_begin/10; if(ele3==true) { } else

m_num=m_num*10-8; m_num=m_num*10+8;

}

}

} else

m_num=m_num*10-9;

else { }

UpdateData(FALSE); //数簓据Y刷¢新?

m_begin=9; d++;

for(int i=0;iif(ele3==true) { } else

m_num=m_num-m_begin; m_num=m_begin+m_num;

m_begin=m_begin/10;

void CCalculatorsDlg::OnBUTTONAdd() //按恪?下?“°+”±号?键ü的?处鋦理え?函ˉ数簓 { }

void CCalculatorsDlg::OnBUTTONSub() //按恪?下?“°-”±号?键ü的?处鋦理え?函ˉ数簓 { }

void CCalculatorsDlg::OnBUTTONMul() //按恪?下?“°*”±号?键ü的?处鋦理え?函ˉ数簓 {

m_shuru=m_shuru+\"-\"; Result(); m_bit=2;

m_shuru=m_shuru+\"+\"; Result(); m_bit=1;

}

m_shuru=m_shuru+\"*\"; Result(); m_bit=3;

void CCalculatorsDlg::OnBUTTONDiv() //按恪?下?“°/”±号?键ü的?处鋦理え?函ˉ数簓 { }

void CCalculatorsDlg::OnBUTTONResult() //按恪?下?“°=”±号?键ü的?处鋦理え?函ˉ数簓 { }

void CCalculatorsDlg::OnEDIT() { }

void CCalculatorsDlg::OnBUTTONClear() {

// TODO: If this is a RICHEDIT control, the control will not

// send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask.

// TODO: Add your control notification handler code here }

Result(); if(ele2==true) {

sprintf_s(buffer,\"%10.12g\",m_end); m_shuru=buffer;

UpdateData(FALSE); //数簓据Y刷¢新?,显?示?目?前°计?算?结á果? m_bit=0; m_shuru=m_shuru+\"/\"; Result(); m_bit=4;

ele=false;//消?除y小?数簓点?按恪?下?标括?记? m_num=0;

m_shuru=\"\";//输?入?清?空? m_begin=0;//清?0 m_end=0;//清?0

m_bit=0;//当獭?前°操õ作痢?符?为a无T效§操õ作痢? d=0;

UpdateData(FALSE); //数簓据Y刷¢新? ele2=true; }

void CCalculatorsDlg::OnBUTTONDelete() {

if (ele==false ) //判D断?当獭?前°是?否?有瓺小?数簓点?,?如?果?没?有瓺 m_num=m_num/10-fmod(m_num/10,1); //去ǎ?掉?小?数簓位? else {

if (ele==true && d>0) //如?果?是?小?数簓 {

for (int j=0;jm_num=m_num*10; //移?动ˉ至á只?有瓺一?个?小?数簓位? }

m_num=m_num-fmod(m_num,1); //去ǎ?掉?小?数簓位?

for (int k=0;km_num=m_num/10; } } d--; }

int n=m_shuru.GetLength(); m_shuru.Delete(n-1,1);

if (d==0)ele=false;//如?果?全?部?清?空?了? ele2=true;

UpdateData(FALSE); //数簓据Y刷¢新? }

void CCalculatorsDlg::OnBUTTONpoint() {

if(ele==false)

}

{ } ele=true;

m_shuru=m_shuru+\".\";

UpdateData(FALSE);//数簓据Y刷¢新?

void CCalculatorsDlg::OnBnClickedButtonexit() { }

void CCalculatorsDlg::OnBnClickedButtonsign() { }

if(ele3==true) { } else { }

m_shuru=m_shuru+\"+\"; ele3=true;

m_shuru=m_shuru+\"-\"; ele3=false;

exit(0);//退?出?计?算?器ô程ì序ò

因篇幅问题不能全部显示,请点此查看更多更全内容