FormulaParserLib start page


FormulaParserLib function reference


FormulaParserLib_SetFormula The function sets formula expression to parse.
FormulaParserLib_IsValid The function checks formula expression validity.
FormulaParserLib_Parse The function makes parsing of formula.
FormulaParserLib_GetStatementNumber The function gets number of statements for the formula.
FormulaParserLib_GetStatement The function gets formula statements information.

FormulaParserLib_SetFormula

The function sets formula expression to parse.

BOOL FormulaParserLib_SetFormula(char *formula)

Parameters:
formula - [in] formula expression less then 1000 characters in length

Return values:

If the function succeeds, the return value is TRUE. If the function fails, the return value is FALSE.

Remarks:

Use the function to set formula expression to parse.
There are next operations, arguments and functions may be used in formula expression:

Operations by priority:
* (arithmetical multiplication), / (arithmetical division) - real numbers calculation
+ (arithmetical addition), - (arithmetical substraction) - real numbers calculation
& (bitwise AND), | (bitwise or), ^ (bitwise xor) - integer numbers calculation

Functions:
abs(x) absolute value
acos(x) arccosine
asin(x) arcsine
atan(x) arctangent
atan2(y,x) arctangent y/x
ceil(x) ceiling (the smallest integer that is greater than or equal to x)
cos(x) cosine
cosh(x) hyperbolic cosine
div(x,y) integer quotient from division x/y
exp(x) exponential
floor(x) floor (the largest integer that is less than or equal to x)
mod(x,y) integer remainder from division x/y
hypot(x,y) length of the hypotenuse of a right triangle, given the length of the two sides x and y. Equivalent to the square root of x2 + y2
log(x) natural logarithm
log10(x) base-10 logarithm
max(x,y) maximal of 2 arguments
min(x,y) minimal of 2 arguments
pow(x,y) x raised to the power of y
sin(x) sine
sinh(x) hyperbolic sine
sqrt(x) square root
tan(x) tangent
tanh(x) hyperbolic tangent

Arguments:
Arguments may be variable names or numbers with or without decimal point and sign. Names may start only with literal character (A..Z, a..z) and may contain literal characters, digits and underscore '_' inside.

The following names are correct:

a
image1
arg_5
s2p6d4

The following names are incorrect:

2arg (starting with number)
arg-27 (contains non-literal symbol)
param$ (contains non-literal symbol)

Numbers may start from sign, point or digit and may contain only digits.

The following numbers are correct:

.095
+2334
-.3235
-123.234
2343.454

The following numbers are incorrect:

-.32.34 (two points inside number)
4534a44 ('a' is not a digit)
344.-34 (sign inside argument)

Example 1:

char formula_str[1000]="sqrt(5+min(a,b))";

if(!FormulaParserLib_SetFormula(formula_str))
{
  //error handling
}

Example 2:

CString formula_str="sqrt(5+min(a,b))";

if(!FormulaParserLib_SetFormula(formula_str.GetBuffer(1000)))
{
  //error handling
}
To the start of page...

FormulaParserLib_IsValid

The function checks formula expression validity.

BOOL FormulaParserLib_IsValid(char* formula)

Parameters:
formula - [in] formula expression less then 1000 characters to check. If NULL or "" formula expression set with FormulaParserLib_SetFormula is used

Return values:

If the function succeeds and formula is valid, the return value is TRUE. Otherwise the return value is FALSE.

Remarks:

Use the function to check formula expression validity.

Example 1:

char formula_str[1000]="sqrt(5+min(a,b))";

if(!FormulaParserLib_SetFormula(formula_str))
{
  //error handling
}

if(!FormulaParserLib_IsValid(NULL)
{
  //error handling
}

Example 2:

CString formula_str="sqrt(5+min(a,b))";

if(!FormulaParserLib_IsValid(formula_str.GetBuffer(1000)))
{
  //error handling
}
To the start of page...

FormulaParserLib_Parse

The function makes parsing of formula.

BOOL FormulaParserLib_Parse()

Parameters:
none

Return values:

If the function succeeds and formula parsed without errors, the return value is TRUE. If any errors during parsing, the return value is FALSE.

Remarks:

Use the function to parse formula expression set with FormulaParserLib_SetFormula.

Example 1:

char formula_str[1000]="sqrt(5+min(a,b))";

if(!FormulaParserLib_SetFormula(formula_str))
{
  //error handling
}

if(!FormulaParserLib_IsValid(NULL)
{
  //error handling
}

if(!FormulaParserLib_Parse())
{
  //error handling
}

Example 2:

CString formula_str="sqrt(5+min(a,b))";

if(!FormulaParserLib_SetFormula(formula_str.GetBuffer(1000)))
{
  //error handling
}

if(!FormulaParserLib_IsValid(NULL)
{
  //error handling
}

if(!FormulaParserLib_Parse())
{
  //error handling
}

To the start of page...

FormulaParserLib_GetStatementNumber

The function gets number of statements for the formula.

BOOL FormulaParserLib_GetStatementNumber(long *num)

Parameters:
num - [out] number of statement. Memory must be allocated by client

Return values:

If the function succeeds, the return value is TRUE. Otherwise, the return value is FALSE.

Remarks:

Use the function to get number of statements for the formula. The statements are simple (with only one operation or function) expressions that allows calculate complex formulas step by step.

Example 1:

char formula_str[1000]="sqrt(5+min(a,b))";

if(!FormulaParserLib_SetFormula(formula_str))
{
  //error handling
}

if(!FormulaParserLib_IsValid(NULL)
{
  //error handling
}

if(!FormulaParserLib_Parse())
{
  //error handling
}

long num;
if(!FormulaParserLib_GetStatementNumber(&num))
{
  //error handling
}

Example 2:

CString formula_str="sqrt(5+min(a,b))";

if(!FormulaParserLib_SetFormula(formula_str.GetBuffer(1000)))
{
  //error handling
}

if(!FormulaParserLib_IsValid(NULL)
{
  //error handling
}

if(!FormulaParserLib_Parse())
{
  //error handling
}

long num;
if(!FormulaParserLib_GetStatementNumber(&num))
{
  //error handling
}

To the start of page...

FormulaParserLib_GetStatement

The function gets formula statements information.

BOOL FormulaParserLib_GetStatement(long num,char* arg1,char* arg2,char* oper,char* funct,char* res)

Parameters:
num - [in] number of statement (from 0).
arg1 - [out] left argument (operand) of statement or argument of function.
arg2 - [out] right argument (operand) of statement.
oper - [out] operation.
funct - [out] function.
res - [out] name of result.

Return values:

If the function succeeds, the return value is TRUE. Otherwise, the return value is FALSE.

Remarks:

Memory for arg1,arg2,oper,funct,res parameters must be allocated by client. This memory must be enough to contain returned strings.

After parsing formula expression is separated on simple calculation steps - statements.
For example for the formula "a+b-c*d/sqrt(e)" such statements may be:

Result0=a+b num=0, arg1="a", arg2="b", oper="+", funct="", res="Result0"
Result1=c*d num=1, arg1="c", arg2="d", oper="*", funct="", res="Result1"
Result2=sqrt(e) num=2, arg1="e", arg2="", oper="", funct="sqrt", res="Result2"
Result3=Result1/Result2 num=3, arg1="Result1", arg2="Result2", oper="/", funct="", res="Result3"
Result4=Result0-Result3 num=4, arg1="Result0", arg2="Result3", oper="-", funct="", res="Result4"

There are three possible kinds of statements:

1. Operation:
res = arg1 oper arg2
funct is set to empty string ("")

2. Function:
res = funct(arg1
arg2 and oper are set to empty strings ("")

3. Constant:
res = arg1
arg2,oper,funct are set to empty strings ("")

Example 1:

char formula_str[1000]="sqrt(5+min(a,b))";

if(!FormulaParserLib_SetFormula(formula_str))
{
  //error handling
}

if(!FormulaParserLib_IsValid(NULL)
{
  //error handling
}

if(!FormulaParserLib_Parse())
{
  //error handling
}

long num;
if(!FormulaParserLib_GetStatementNumber(&num))
{
  //error handling
}

char arg1[1000],arg2[1000],oper[1000],funct[1000],res[1000];
for(long i=0;i<num;i++)
{
	if(!FormulaParserLib_GetStatement(i,arg1,arg2,oper,funct,res))
	{
		//error handling
	}
}

Example 2:

CString formula_str="sqrt(5+min(a,b))";

if(!FormulaParserLib_SetFormula(formula_str.GetBuffer(1000)))
{
  //error handling
}

if(!FormulaParserLib_IsValid(NULL)
{
  //error handling
}

if(!FormulaParserLib_Parse())
{
  //error handling
}

long num;
if(!FormulaParserLib_GetStatementNumber(&num))
{
  //error handling
}

CString arg1,arg2,oper,funct,res;
for(long i=0;i<num;i++)
{
	if(!FormulaParserLib_GetStatement(i,arg1.GetBuffer(1000),arg2.GetBuffer(1000),oper.GetBuffer(1000),
										funct.GetBuffer(1000),res.GetBuffer(1000)))
	{
		//error handling
	}
	arg1.ReleaseBuffer();
	arg2.ReleaseBuffer();
	oper.ReleaseBuffer();
	funct.ReleaseBuffer();
	res.ReleaseBuffer();
}

To the start of page...


www.maxdz.com
Rambler's Top100