FormulaCalculatorLib start page


FormulaCalculatorLib function reference


FormulaCalculatorLib_SetFormula The function sets formula expression to calculate.
FormulaCalculatorLib_Calculate The function calculates the formula.
FormulaCalculatorLib_Reset The function resets the calculator.
FormulaCalculatorLib_AddStatement The function adds calculation statement to the calculator.
FormulaCalculatorLib_AddArgument The function adds argument to the calculator.

FormulaCalculatorLib_SetFormula

The function sets formula expression to calculate.

BOOL FormulaCalculatorLib_SetFormula(char* formula_str)

Parameters:
formula_str - [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 calculate.
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(!FormulaCalculatorLib_SetFormula(formula_str))
{
  //error handling
}

Example 2:

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

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

FormulaCalculatorLib_Calculate

The function calculates the formula.

BOOL FormulaCalculatorLib_Calculate(double* double_res,char* arg_list)

Parameters:
double_res - [out] result of calculation.
arg_list - [out] argument list.

Return values:

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

Remarks:

Use the function calculate the formula. If argument list is empty (arg_list[0] is 0) then the formula calculated successfully and there are no undefined variables found. Otherwise argument list contains list of undefined variables (arguments), separated by comma (',') and double_res is always 0.

Example

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

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

double res;
char arg_list[1000];
if(!FormulaCalculatorLib_Calculate(&res,arg_list)
{
  //error handling
}

if(arg_list[0])
{
	//arg_list contains argument list, separated by comma. For this example - "a,b"
	//define arguments using FormulaCalculatorLib_AddArgument, then call FormulaCalculatorLib_Calculate again
}
To the start of page...

FormulaCalculatorLib_Reset

The function resets the calculator.

BOOL FormulaCalculatorLib_Reset()

Parameters:
none

Return values:

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

Remarks:

The functions resets statement and argument structures of calculator.

Example

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

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

double res;
char arg_list[1000];
if(!FormulaCalculatorLib_Calculate(&res,arg_list)
{
  //error handling
}

if(!FormulaCalculatorLib_Reset())
{
  //error handling
}
To the start of page...

FormulaCalculatorLib_AddStatement

The function adds calculation statement to the calculator.

BOOL FormulaCalculatorLib_AddStatement(char* arg1,char* arg2,char* oper,char* funct,char* res)

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

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 add calculation statement to the calculator.
Usually data received from FormulaParserLib_GetStatement must be used here.
Refer to the FormulaParserLib function reference for the details on FormulaParserLib_GetStatement.

Example:


for(long i=0;i<statement_num;i++)
{
	char arg1[1000],arg2[1000],oper[1000],funct[1000],res[1000];
	if(!FormulaParserLib_GetStatement(i,arg1,arg2,aoper,funct,res))
	{
	  //error handling
	}
	if(!FormulaCalculatorLib_AddStatement(arg1,arg2,aoper,funct,res))
	{
		//error handling
	}
}
To the start of page...

FormulaCalculatorLib_AddArgument

The function adds argument to the calculator.

BOOL FormulaCalculatorLib_AddArgument(char* name,char* val_str)

Parameters:
name - [in] argument name.
val_str - [in] argument value.

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 add argument to the calculator. Used when undefined variables are present in formula expression.See example...

Example:

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

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

double res;
char arg_list[1000];
if(!FormulaCalculatorLib_Calculate(&res,arg_list))
{
  //error handling
}

if(arg_list[0])
{
  //now "a,b" is in arg_list 

  if(!FormulaCalculatorLib_AddArgument("a",10))
  {
    //error handling
  }
  if(!FormulaCalculatorLib_AddArgument("b",20))
  {
    //error handling
  }
  if(!FormulaCalculatorLib_Calculate(&res,arg_list))
  {
    //error handling
  }
  //arg list is empty. The result is calculated...
}
		
To the start of page...

www.maxdz.com
Rambler's Top100