How to evaluate in C# a string of expression

  • Thread starter Thread starter Zeng
  • Start date Start date
Z

Zeng

Hello,


Please help!!! I've been stuck on this issue for months. I just wonder if
there is a way to programmatically evaluate expression
strings such as


( ( 3 + 5 ) / 2 ) > 4 --> this should return 0 or false( ( 3 + 6 ) / 3 ) >
( ( 5 + 3 ) / 4 ) --> this should return 1 or trueThanks for any comment or
advice.

There was an old post fro Scott Alen but I couldn't follow what he refered
to to make it happens. Currently I have to parse and evaluate the
expression myself and it's messy and very slow. Thanks for any comment and
advise.

Zeng
 
Try this:

textBox1.Text = (3 + 5 / 2 > 4).ToString();
textBox2.Text = ((3 + 5) / 2 > 4).ToString();
 
I can think of at least three ways to do this without rolling your own
interpreter.

(a) You can invoke the C# compiler on it and compile into memory. This
approach requires you to be extremely confortable with reflection in order
to instantiate and talk to the object you create.

(b) You can host the Windows Scripting Host engine and use javascript or
VBScript (or perl or whatever rings your bell) to parse and evaluate
expressions.

(c) You can pay me to write you a component that does (a) or (b).
 
Zeng,

I still did not try this one.

\\\Eval by Nigel Amstrong
1. Create a file called: DynamicMath.js
2. Add this code to it:

class DynamicMath
{
static function Eval(MathExpression : String) : double
{
return eval(MathExpression);

};
}

3. Compile it with the command line jsc compiler: jsc /t:library
DynamicMath.js
4. Add a reference to DynamicMath.dll to your project (and to
Microsoft.JScript.dll as well)
5. Use from your favourite .NET language:
Dim d As Double = DynamicMath.Eval("2 + 3 + 4")
MessageBox.Show(d)
6. That's it..
///

Maybe you can do

Cor
 
Zeng said:
Hello,


Please help!!! I've been stuck on this issue for months. I just wonder
if
there is a way to programmatically evaluate expression
strings such as


( ( 3 + 5 ) / 2 ) > 4 --> this should return 0 or false( ( 3 + 6 ) / 3 )
( ( 5 + 3 ) / 4 ) --> this should return 1 or trueThanks for any comment
or
advice.

There was an old post fro Scott Alen but I couldn't follow what he refered
to to make it happens. Currently I have to parse and evaluate the
expression myself and it's messy and very slow. Thanks for any comment
and
advise.

If you're only trying to evaluate simple mathematical expressions, building
your own parser might be an option: Most parser generators (like ANTLR) come
with a "calc"-sample already, it's usually not much work to add one or two
operators.

Niki
 
Zeng said:
Hello,


Please help!!! I've been stuck on this issue for months. I just wonder if
there is a way to programmatically evaluate expression
strings such as


( ( 3 + 5 ) / 2 ) > 4 --> this should return 0 or false( ( 3 + 6 ) / 3 ) >
( ( 5 + 3 ) / 4 ) --> this should return 1 or trueThanks for any comment or
advice.

There was an old post fro Scott Alen but I couldn't follow what he refered
to to make it happens. Currently I have to parse and evaluate the
expression myself and it's messy and very slow. Thanks for any comment and
advise.

Zeng


Hi Zeng,

You can do this with the DotBasic runtime, which is completely free.

In the download package there is a CSharp source file showing how to do
inline evaluation.

Here are the relevent lines:

....
using DotBasicRuntime;
private Runtime runtime;
....
runtime = new Runtime();
object result = runtime.Eval(inputText.Text);
....

Testing the examples you posted:

runtime.Eval("( ( 3 + 5 ) / 2 ) > 4") returned False

runtime.Eval("( ( 3 + 6 ) / 3 ) > ( ( 5 + 3 ) / 4 )") returned True


The Eval statement can handle any legal DotBasic expression or program.
http://dotbasicscript.com

Regards,

-- Peter Fisk
 
I think the easiest way to do is to create a jscript.net class with eval
function and use jsc.exe to compile it as library dll and then reference it
in your C# project.

John
 
Zeng said:
Hello,


Please help!!! I've been stuck on this issue for months. I just wonder
if there is a way to programmatically evaluate expression
strings such as


( ( 3 + 5 ) / 2 ) > 4 --> this should return 0 or false( ( 3 + 6 ) / 3 )
( ( 5 + 3 ) / 4 ) --> this should return 1 or trueThanks for any comment
or advice.

There was an old post fro Scott Alen but I couldn't follow what he refered
to to make it happens. Currently I have to parse and evaluate the
expression myself and it's messy and very slow. Thanks for any comment
and advise.

Zeng
Look in the compiler access. It is possible to compile on the fly.
 
Zeng said:
Hello,


Please help!!! I've been stuck on this issue for months. I just wonder
if there is a way to programmatically evaluate expression
strings such as


( ( 3 + 5 ) / 2 ) > 4 --> this should return 0 or false( ( 3 + 6 ) / 3 )
( ( 5 + 3 ) / 4 ) --> this should return 1 or trueThanks for any comment
or advice.

There was an old post fro Scott Alen but I couldn't follow what he refered
to to make it happens. Currently I have to parse and evaluate the
expression myself and it's messy and very slow. Thanks for any comment
and advise.

Zeng
Another easier solution:
1. make an ascx file with language="jscript"
2. make a function in this that says evaluate with one parameter sExpression
the expression as a string
3. in the function say
return eval(sExpression);

That should do it.
Thanks
 
Thanks for replying, just to update, I have tried my computations through
javascript by writing a js file and compile it then link my main app to the
dll. It's very slow, slower than my current slow method. Probably because j
script has to go through many translation layers before it can crank out the
result. I have to look at a different way to deal with it maybe a another
way to parse it in C#

zeng
 
( ( 3 + 5 ) / 2 ) > 4 --> this should return 0 or false( ( 3 + 6 ) / 3 )
( ( 5 + 3 ) / 4 ) --> this should return 1 or trueThanks for any comment
or
advice.

There was an old post fro Scott Alen but I couldn't follow what he refered
to to make it happens. Currently I have to parse and evaluate the
expression myself and it's messy and very slow. Thanks for any comment
and
advise.
Here is the test I did with information from here
----- Math.js -----------
class JsMath
{
static function Eval(MathExpression : String) : double
{
return eval(MathExpression);
};
}
------ Test.cs ----------
using System;
using System.Text;

class Test
{
static void Main(string[] args)
{
System.Console.WriteLine(JsMath.Eval("(1 + 2) * 8"));
}
}
 
I have seen a couple of other creative solutions as well,
but this solution i came up with, because it did the work, it was kind of
efficient, and i needed to write no code.

public static double Evaluate ( string expression )
{
// That is some code instruction, is'nt it? :)
return (double) new System.Xml.XPath.XPathDocument
( new StringReader("<r/>")).CreateNavigator().Evaluate
( string.Format("number({0})", new
System.Text.RegularExpressions.Regex(@"([\+\-\*])")
.Replace(expression, " ${1} ")
.Replace("/", " div ")
.Replace("%", " mod ") ) );
}


HTH.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Jeff Lindholm said:
( ( 3 + 5 ) / 2 ) > 4 --> this should return 0 or false( ( 3 + 6 ) /
3 ) >
( ( 5 + 3 ) / 4 ) --> this should return 1 or trueThanks for any comment
or
advice.

There was an old post fro Scott Alen but I couldn't follow what he
refered
to to make it happens. Currently I have to parse and evaluate the
expression myself and it's messy and very slow. Thanks for any comment
and
advise.
Here is the test I did with information from here
----- Math.js -----------
class JsMath
{
static function Eval(MathExpression : String) : double
{
return eval(MathExpression);
};
}
------ Test.cs ----------
using System;
using System.Text;

class Test
{
static void Main(string[] args)
{
System.Console.WriteLine(JsMath.Eval("(1 + 2) * 8"));
}
}
----------------------
jsc /t:library Math.js
csc -r:Microsoft.JScript.dll -r:Math.dll test.cs

change test.cs as needed for what you want.
 
Back
Top