marcin(a)studio4plus.com

libsexpr - usage example

For clarity error checking has been omitted.

#include <stdio.h>

#include "sexpr.h"

main ()
{
        char *text;
        int i;
        eTree_t *expression;
        eVariables_t variables;
        eValue_t value;

        /* text of the expression entered here */
        text = "x^2 + y^2";

        variables = eInitVar();      /* create empty list of variables */
        expression = eParse(&variables,
                        text,
                        NULL,  /* this is related to error checking... */
                        NULL,  /* ...skipped in this example */
                        1);    /* add new variables? yes */

        for(i = 0; i < eGetNumVars(variables); i++) {
                printf("Enter value of variable %s: ", eGetVarName(variables, i));
                scanf("%lf", eGetVarPtr(variables, i));
        }

        /* evaluate expression */
        printf("Computed value: %f\n", eEval(&variables, expression));

        /* cleaning */
        eTreeDestroy(expression);
        eDestroyVar(variables);

        return 0;
};
2005