📄 mathexprdlg.cpp
字号:
// MathExprDlg.cpp : implementation file
//
//#include <vld.h> // memory leak detector
#include "stdafx.h"
#include "MathExpr.h"
#include "MathExprDlg.h"
#include "../../../MTParserLib/TestCases.h"
#include "../../../MTParserLib/MTTools.h"
#include "IndexDlg.h"
#include "DefineMacroDlg.h"
#include <math.h>
#include <float.h> // for the _finite function
#define PLUGINFILE _T("MTDateCOMPlugin.dll") // demo COM plugin file
#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()
/////////////////////////////////////////////////////////////////////////////
// CMathExprDlg dialog
CMathExprDlg::CMathExprDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMathExprDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMathExprDlg)
m_expr = _T("");
m_msg = _T("");
m_varx = 0.0;
m_vary = 0.0;
m_varz = 0.0;
m_result = 0.0;
m_benchType = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMathExprDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMathExprDlg)
DDX_Control(pDX, IDC_BENCHMARK, m_benchBtn);
DDX_Control(pDX, IDC_EVALUATE, m_evaluateBtn);
DDX_Text(pDX, IDC_EXPR, m_expr);
DDX_Text(pDX, IDC_MSG, m_msg);
DDX_Text(pDX, IDC_VARX, m_varx);
DDX_Text(pDX, IDC_VARY, m_vary);
DDX_Text(pDX, IDC_VARZ, m_varz);
DDX_Text(pDX, IDC_RESULT, m_result);
DDX_Radio(pDX, IDC_BENCHEVAL, m_benchType);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMathExprDlg, CDialog)
//{{AFX_MSG_MAP(CMathExprDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_EVALUATE, OnEvaluate)
ON_BN_CLICKED(IDC_INDEX, OnIndex)
ON_BN_CLICKED(IDC_BENCHMARK, OnBenchmark)
ON_BN_CLICKED(IDC_DEFINEMACRO, OnDefinemacro)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMathExprDlg message handlers
BOOL CMathExprDlg::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
m_expr = _T("pi*min(x+y+sin(z)/2^3-40.9988*2, avg(y,x*10,3,5))");
CoInitialize(NULL);
try
{
if( !MTTools::registerCOMObject(PLUGINFILE) )
{
MessageBox(_T("Unable to register the plugin"));
}
else
{
m_mathExpr.loadPlugin(_T("{4C639DCD-2043-42DC-9132-4B5C730855D6}"));
}
m_mathExpr.defineVar(_T("x"), &m_varx);
m_mathExpr.defineVar(_T("y"), &m_vary);
m_mathExpr.defineVar(_T("z"), &m_varz);
// constant definitions
m_mathExpr.defineConst(_T("pi"), 3.14159265359);
m_mathExpr.defineConst(_T("e"), 2.7182818284590452354);
}
catch( MTException e )
{
MessageBox(e.m_description.c_str(), _T("Definition error"), MB_OK|MB_ICONSTOP);
EndDialog(IDCANCEL);
return TRUE;
}
// Modification safety net! Unit tests.
MTTestSuite tc;
if( !tc.doTests() )
{
MessageBox(_T("Unit tests failed!"), _T(""), MB_OK);
}
UpdateData(0);
return TRUE; // return TRUE unless you set the focus to a control
}
void CMathExprDlg::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 CMathExprDlg::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 CMathExprDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMathExprDlg::OnOK()
{
//CDialog::OnOK();
}
void CMathExprDlg::OnEvaluate()
{
UpdateData(1);
m_msg = _T("");
try
{
m_result = m_mathExpr.evaluate(m_expr.GetBuffer(0));
unsigned int nbUsedVars = m_mathExpr.getNbUsedVars();
if( nbUsedVars > 0 )
{
m_msg = _T("Used variables: ");
for( unsigned int t=0; t<nbUsedVars; t++ )
{
m_msg += m_mathExpr.getUsedVar(t).varName.c_str();
if( t != nbUsedVars-1 )
{
m_msg += _T(", ");
}
}
}
else
{
m_msg = _T("No variable used");
}
if( m_mathExpr.isConstant() )
{
m_msg += _T("\r\n");
m_msg += _T("Constant expression");
}
if( !m_mathExpr.isFinite() )
{
m_msg += _T("\r\nInvalid result");
m_result = 0;
}
}
catch( MTException &e )
{
printException(e);
}
UpdateData(0);
}
void CMathExprDlg::OnBenchmark()
{
UpdateData(1);
m_msg = _T("Processing...");
UpdateData(0);
UpdateWindow();
try
{
MTSTRING expr = m_expr.GetBuffer(0);
m_mathExpr.compile(m_expr.GetBuffer(0));
unsigned int loops;
unsigned int elapsed;
bool isValid;
CString msg;
if( m_benchType == 0 )
{
// evaluation benchmark
EvaluateBenchmark bench;
bench.setParser( &m_mathExpr );
isValid = bench.bench(500, loops, elapsed);
msg = _T("Evaluation Benchmark Results:\r\n\r\nTime per evaluation(ms) = %f\r\nEvaluations per second = %d");
}
else
{
// compilation benchmark
CompileBenchmark bench;
bench.setParser( &m_mathExpr, expr );
isValid = bench.bench(50, loops, elapsed);
msg = _T("Compilation Benchmark Results:\r\n\r\nTime per compilation(ms) = %f\r\nCompilations per second = %d");
}
if( isValid )
{
double each = (elapsed)/(double)loops;
int nbEvalPerSec = 1000*loops/elapsed;
m_msg.Format(msg, each, nbEvalPerSec);
}
else
{
CString msg = _T("Unable to benchmark accurately. Try again.");
m_msg = msg;
}
}
catch( MTException &e )
{
printException(e);
}
UpdateData(0);
}
void CMathExprDlg::OnIndex()
{
IndexDlg index(&m_mathExpr);
index.DoModal();
}
void CMathExprDlg::printException(const MTException &e)
{
m_msg = _T("");
for( unsigned int t=0; t<e.m_descriptionList.size(); t++ )
{
m_msg += e.m_descriptionList[t].c_str();
if( t != e.m_descriptionList.size()-1 )
{
m_msg += _T("\r\n");
}
}
UpdateData(0);
}
BOOL CMathExprDlg::DestroyWindow()
{
MTTools::unregisterCOMObject(PLUGINFILE);
return CDialog::DestroyWindow();
}
void CMathExprDlg::OnDefinemacro()
{
CDefineMacroDlg dlg;
dlg.setParser(&m_mathExpr);
dlg.DoModal();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -