⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mycalculatordlg.cpp

📁 二叉树结构的定义
💻 CPP
字号:
// MyCalculatorDlg.cpp : implementation file
//

#include "stdafx.h"
#include <math.h>
#include "MyCalculator.h"
#include "MyCalculatorDlg.h"

#include "resource.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:
	CAboutDlg();

// Dialog Data
	//{{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:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

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

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

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyCalculatorDlg dialog

CMyCalculatorDlg::CMyCalculatorDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyCalculatorDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyCalculatorDlg)
	m_display = _T("0.0");
	m_first=0.0;
	m_second=0.0;
	m_coff=1.0;
	m_operator=_T("+");
    m_item=0;
	m_op=0.0;
	//m_third=0.0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyCalculatorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyCalculatorDlg)
	DDX_Text(pDX, IDC_DISPLAY, m_display);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyCalculatorDlg, CDialog)
	//{{AFX_MSG_MAP(CMyCalculatorDlg)
	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)
	ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
	ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
	ON_BN_CLICKED(IDC_BUTTON_BACKSPACE, OnButtonBackspace)
	ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnButtonClear)
	ON_BN_CLICKED(IDC_BUTTON_DIV, OnButtonDiv)
	ON_BN_CLICKED(IDC_BUTTON_EQUAL, OnButtonEqual)
	ON_BN_CLICKED(IDC_BUTTON_MUTIPLY, OnButtonMutiply)
	ON_BN_CLICKED(IDC_BUTTON_POINT, OnButtonPoint)
	ON_BN_CLICKED(IDC_BUTTON_RECIPROCAL, OnButtonReciprocal)
	ON_BN_CLICKED(IDC_BUTTON_SIGN, OnButtonSign)
	ON_BN_CLICKED(IDC_BUTTON_SQRT, OnButtonSqrt)
	ON_BN_CLICKED(IDC_BUTTON_MINUS, OnButtonMinus)
	ON_BN_CLICKED(IDC_BUTTON_MODE, OnButtonMode)
	ON_COMMAND(ID_MENU_about, OnMENUabout)
	ON_BN_CLICKED(IDC_BUTTON_CLEARNOW, OnButtonClearnow)
	ON_BN_CLICKED(IDC_BUTTON_M, OnButtonM)
	ON_BN_CLICKED(IDC_BUTTON_MS, OnButtonMs)
	ON_BN_CLICKED(IDC_BUTTON_MC, OnButtonMc)
	ON_BN_CLICKED(IDC_BUTTON_MR, OnButtonMr)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyCalculatorDlg message handlers

BOOL CMyCalculatorDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

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

	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 for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	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 CMyCalculatorDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::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 CMyCalculatorDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

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

		// 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 CMyCalculatorDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}



void CMyCalculatorDlg::OnButton0() 
{
	// TODO: Add your control notification handler code here
	if(m_item==1)
	{
		m_first=0;
		m_second=0;
		m_operator="+";
	}

	m_item=0;

   OnButton(0);
}

void CMyCalculatorDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	if(m_item==1)
	{
		m_first=0;
		m_second=0;
		m_operator="+";
	}
        m_item=0;
	OnButton(1);
}

void CMyCalculatorDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	if(m_item==1)
	{
		m_first=0;
		m_second=0;
		m_operator="+";
	}
		m_item=0;

	OnButton(2);
}

void CMyCalculatorDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	if(m_item==1)
	{
		m_first=0;
		m_second=0;
		m_operator="+";
	}
		m_item=0;

    OnButton(3);
}

void CMyCalculatorDlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
	if(m_item==1)
	{
		m_first=0;
		m_second=0;
		m_operator="+";
	}
		m_item=0;

	OnButton(4);
}

void CMyCalculatorDlg::OnButton5() 
{
	// TODO: Add your control notification handler code here
	if(m_item==1)
	{
		m_first=0;
		m_second=0;
		m_operator="+";
	}
		m_item=0;

	OnButton(5);
}

void CMyCalculatorDlg::OnButton6() 
{
	// TODO: Add your control notification handler code here
	if(m_item==1)
	{
		m_first=0;
		m_second=0;
		m_operator="+";
	}
		m_item=0;

     OnButton(6);
}

void CMyCalculatorDlg::OnButton7() 
{
	// TODO: Add your control notification handler code here
	if(m_item==1)
	{
		m_first=0;
		m_second=0;
		m_operator="+";
	}
		m_item=0;

	OnButton(7);
}

void CMyCalculatorDlg::OnButton8() 
{
	// TODO: Add your control notification handler code here
	if(m_item==1)
	{
		m_first=0;
		m_second=0;
		m_operator="+";
	}
		m_item=0;

	OnButton(8);
	
}

void CMyCalculatorDlg::OnButton9() 
{
	// TODO: Add your control notification handler code here
	if(m_item==1)
	{
		m_first=0;
		m_second=0;
		m_operator="+";
	}
		m_item=0;

	OnButton(9);
}

void CMyCalculatorDlg::OnButtonAdd() 
{
	// TODO: Add your control notification handler code here

	m_item=0;

    Calculate();
	m_operator="+";
	m_second=0.0;

}

void CMyCalculatorDlg::OnButtonBackspace() 
{
	// TODO: Add your control notification handler code here
 if(m_item==0)
 {
	   UpdateData(true);
  //m_display.Format(_T("%f"),m_second);
  	/*int i=m_display.GetLength();
	while(m_display.GetAt(i-1)=='0')
	{
		m_display.Delete(i-1,1);
		i--;
	}*/
  m_display=m_display.Left(m_display.GetLength()-1);// TODO: Add your control notification handler code here
  UpdateData(false);
  m_second=atof(m_display);
	//m_second=(int)(m_second/10);
  //UpdateDisplay(m_second);
 }
 
}

void CMyCalculatorDlg::OnButtonClear() 
{
	// TODO: Add your control notification handler code here
	m_first=0.0;
	m_second=0.0;
	m_operator="+";
	m_coff=1.0;
	m_item=0;

UpdateDisplay(0.0);
	
}

void CMyCalculatorDlg::OnButtonDiv() 
{
	// TODO: Add your control notification handler code here
	m_item=0;

    Calculate();
	m_operator="/";
	m_second=0.0;

}

void CMyCalculatorDlg::OnButtonEqual() 
{
	// TODO: Add your control notification handler code here

    Calculate();
	//m_second=0.0;
	m_item=1;
    //m_first=0.0;
	//m_operator="+";
}



void CMyCalculatorDlg::OnButtonMutiply() 
{
	// TODO: Add your control notification handler code here

	m_item=0;

    Calculate();
	m_second=0.0;
	m_operator="*";

}

void CMyCalculatorDlg::OnButtonPoint() 
{
	// TODO: Add your control notification handler code here
	m_coff=0.1;
}

void CMyCalculatorDlg::OnButtonReciprocal() 
{
	// TODO: Add your control notification handler code here
	if(m_item==0)
	{
	if(fabs(m_second)<0.000001)
	{
     m_display="除数不能为零";
	 UpdateData(false);
	 return;
	}
	m_second=1.0/m_second;
    UpdateDisplay(m_second);
	m_item=0;
	}
	else
	{
      m_first=1.0/m_first;
      UpdateDisplay(m_first);
	}
	m_item=0;
}

void CMyCalculatorDlg::OnButtonSign() 
{
	// TODO: Add your control notification handler code here
	if(	m_item==0)
	{
	 m_second=-m_second;
     UpdateDisplay(m_second);
     m_item=0;
	}
	else
	{
     m_first=-m_first;
     UpdateDisplay(m_first);
	}
	m_item=0;

}

void CMyCalculatorDlg::OnButtonSqrt() 
{
	// TODO: Add your control notification handler code here
	if(m_item==0)
	{
	 m_second=sqrt(m_second);
     UpdateDisplay(m_second);

	}
	else 
	{
     m_first=sqrt(m_first);
     UpdateDisplay(m_first);
	}
}

void CMyCalculatorDlg::OnButtonMinus() 
{
	// TODO: Add your control notification handler code

 	m_item=0;
   
    Calculate();
	m_second=0.0;
	m_operator="-";

}

void CMyCalculatorDlg::OnButton(int fig)
{
 if(m_coff==1.0)//作为整数输入
		m_second=m_second*10+fig;
	else
	{
		m_second=m_second+fig*m_coff;
		m_coff*=0.1;
	}
	UpdateDisplay(m_second);//更新编辑框的数据显示
}

void CMyCalculatorDlg::Calculate()
{
 		switch(m_operator.GetAt(0))
	{
	case'+':
		m_first+=m_second;break;
	case'-':
		m_first-=m_second;break;
	case'*':
		m_first*=m_second;break;
	case'%':
		m_first=(int)m_first%(int)m_second;break;
	case'/':
		if(fabs(m_second)<=0.0000001)
		{
			m_display="除数不能为零";
			UpdateData(false);
			return;
		}
		m_first/=m_second;break;
	}
	m_coff=1.0;
UpdateDisplay(m_first);
}

void CMyCalculatorDlg::UpdateDisplay(double lval)
{
    //m_display = _T("0.0");
    m_display.Format(_T("%lf"),lval);
	int i=m_display.GetLength();
	while(m_display.GetAt(i-1)=='0')
	{
		m_display.Delete(i-1,1);
		i--;
	}

		UpdateData(false);
}



void CMyCalculatorDlg::OnButtonMode() 
{
	// TODO: Add your control notification handler code here
	m_item=0;

    Calculate();
	m_operator="%";
	m_second=0.0;

	
}

void CMyCalculatorDlg::OnMENUabout() 
{
	// TODO: Add your command handler code here
	CAboutDlg dig;
	dig.DoModal();
}

void CMyCalculatorDlg::OnButtonClearnow() 
{
	// TODO: Add your control notification handler code here
	m_second=0.0;
    UpdateDisplay(m_second);
}

void CMyCalculatorDlg::OnButtonM() 
{
	// TODO: Add your control notification handler code here
	if(m_item==0)
	m_op=m_op+m_second;
	else m_op=m_op+m_first;

	
}

void CMyCalculatorDlg::OnButtonMs() 
{
	// TODO: Add your control notification handler code here
	if(m_item==0)
	m_op=m_second;
	else m_op=m_first;
	
}

void CMyCalculatorDlg::OnButtonMc() 
{
	// TODO: Add your control notification handler code here
	m_op=0.0;
}

void CMyCalculatorDlg::OnButtonMr() 
{
	// TODO: Add your control notification handler code here

	UpdateDisplay(m_op);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -