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

📄 calculatordlg.cpp

📁 一个不错的简单实用的公式解析器
💻 CPP
字号:
// CalculatorDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Calculator.h"
#include "CalculatorDlg.h"
#include "ALXParser.h"
#include "OperatorsDlg.h"
#include "FunctionsDlg.h"
#include "VariablesDlg.h"
#include "ListExpressionDlg.h"
#include "SetCycleDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CCalculatorDlg dialog

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

void CCalculatorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCalculatorDlg)
	DDX_Control(pDX, IDC_STATIC_TIME, m_ctrlTime);
	DDX_Control(pDX, IDC_EDIT_EXPRESSION, m_ctrlEditExpression);
	DDX_Control(pDX, IDC_LIST_ERRORS, m_ctrlListErrors);
	DDX_Control(pDX, IDC_EDIT_RESULT, m_ctrlResult);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCalculatorDlg, CDialog)
	//{{AFX_MSG_MAP(CCalculatorDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_GO, OnButtonGo)
	ON_BN_CLICKED(IDC_BUTTON_SELECT, OnButtonSelect)
	ON_BN_CLICKED(IDC_BUTTON_OPERATORS, OnButtonOperators)
	ON_BN_CLICKED(IDC_BUTTON_FUNCTIONS, OnButtonFunctions)
	ON_BN_CLICKED(IDC_BUTTON_VARIABLES, OnButtonVariables)
	ON_BN_CLICKED(IDC_BUTTON_CYCLE, OnButtonCycle)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCalculatorDlg message handlers

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

	m_ctrlEditExpression.Init();

	// 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
	
	// Start the dialog in its contracted phase
	ExpandDialog (IDC_DIVIDER, FALSE);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CCalculatorDlg::OnButtonGo() 
{
	Calculete(-1, 1);
}

void CCalculatorDlg::ExpandDialog (int nResourceID, BOOL bExpand)
{
	// Expand the dialog to full size if bExpand is TRUE; otherwise
	// contract, with the new bottom set to the bottom of the 
	// divider control specified in nResourceID.

	static  CRect rcLarge;
	static  CRect rcSmall;
	static	CString strAdvansed;
	CString sExpand;

	// First time through, save the dialog抯 large
	// and small sizes
	if (rcLarge.IsRectNull())
	{
		CRect rcLandmark;
		CWnd* pWndLandmark = GetDlgItem (nResourceID);
		ASSERT(pWndLandmark);	
	
		GetWindowRect (rcLarge);
		pWndLandmark->GetWindowRect (rcLandmark);
	
		rcSmall = rcLarge;
		rcSmall.bottom = rcLandmark.top;
	}

	if (bExpand)
	{
		// Expand the dialog: resize the dialog
		// to its original size (rcLarge)
		SetWindowPos(NULL, 0, 0, rcLarge.Width(), rcLarge.Height(),
			SWP_NOMOVE | SWP_NOZORDER);
	}
	else
	{
		// Contract the dialog to the small size
		SetWindowPos(NULL, 0, 0, rcSmall.Width(), rcSmall.Height(),
			SWP_NOMOVE | SWP_NOZORDER);
	}	
}

void CCalculatorDlg::OnButtonSelect() 
{
	CListExpressionDlg m_ListExpressionDlg;

	if(m_ListExpressionDlg.DoModal() == IDOK)
	{
		m_ctrlEditExpression.SetSel(0, -1);
		m_ctrlEditExpression.ReplaceSel(m_ListExpressionDlg.m_strExpression, TRUE);
	}
	
	m_ctrlEditExpression.SetFocus();
}

void CCalculatorDlg::OnButtonOperators() 
{
	COperatorsDlg m_ListOperatorsDlg;

	m_ListOperatorsDlg.DoModal();

	m_ctrlEditExpression.SetFocus();
}

void CCalculatorDlg::OnButtonFunctions() 
{
	CFunctionsDlg m_ListFunctionsDlg;

	if(m_ListFunctionsDlg.DoModal() == IDOK)
		m_ctrlEditExpression.ReplaceSel(m_ListFunctionsDlg.m_strFunction, TRUE);

	m_ctrlEditExpression.SetFocus();
}

void CCalculatorDlg::OnButtonVariables() 
{
	CVariablesDlg m_ListVariablesDlg;

	m_ListVariablesDlg.m_pParser = &m_Parser;
	m_ListVariablesDlg.DoModal();

	m_ctrlEditExpression.SetFocus();
}

void CCalculatorDlg::OnButtonCycle() 
{
	static int	nType = 0;
	static UINT	nCount = 10000;

	CSetCycleDlg m_SetCycleDlg;

	m_SetCycleDlg.m_nType = nType;
	m_SetCycleDlg.m_nCount = nCount;

	if(m_SetCycleDlg.DoModal() == IDOK)
	{
		nType = m_SetCycleDlg.m_nType;
		nCount = m_SetCycleDlg.m_nCount;

		Calculete(m_SetCycleDlg.m_nType, m_SetCycleDlg.m_nCount);
	}
}

void CCalculatorDlg::Calculete(int nType, UINT nCount)
{
	CString strExpression;
	m_ctrlEditExpression.GetWindowText(strExpression);
	m_ctrlEditExpression.SetFocus();

	VARIANT* pvarResult = NULL;
	m_ctrlListErrors.ResetContent();

	SYSTEMTIME stBeginTime, stEndTime;
	try
	{
		switch(nType)
		{
		case 0:
			{
				::GetSystemTime(&stBeginTime);

				for(UINT i = 0; i < nCount; i++)
				{
					m_Parser.Parse(strExpression);
					pvarResult = m_Parser.Execute();
				}

				::GetSystemTime(&stEndTime);
	
				break;
			}
		case 1:
			{
				::GetSystemTime(&stBeginTime);

				for(UINT i = 0; i < nCount; i++)
				{
					m_Parser.Parse(strExpression);
				}

				::GetSystemTime(&stEndTime);
	
				pvarResult = m_Parser.Execute();

				break;
			}
		case 2:
			{
				m_Parser.Parse(strExpression);

				::GetSystemTime(&stBeginTime);

				for(UINT i = 0; i < nCount; i++)
				{
					pvarResult = m_Parser.Execute();
				}

				::GetSystemTime(&stEndTime);
	
				break;
			}
		default:
			{
				::GetSystemTime(&stBeginTime);

				m_Parser.Parse(strExpression);
				pvarResult = m_Parser.Execute();

				::GetSystemTime(&stEndTime);
			}
		}
	}
	catch(CALXParserException* e)
	{
		ExpandDialog (IDC_DIVIDER, TRUE);

		m_ctrlResult.SetWindowText("");
		m_ctrlTime.SetWindowText("");

		for(int i = 0; i < e->GetErrorCount(); i++)
		{
			e->GetErrorInfo(i);
			m_ctrlListErrors.InsertString(i, e->m_pErrorInfo->m_strDescription);
//			e->ReportError();
		}
		e->Delete();

		return;
	}
	catch(CException* e)
	{
		ExpandDialog (IDC_DIVIDER, FALSE);

		m_ctrlResult.SetWindowText("");
		m_ctrlTime.SetWindowText("");

		e->ReportError();
		e->Delete();

		return;
	}

	ExpandDialog (IDC_DIVIDER, FALSE);

	WORD wMilliseconds = (stEndTime.wMinute * 60 * 1000 + stEndTime.wSecond * 1000 + stEndTime.wMilliseconds) - (stBeginTime.wMinute * 60 * 1000 + stBeginTime.wSecond * 1000 + stBeginTime.wMilliseconds);
	if(wMilliseconds <= 0)
		m_ctrlTime.SetWindowText("Less 10 millisec");
	else
	{
		CString str;
		str.Format("%d millisec", wMilliseconds);
		m_ctrlTime.SetWindowText(str);
	}
	



	CString strRez;
	switch(V_VT(pvarResult))
	{
	case VT_R8:
		{
			strRez.Format("%f",V_R8(pvarResult));
			break;
		}
	case VT_BSTR:
		{
			CString strRezult(V_BSTR(pvarResult));
			strRez.Format("\"%s\"",strRezult);
			break;
		}
	case VT_DATE:
		{
			COleDateTime dte(V_DATE(pvarResult));
			strRez = "{"+dte.Format()+"}";
			break;
		}
	case VT_BOOL:
		{
			if(V_BOOL(pvarResult))
				strRez = ".T.";
			else
				strRez = ".F.";
			break;
		}
	default:
		strRez = "Error result"; 

	}

	m_ctrlResult.SetWindowText(strRez);

	return;
}

⌨️ 快捷键说明

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