any existing code for Calculation Engine (define & use formulae) ?

Discussion in 'C Programming' started by Mister B, Mar 16, 2011.

  1. Mister B

    Mister B Guest

    To avoid reinventing the wheel, I was wondering if anyone knows of existingsoftware (in C or libraries that could be called from C code on Linux platform) that implements a Calculation Engine, whereby formulae can be definedwith a number of inputs, and then triggered to produce an output.

    For example:
    formula 1: a + b - c
    formula 2: a * ( b + c + d ) / 3
    formula 3: a * sqrt (b)

    Value A uses formula 1 with inputs X, Y, Z
    Value B uses formula 3 with inputs M, N
    etc

    TIA
    Mark
     
    Mister B, Mar 16, 2011
    #1
    1. Advertising

  2. Mister B

    Fred Guest

    On Mar 16, 10:18 am, Mister B <> wrote:
    > To avoid reinventing the wheel, I was wondering if anyone knows of existing software (in C or libraries that could be called from C code on Linux platform) that implements a Calculation Engine, whereby formulae can be defined with a number of inputs, and then triggered to produce an output.
    >
    > For example:
    > formula 1:   a + b - c
    > formula 2:   a * ( b + c + d ) / 3
    > formula 3:   a * sqrt (b)
    >
    > Value A uses formula 1 with inputs X, Y, Z
    > Value B uses formula 3 with inputs M, N
    > etc
    >


    What do you mean by "Value A" and "Value B" ?
    --
    Fred K
     
    Fred, Mar 16, 2011
    #2
    1. Advertising

  3. Mark Bergman wrote:
    > To avoid reinventing the wheel, I was wondering if anyone knows of
    > existing software (in C or libraries that could be called from C code on
    > Linux platform) that implements a Calculation Engine, whereby formulae
    > can be defined with a number of inputs, and then triggered to produce an
    > output.


    See <http://en.wikipedia.org/wiki/Hoc_(programming_language)>, which includes links to several extended versions available.

    —Joel
     
    Joel C. Salomon, Mar 16, 2011
    #3
  4. Mister B wrote:

    > ... existing software (in C or libraries that could be called from C code on Linux platform) that implements a Calculation Engine
    > ...


    I think what you call "Calculation Engine", usually goes by the name
    of "Spreadsheet".
    I don't know off the top of my head if there is a simple C library
    available (I'll let you do the search,I would expect to find a few),
    but, for what it is worth, earlier Turbo Pascal version came with a
    spreadsheet demo in source form, and there is SIAG. (Scheme)
    Neither one should be too difficult to translate to C.
    --
    Roberto Waltman

    [ Please reply to the group.
    Return address is invalid ]
     
    Roberto Waltman, Mar 16, 2011
    #4
  5. Mister B

    BartC Guest

    "Mister B" <> wrote in message
    news:...
    > To avoid reinventing the wheel, I was wondering if anyone knows of
    > existing software (in C or libraries that could be called from C code on
    > Linux platform) that implements a Calculation Engine, whereby formulae can
    > be defined with a number of inputs, and then triggered to produce an
    > output.
    >
    > For example:
    > formula 1: a + b - c
    > formula 2: a * ( b + c + d ) / 3
    > formula 3: a * sqrt (b)
    >
    > Value A uses formula 1 with inputs X, Y, Z
    > Value B uses formula 3 with inputs M, N
    > etc


    This is not clear; where do the formulae come from? Are they input by the
    user, exist in a file (both in text format), or hardcoded in the program
    (possibly as text too)?

    What determines which formula is applied?

    Doing this properly is a lot of work, as you say, but having an attempt
    might be a useful exercise, and could make it easier to choose an
    appropriate library once the ins and outs are known.

    I had a go at something myself; this code has formulae and inputs hardcoded
    (this is just user-input and not difficult). I use C for the 'engine', by
    generating a short C program to apply the formula. (The new program prints
    it's output; normally you'd need a way of getting the result back into the
    calling program).

    You need to change the line system("run.bat") into something that compiles
    and runs the output of this program, on your machine.

    In practice, this sort of approach wouldn't use C as the output format, it's
    more likely to be a soft language, and one that can be closely integrated
    into C (Python or Lua perhaps), so that the result can be picked up via some
    interface.

    #include <stdio.h>
    #include <stdlib.h>

    #define outfile "formula.c"

    char* formulae[]={
    "a+b-c",
    "a*(b+c+d)/3",
    "a*sqrt(b)"};

    double evaluate(char* formula, double* params,int nparams){
    FILE* f;
    int i;

    f=fopen(outfile,"w");

    fprintf(f,"#include<stdio.h>\n");
    fprintf(f,"#include<stdlib.h>\n");
    fprintf(f,"#include<math.h>\n");
    fprintf(f,"\n");

    fprintf(f,"double eval(");
    for (i=0; i<nparams; ++i){
    fprintf(f,"double %c",'a'+i);
    if (i<(nparams-1)) fprintf(f,",");
    }
    fprintf(f,") {\n");

    fprintf(f,"double x;\n");

    fprintf(f,"x=%s;\n",formula);

    fprintf(f,"printf(\"Result is: %%f\",x);\n");

    fprintf(f,"}\n");
    fprintf(f,"\n");

    fprintf(f,"int main(void) {\n");
    fprintf(f,"eval(");
    for (i=0; i<nparams; ++i){
    fprintf(f,"%f",params);
    if (i<(nparams-1)) fprintf(f,",");
    }

    fprintf(f,");\n");
    fprintf(f,"}\n");

    fclose(f);

    return 0.0;
    }

    int main(void){
    double args[]={23,34,18};

    evaluate(formulae[0],args,3);

    system("run.bat");

    }

    --
    Bartc
     
    BartC, Mar 16, 2011
    #5
  6. Mister B

    James Waldby Guest

    Re: any existing code for Calculation Engine (define & useformulae) ?

    On Wed, 16 Mar 2011 14:53:24 -0400, Joel C. Salomon wrote:
    > Mark Bergman wrote:
    >> To avoid reinventing the wheel, I was wondering if anyone knows of
    >> existing software (in C or libraries that could be called from C code
    >> on Linux platform) that implements a Calculation Engine, whereby
    >> formulae can be defined with a number of inputs, and then triggered to
    >> produce an output.

    >
    > See <http://en.wikipedia.org/wiki/Hoc_(programming_language)>, which
    > includes links to several extended versions available.


    That may be a good choice. Depending on preferences, OP also might
    find <http://en.wikipedia.org/wiki/Comparison_of_parser_generators>
    of interest, and also (eg)
    <http://www.codeproject.com/KB/recipes/MathieuMathParser.aspx>

    --
    jiw
     
    James Waldby, Mar 17, 2011
    #6
  7. Mister B

    Dann Corbit Guest

    In article <d644dcb0-44ee-4563-a259-c6cdd924d946
    @glegroupsg2000goo.googlegroups.com>, says...
    >
    > To avoid reinventing the wheel, I was wondering if anyone knows of existing software (in C or libraries that could be called from C code on Linux platform) that implements a Calculation Engine, whereby formulae can be defined with a number of inputs, and then triggered to produce an output.
    >
    > For example:
    > formula 1: a + b - c
    > formula 2: a * ( b + c + d ) / 3
    > formula 3: a * sqrt (b)
    >
    > Value A uses formula 1 with inputs X, Y, Z
    > Value B uses formula 3 with inputs M, N
    > etc


    Perhaps you are interested in this:
    18.14: I need code to parse and evaluate expressions.

    A: Two available packages are "defunc," posted to comp.sources.misc
    in December, 1993 (V41 i32,33), to alt.sources in January, 1994,
    and available from sunsite.unc.edu in
    pub/packages/development/libraries/defunc-1.3.tar.Z, and
    "parse," at lamont.ldgo.columbia.edu. Other options include the
    S-Lang interpreter, available via anonymous ftp from
    amy.tch.harvard.edu in pub/slang, and the shareware Cmm ("C-
    minus-minus" or "C minus the hard stuff"). See also questions
    18.16 and 20.6.

    There is also some parsing/evaluation code in _Software
    Solutions in C_ (chapter 12, pp. 235-55).

    In general, when you want some software to perform a task, a sourceforge
    search is a good place to start, along with googlecode, etc.
     
    Dann Corbit, Mar 21, 2011
    #7
  8. Mister B

    Gene Guest

    On Mar 16, 1:18 pm, Mister B <> wrote:
    > To avoid reinventing the wheel, I was wondering if anyone knows of existing software (in C or libraries that could be called from C code on Linux platform) that implements a Calculation Engine, whereby formulae can be defined with a number of inputs, and then triggered to produce an output.
    >
    > For example:
    > formula 1:   a + b - c
    > formula 2:   a * ( b + c + d ) / 3
    > formula 3:   a * sqrt (b)
    >
    > Value A uses formula 1 with inputs X, Y, Z
    > Value B uses formula 3 with inputs M, N
    > etc
    >
    > TIA
    > Mark
     
    Gene, Apr 1, 2011
    #8
  9. Mister B

    Gene Guest

    On Mar 16, 1:18 pm, Mister B <> wrote:
    > To avoid reinventing the wheel, I was wondering if anyone knows of existing software (in C or libraries that could be called from C code on Linux platform) that implements a Calculation Engine, whereby formulae can be defined with a number of inputs, and then triggered to produce an output.
    >
    > For example:
    > formula 1:   a + b - c
    > formula 2:   a * ( b + c + d ) / 3
    > formula 3:   a * sqrt (b)
    >
    > Value A uses formula 1 with inputs X, Y, Z
    > Value B uses formula 3 with inputs M, N
    > etc


    Mark,

    The term for what you're looking for is an expression evaluator. When
    I Google for "expression evaluator in c", this pops up very near the
    top of the list:

    http://www.codeproject.com/KB/library/expreval.aspx

    On the other hand, writing a little expression evaluator is a
    wonderful task for learning. See any good compiler book and the terms
    "operator precedence parser" and/or recursive descent parser.

    You can also use a parser generator. See the documentation for GNU
    Bison. They use an expression evaluator as a tutorial example. And
    Bison generates C.
     
    Gene, Apr 1, 2011
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nrm
    Replies:
    3
    Views:
    535
  2. Mars
    Replies:
    10
    Views:
    499
  3. nnp
    Replies:
    0
    Views:
    257
  4. Geeta Sachdeva
    Replies:
    4
    Views:
    136
    bbiker
    Oct 16, 2007
  5. Replies:
    0
    Views:
    192
Loading...

Share This Page