Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 472

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 112

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 112

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/jerdew5/orbworks.com/forum/includes/bbcode.php on line 112
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3368: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3370: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3371: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 3372: Cannot modify header information - headers already sent by (output started at /includes/bbcode.php:472)
OrbWorks Community Forum • View topic - Need math expression parser

Need math expression parser

Discuss OrbForms Designer and PocketC Architect

Postby Rudi on Wed Mar 28, 2007 7:48 am

Hi,
this problem ist not easy for me an so i searched the internet for ansi-c sourcecodes to "spy" al little bit.

I need following function for a Freeware project:

float math_expr(string);
...
float res1, res2;
res1 = math_expr("1*(2.1 + 3.3)/10");
res2 = math_expr("(1.1+2.2)/(10.55-2.2)";
...
Because the compiler must have this feature (it must compile eg. A=3.3*2.2+1.0) i hope there is a possible solution to "integrate" an expressionparser into orbforms.

If not, is there anybody who own or know a source for a math_expression in Ansi-C or (better) in Orbforms?

Best regards,
Rudi
Rudi
 
Posts: 33
Joined: Thu Feb 12, 2004 2:42 pm
Location: Germany

Postby dewey on Thu Mar 29, 2007 4:06 am

The runtime doesn't have any expression evaluation - this is only in the compiler. Luckily, I actually wrote a simple expression evaluator long ago when I was doing runtime performance work.

Code: Select all
enum { TOK_END, TOK_NUM, TOK_OP };
struct Expr {
  float eval(string expr, int* piError);
 
  // expression state
  string es;
  int ies;
 
  // current token
  char op;
  float num;
  int tok;
 
  // internal methods
  void nextToken();
  float doExpr();
  float doAdd();
  float doMult();
  float doPrimary();
};

void Expr.nextToken() {
  char c;
  int istart;
  while (es[ies] == ' ') ies++;
  c = es[ies];
  if (c == '\0') {
    tok = TOK_END;
    return;
  }
  istart = ies;
  if (c >= '0' && c <= '9') {
    while (c >= '0' && c <= '9' || c == '.') {
      c = es[++ies];
    }
    num = (float)substr(es, istart, ies - istart);
    tok = TOK_NUM;
    return;
  }
  op = es[ies++];
  tok = TOK_OP;
}

float Expr.doPrimary() {
  float res;
  if (tok == TOK_NUM) {
    res = num;
    nextToken();
  }
  else if (tok == TOK_OP && op == '(') {
    nextToken();
    res = doExpr();
    if (op == ')')
      nextToken();
  }
  return res;
}

float Expr.doMult() {
  float val, right;
  char lastOp;
  val = doPrimary();
  while (tok == TOK_OP && (op == '*' || op == '/')) {
    lastOp = op;
    nextToken();
    right = doPrimary();
    if (lastOp == '*')
      val *= right;
    else
      val /= right;
  }
  return val;
}

float Expr.doAdd() {
  float val, right;
  char lastOp;
  val = doMult();
  while (tok == TOK_OP && (op == '+' || op == '-')) {
    lastOp = op;
    nextToken();
    right = doMult();
    if (lastOp == '+')
      val += right;
    else
      val -= right;
  }
  return val;
}

float Expr.doExpr() {
  float res;
  res = doAdd();
  return res;
}

float Expr.eval(string expr, int* piError) {
  float res;
  *piError = 0;
  es = expr;
  ies = 0;
  nextToken();
  res = doExpr();
  if (tok != TOK_END)
    *piError = ies;
  return res;
}


To use it:
Code: Select all
Expr e;
int iError; // the position of the error, if any
float result;

result = e.eval("5.2 * (1 + 1.4)", &iError);
if (iError) alert("error at position: " + iError);
else alert("result: " + result);
dewey
Site Admin
 
Posts: 749
Joined: Sun Oct 21, 2007 5:12 am

Postby Rudi on Tue Apr 03, 2007 3:45 am

Wow,
thank you --- very easy to understanding code :) and ready to integrate various new functions in this expr-body procedures.

THANKS!!!!
Rudi
Rudi
 
Posts: 33
Joined: Thu Feb 12, 2004 2:42 pm
Location: Germany


Return to OrbForms Designer and PocketC Architect

Who is online

Users browsing this forum: No registered users and 2 guests

cron