Contents
You may use mathematically-correct formulas, including brackets, functions etc.
ARCTAN, COS, SIN, TAN, ABS, EXP, LN, LOG, SQRT, SQR, INT, FRAC, TRUNC, ROUND( x, precision ), ARCSIN, ARCCOS, SGN, SIGN
<condition> ? <true expr.> : <false expr.>where
CCalc script begins with "#!". The syntax is clear from the example:
#! a := 2; b := 3; return a * b + 1;
There library consists of three components with their own API:
hqStrMap* Strmap_Create( int extrabytes, int dup ); hqStrMap* Strmap_CreateFromChain( int extrabytes, char *strchain, void *data ); void StrMap_Destroy( hqStrMap* strmap ); void StrMap_AddString( hqStrMap* strmap, char *str, void *data ); void StrMap_AddStrLen( hqStrMap* strmap, char *str, int len, void *data ); void StrMap_ShrinkMem( hqStrMap* strmap ); void StrMap_Trim( hqStrMap* strmap, int NewCount ); void StrMap_TrimClear( hqStrMap* strmap, int NewCount ); void StrMap_SetCapacity( hqStrMap* strmap, int NewCapacity ); int StrMap_IndexOf( hqStrMap* strmap, char *str, void **data ); int StrMap_LenIndexOf( hqStrMap* strmap, char *str, int len, void **data ); char* StrMap_GetString( hqStrMap* strmap, int index, int *len, void **data ); void Strmap_FillFromChain( hqStrMap* strmap, char *strchain, void *data ); |
char varnames[] = "X\000" "Y\000" "Z\000\000"; double varvalues[] = { 5.3, 6.1, -7.45 }; hqStrMap* varmap = Strmap_CreateFromChain( sizeof(double), varnames, varvalues ); ... StrMap_Destroy( varmap ); |
double varvalues[] = { 5.3, 6.1 }; hqStrMap* varmap = Strmap_Create( sizeof(double), 0 /* do not copy strings */ ); StrMap_AddString( varmap, "X", varvalues ); StrMap_AddString( varmap, "Y", varvalues+1 ); ... StrMap_Destroy( varmap ); |
typedef struct { /* public */ hqStrMap *Parameters; hqStrMap *ExtFunctions; PrmSrchFunc MoreParams; void *ParamFuncParam; /* private */ ... } hqMathParser; hqMathParser* MathParser_Create( char *MoreLetters ); void MathParser_Destroy( hqMathParser* parser ); char* MathParser_Parse( hqMathParser* parser, char *Formula, double *result ); |
hqMathParser* parser = MathParser_Create( NULL ); /* ... creating list of variables. See StrMap reference for example */ parser->Parameters = varmap; /* ... parsing expressions */ MathParser_Destroy( parser ); |
double result; char *ErrMsg = MathParser_Parse( parser, "sin(x * x) / 2", &result ); if ( ErrMsg == NULL ) printf( "%.15G\n", result ); else { puts( ErrMsg ); |
Of course this documentation is incomplete as I have no time now to write more. For more information see ccalc.c as an example of usage of the library;