Click here to Skip to main content
15,894,410 members
Articles / Programming Languages / C#

An extensible math expression parser with plug-ins

Rate me:
Please Sign up or sign in to vote.
4.92/5 (147 votes)
13 Mar 2008CPOL51 min read 1.5M   29K   364  
Design and code for an extensible, maintainable, robust, and easy to use math parser.
#ifndef _CONVFUNC_INCLUDED
#define _CONVFUNC_INCLUDED

#include "..\..\..\MTParserLib\MTParserPrivate.h"
#include "..\..\..\MTParserLib\MTParserException.h"


class DateConvFunc : public MTConvFunctionI
{
public:

	
	virtual const MTCHAR* getSymbol(){return _T("datevalue"); }
	virtual const MTCHAR* getHelpString(){ return _T("datevalue(date_text)");}
	virtual const MTCHAR* getDescription(){ return _T("Converts a date string to a decimal number");}	
	virtual MTDOUBLE convert(const MTSTRING &val)
	{
		COleDateTime dt;
		dt.ParseDateTime(val.c_str(), VAR_DATEVALUEONLY);

		if( dt.m_status != COleDateTime::valid )
		{			
			throwConversionExcep();
			
		}

		return dt.m_dt;
	}

	virtual MTFunctionI* spawn() { return new DateConvFunc(); }
};

class TimeConvFunc : public MTConvFunctionI
{
public:

	
	virtual const MTCHAR* getSymbol(){return _T("timevalue"); }
	virtual const MTCHAR* getHelpString(){ return _T("timevalue(time_text)");}
	virtual const MTCHAR* getDescription(){ return _T("Converts a time string to a decimal number");}	
	virtual MTDOUBLE convert(const MTSTRING &val)
	{
		COleDateTime dt;
		dt.ParseDateTime(val.c_str(), VAR_TIMEVALUEONLY );

		if( dt.m_status != COleDateTime::valid )
		{
			throwConversionExcep();
		}

		return dt.m_dt;
	}

	virtual MTFunctionI* spawn(){ return new TimeConvFunc(); }
};


#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Canada Canada
Software Engineer working at a fun and smart startup company

Comments and Discussions