Contents
You may use mathematically-correct expressions, including brackets, functions etc.
ARCTAN, ARCSIN, ARCCOS, COS, SIN, TAN, ABS, EXP, LN, LOG, SQRT, SQR, SGN, SIGN, FRAC, TRUNC, FLOOR, CEIL, ROUND( x[, precision] )
<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;
The 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 int (*PrmSrchFunc) ( const char *str, int len, double *value,
void *param );
typedef struct {
/* public */
hqStrMap *Parameters; // List of numeric veriables
hqStrMap *ExtFunctions; // List of multi-argument external functions
PrmSrchFunc MoreParams; // Function for calculating unhandled parameters
void *ParamFuncParam; // Parameter given to this function
/* private */
...
} hqMathParser;
hqMathParser* MathParser_Create( char *MoreLetters );
void MathParser_Destroy( hqMathParser* parser );
char* MathParser_Parse( hqMathParser* parser, char *Formula, double *result );
|
/* ... creating list of variables. See StrMap reference for example */ hqMathParser* parser = MathParser_Create( NULL ); parser->Parameters = varmap; |
/* multiarg test function */
char* dummy( int paramcnt, double *args, hqStrMap *strparams, double *result );
char funcnames[] =
"DUMMY\000";
char* (*funcaddrs[])() = {
&dummy };
hqStrMap* strmap = Strmap_CreateFromChain( sizeof(void*), funcnames, funcaddrs );
hqMathParser* parser = MathParser_Create( NULL );
parser->ExtFunctions = strmap;
|
double result;
char *ErrMsg = MathParser_Parse( parser, "sin(x * x) / 2", &result );
if ( ErrMsg == NULL )
printf( "%.15G\n", result );
else {
puts( ErrMsg );
|
MathParser_Destroy( parser ); /* Don't forget to destroy lists of UDFs and UNVs if they are created before */ StrMap_Destroy( strmap ); |
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;