Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > any existing code for Calculation Engine (define & use formulae) ?

Reply
Thread Tools

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

 
 
Mister B
Guest
Posts: n/a
 
      03-16-2011
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
 
Reply With Quote
 
 
 
 
Fred
Guest
Posts: n/a
 
      03-16-2011
On Mar 16, 10:18*am, Mister B <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Joel C. Salomon
Guest
Posts: n/a
 
      03-16-2011
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

 
Reply With Quote
 
Roberto Waltman
Guest
Posts: n/a
 
      03-16-2011
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 ]
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      03-16-2011


"Mister B" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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[i]);
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

 
Reply With Quote
 
James Waldby
Guest
Posts: n/a
 
      03-17-2011
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
 
Reply With Quote
 
Dann Corbit
Guest
Posts: n/a
 
      03-21-2011
In article <d644dcb0-44ee-4563-a259-c6cdd924d946
@glegroupsg2000goo.googlegroups.com>, http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
Gene
Guest
Posts: n/a
 
      04-01-2011
On Mar 16, 1:18*pm, Mister B <(E-Mail Removed)> 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


 
Reply With Quote
 
Gene
Guest
Posts: n/a
 
      04-01-2011
On Mar 16, 1:18*pm, Mister B <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Existing Dll - using Functions from an existing dll Tristin.Colby@gmail.com Ruby 0 02-05-2008 07:38 PM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
Why no existing Java type to existing XML schema binding support? nrm Java 3 04-10-2006 04:52 PM
Using MABLE logic engine with existing .NET applications. Al Fatykhov ASP .Net 0 02-14-2006 01:03 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM



Advertisments