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

📄 caldlg.cpp

📁 MFC下的计算器
💻 CPP
字号:
// calDlg.cpp : implementation file
//

#include "stdafx.h"
#include "cal.h"
#include "calDlg.h"
#include "math.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()

/////////////////////////////////////////////////////////////////////////////
// CCalDlg dialog

CCalDlg::CCalDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCalDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCalDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCalDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCalDlg)
	DDX_Control(pDX, IDC_BUTTON_9, m_Button9);
	DDX_Control(pDX, IDC_BUTTON_8, m_Button8);
	DDX_Control(pDX, IDC_BUTTON_7, m_Button7);
	DDX_Control(pDX, IDC_BUTTON_6, m_Button6);
	DDX_Control(pDX, IDC_BUTTON_5, m_Button5);
	DDX_Control(pDX, IDC_BUTTON_4, m_Button4);
	DDX_Control(pDX, IDC_BUTTON_3, m_Button3);
	DDX_Control(pDX, IDC_BUTTON_27, m_ButtonLog);
	DDX_Control(pDX, IDC_BUTTON_26, m_ButtonLn);
	DDX_Control(pDX, IDC_BUTTON_25, m_ButtonExp);
	DDX_Control(pDX, IDC_BUTTON_24, m_ButtonPow);
	DDX_Control(pDX, IDC_BUTTON_23, m_ButtonCub);
	DDX_Control(pDX, IDC_BUTTON_22, m_ButtonSqu);
	DDX_Control(pDX, IDC_BUTTON_21, m_ButtonTan);
	DDX_Control(pDX, IDC_BUTTON_20, m_ButtonCos);
	DDX_Control(pDX, IDC_BUTTON_2, m_Button2);
	DDX_Control(pDX, IDC_BUTTON_19, m_ButtonSin);
	DDX_Control(pDX, IDC_BUTTON_18, m_ButtonReverse);
	DDX_Control(pDX, IDC_BUTTON_17, m_ButtonSqrt);
	DDX_Control(pDX, IDC_BUTTON_16, m_ButtonClear);
	DDX_Control(pDX, IDC_BUTTON_15, m_ButtonDiv);
	DDX_Control(pDX, IDC_BUTTON_14, m_ButtonMul);
	DDX_Control(pDX, IDC_BUTTON_13, m_ButtonSub);
	DDX_Control(pDX, IDC_BUTTON_12, m_ButtonAdd);
	DDX_Control(pDX, IDC_BUTTON_11, m_ButtonEqu);
	DDX_Control(pDX, IDC_BUTTON_10, m_ButtonPoint);
	DDX_Control(pDX, IDC_BUTTON_1, m_Button1);
	DDX_Control(pDX, IDC_BUTTON_0, m_Button0);
	DDX_Control(pDX, IDC_EDIT1, m_DspEdit);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCalDlg, CDialog)
	//{{AFX_MSG_MAP(CCalDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_0, OnButton0)
	ON_BN_CLICKED(IDC_BUTTON_1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON_11, OnButtonEqu)
	ON_BN_CLICKED(IDC_BUTTON_12, OnButtonAdd)
	ON_BN_CLICKED(IDC_BUTTON_10, OnButtonPoint)
	ON_BN_CLICKED(IDC_BUTTON_13, OnButtonSub)
	ON_BN_CLICKED(IDC_BUTTON_14, OnButtonMul)
	ON_BN_CLICKED(IDC_BUTTON_15, OnButtonDiv)
	ON_BN_CLICKED(IDC_BUTTON_16, OnButtonClear)
	ON_BN_CLICKED(IDC_BUTTON_17, OnButtonSqrt)
	ON_BN_CLICKED(IDC_BUTTON_18, OnButtonReverse)
	ON_BN_CLICKED(IDC_BUTTON_19, OnButtonSin)
	ON_BN_CLICKED(IDC_BUTTON_2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON_20, OnButtonCos)
	ON_BN_CLICKED(IDC_BUTTON_21, OnButtonTan)
	ON_BN_CLICKED(IDC_BUTTON_22, OnButtonSqu)
	ON_BN_CLICKED(IDC_BUTTON_23, OnButtonCub)
	ON_BN_CLICKED(IDC_BUTTON_24, OnButtonPow)
	ON_BN_CLICKED(IDC_BUTTON_25, OnButtonExp)
	ON_BN_CLICKED(IDC_BUTTON_26, OnButtonLn)
	ON_BN_CLICKED(IDC_BUTTON_27, OnButtonLog)
	ON_BN_CLICKED(IDC_BUTTON_3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON_4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON_5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON_6, OnButton6)
	ON_BN_CLICKED(IDC_BUTTON_7, OnButton7)
	ON_BN_CLICKED(IDC_BUTTON_8, OnButton8)
	ON_BN_CLICKED(IDC_BUTTON_9, OnButton9)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCalDlg message handlers

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

void CCalDlg::OnButton0() 
{
	// TODO: Add your control notification handler code here
	if(m_Mark==0)
	{
		m_StrBegin=m_StrBegin+"0";
			m_DspEdit.OnDisplay(m_StrBegin);
	}
	else
	{
		m_StrEnd=m_StrEnd+"0";
		m_IsCheckPoint=0;
		m_DspEdit.OnDisplay(m_StrEnd);
	}
}

void CCalDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	if(m_Mark==0)
	{
		m_StrBegin=m_StrBegin+"1";
			m_DspEdit.OnDisplay(m_StrBegin);
	}
	else
	{
		m_StrEnd=m_StrEnd+"1";
		m_IsCheckPoint=0;
		m_DspEdit.OnDisplay(m_StrEnd);
	}
}

void CCalDlg::OnButtonEqu() 
{
	// TODO: Add your control notification handler code here
		Result();
		
}

void CCalDlg::OnButtonAdd() 
{
	// TODO: Add your control notification handler code here
	m_begin=atof(m_StrBegin);
	m_bit=1;
	m_Mark=1;

}

void CCalDlg::OnButtonPoint() 
{
	// TODO: Add your control notification handler code here
	if(m_Mark==0)
	{

		if(m_IsCheckPoint!=1&&m_StrBegin!="")
		{
			m_StrBegin=m_StrBegin+".";
			m_IsCheckPoint=1;
		}
	}
	else 
	{
		if(m_IsCheckPoint!=1&&m_StrBegin!="")
		{
			m_StrEnd=m_StrEnd+".";
			m_IsCheckPoint=1;
		}
	}

}

void CCalDlg::OnButtonSub() 
{
	// TODO: Add your control notification handler code here
	m_begin=atof(m_StrBegin);
	m_bit=2;
	m_Mark=1;

}

void CCalDlg::OnButtonMul() 
{
	// TODO: Add your control notification handler code here
	m_begin=atof(m_StrBegin);
	m_bit=3;
	m_Mark=1;

	
}

void CCalDlg::OnButtonDiv() 
{
	// TODO: Add your control notification handler code here
	m_begin=atof(m_StrBegin);
	m_bit=4;
	m_Mark=1;

}

void CCalDlg::OnButtonClear() 
{
	// TODO: Add your control notification handler code here
	m_IsCheckPoint=0;   //消除小数点按下的标记
	 
	m_StrBegin="";
	m_StrEnd="";

    m_Mark=0;
	m_begin=0.0;
	m_end=0.0;
	m_bit=0;
	m_DspEdit.OnDisplay("0.");
}

void CCalDlg::OnButtonSqrt() 
{
	// TODO: Add your control notification handler code here
	m_begin=atof(m_StrBegin);
	m_bit=5;
	m_Mark=1;

		
}

void CCalDlg::OnButtonReverse() 
{
	// TODO: Add your control notification handler code here
	m_begin=atof(m_StrBegin);
	m_bit=6;
	m_Mark=1;

	
}

void CCalDlg::OnButtonSin() 
{
	// TODO: Add your control notification handler code here
	m_begin=atof(m_StrBegin);
	m_bit=8;
	m_Mark=1;

		
}

void CCalDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	if(m_Mark==0)
	{
		m_StrBegin=m_StrBegin+"2";
			m_DspEdit.OnDisplay(m_StrBegin);
	}
	else
	{
		m_StrEnd=m_StrEnd+"2";
		m_IsCheckPoint=0;
		m_DspEdit.OnDisplay(m_StrEnd);
	}
}

void CCalDlg::OnButtonCos() 
{
	// TODO: Add your control notification handler code here
	m_begin=atof(m_StrBegin);
	m_bit=9;
	m_Mark=1;

}

void CCalDlg::OnButtonTan() 
{
	// TODO: Add your control notification handler code here
	m_begin=atof(m_StrBegin);
	m_bit=10;
	m_Mark=1;

}

void CCalDlg::OnButtonSqu() 
{
	// TODO: Add your control notification handler code here
	m_begin=atof(m_StrBegin);
	m_bit=11;
	m_Mark=1;

}

void CCalDlg::OnButtonCub() 
{
	// TODO: Add your control notification handler code here
	m_begin=atof(m_StrBegin);
	m_bit=12;
	m_Mark=1;

	
}

void CCalDlg::OnButtonPow() 
{
	// TODO: Add your control notification handler code here
	m_begin=atof(m_StrBegin);
	m_bit=13;
	m_Mark=1;

	
}

void CCalDlg::OnButtonExp() 
{
	// TODO: Add your control notification handler code here
 	m_begin=atof(m_StrBegin);
	m_bit=14;
	m_Mark=1;

	
}

void CCalDlg::OnButtonLn() 
{
	// TODO: Add your control notification handler code here
	m_begin=atof(m_StrBegin);
	m_bit=15;
	m_Mark=1;

	
}

void CCalDlg::OnButtonLog() 
{
	// TODO: Add your control notification handler code here
	m_begin=atof(m_StrBegin);
	m_bit=16;
	m_Mark=1;

	
}

void CCalDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	if(m_Mark==0)
	{
		m_StrBegin=m_StrBegin+"3";
			m_DspEdit.OnDisplay(m_StrBegin);
	}
	else
	{
		m_StrEnd=m_StrEnd+"3";
		m_IsCheckPoint=0;
		m_DspEdit.OnDisplay(m_StrEnd);
	}
}

void CCalDlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
	if(m_Mark==0)
	{
		m_StrBegin=m_StrBegin+"4";
			m_DspEdit.OnDisplay(m_StrBegin);
	}
	else
	{
		m_StrEnd=m_StrEnd+"4";
		m_IsCheckPoint=0;
		m_DspEdit.OnDisplay(m_StrEnd);
	}
}

void CCalDlg::OnButton5() 
{
	// TODO: Add your control notification handler code here
	if(m_Mark==0)
	{
		m_StrBegin=m_StrBegin+"5";
			m_DspEdit.OnDisplay(m_StrBegin);
	}
	else
	{
		m_StrEnd=m_StrEnd+"5";
		m_IsCheckPoint=0;
		m_DspEdit.OnDisplay(m_StrEnd);
	}
}

void CCalDlg::OnButton6() 
{
	// TODO: Add your control notification handler code here
	if(m_Mark==0)
	{
		m_StrBegin=m_StrBegin+"6";
			m_DspEdit.OnDisplay(m_StrBegin);
	}
	else
	{
		m_StrEnd=m_StrEnd+"6";
		m_IsCheckPoint=0;
		m_DspEdit.OnDisplay(m_StrEnd);
	}
}

void CCalDlg::OnButton7() 
{
	// TODO: Add your control notification handler code here
	if(m_Mark==0)
	{
		m_StrBegin=m_StrBegin+"7";
			m_DspEdit.OnDisplay(m_StrBegin);
	}
	else
	{
		m_StrEnd=m_StrEnd+"7";
		m_IsCheckPoint=0;
		m_DspEdit.OnDisplay(m_StrEnd);
	}
}

void CCalDlg::OnButton8() 
{
	// TODO: Add your control notification handler code here
	if(m_Mark==0)
	{
		m_StrBegin=m_StrBegin+"8";
			m_DspEdit.OnDisplay(m_StrBegin);
	}
	else
	{
		m_StrEnd=m_StrEnd+"8";
		m_IsCheckPoint=0;
		m_DspEdit.OnDisplay(m_StrEnd);
	}
}

void CCalDlg::OnButton9() 
{
	// TODO: Add your control notification handler code here
	if(m_Mark==0)
	{
		m_StrBegin=m_StrBegin+"9";
			m_DspEdit.OnDisplay(m_StrBegin);
	}
	else
	{
		m_StrEnd=m_StrEnd+"9";
		m_IsCheckPoint=0;
		m_DspEdit.OnDisplay(m_StrEnd);
	}
}

void CCalDlg::Result()
{
	m_end=atof(m_StrEnd);   //把输入数据转换为浮点数

	switch(m_bit)
	{
	case 1:
		m_end+=m_begin;
		break;
	case 2:
		m_end=m_begin-m_end;
		break;
	case 3:
		m_end=m_begin*m_end;
		break;
	case 4:
		m_end=m_begin/m_end;
		break;
	case 5:
		if(m_end<0)
		{
			m_DspEdit.OnDisplay("函数输入无效!");
			break;
		}
		m_end=sqrt(m_end);
		break;
	case 6:
		if(m_end==0)
		{	m_DspEdit.OnDisplay("分母输入无效!");
			break;
		}
		m_end=1.0/m_end;
		break;
	
	case 8:
		m_end=sin(m_end);
		break;
	case 9:
		m_end=cos(m_end);
		break;
	case 10:
		m_end=tan(m_end);
		break;
	case 11:
		m_end=m_end*m_end;
		break;
	case 12:
		m_end=m_end*m_end*m_end;
		break;
	case 13:
		m_end=pow(m_begin,m_end);
		break;
	case 14:
		m_end=exp(m_end);
		break;
	case 15:
		if(m_end<=0)
		{
			m_DspEdit.OnDisplay("函数输入无效!");
			break;
		}
		m_end=log(m_end);
		break;
	case 16:
		if(m_end<=0)
		{
			m_DspEdit.OnDisplay("函数输入无效!");
			return;
		}
		m_end=log10(m_end);
		break;
	}


	m_begin=m_end;
	m_bit=0;
	
	m_StrEnd="";

	gcvt(m_end,15,m_buffer);
	m_StrBegin=m_buffer;
	m_DspEdit.SetSel(0,-1);
	m_DspEdit.ReplaceSel(" ");
	m_DspEdit.OnDisplay(m_buffer);



}

⌨️ 快捷键说明

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