469,271 Members | 1,785 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

How to evaluate in C# a string of expression

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

Nov 16 '05 #1
13 32199
Try this:

textBox1.Text = (3 + 5 / 2 > 4).ToString();
textBox2.Text = ((3 + 5) / 2 > 4).ToString();
"Zeng" <Ze******@hotmail.com> wrote in message
news:uG**************@TK2MSFTNGP09.phx.gbl...
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

Nov 16 '05 #2
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).
Nov 16 '05 #3
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" <Ze******@hotmail.com> schreef in bericht
news:uG****************@TK2MSFTNGP09.phx.gbl...
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

Nov 16 '05 #4
"Zeng" <Ze******@hotmail.com> wrote in
news:uG****************@TK2MSFTNGP09.phx.gbl...
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
Nov 16 '05 #5
Hi Zeng,
try
1. info.lundin.Math from http://www.lundin.info/
2. Complex & DateTime Parser assembly for .NET from
http://www.geocities.com/alexei_cioina/cioinaeval.html

"Zeng" wrote:
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

Nov 16 '05 #6
Zeng wrote:
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

Nov 16 '05 #7

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" <Ze******@hotmail.com> wrote in message
news:uG****************@TK2MSFTNGP09.phx.gbl...
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

Nov 16 '05 #8
Zeng wrote:
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.
Nov 16 '05 #9
Zeng wrote:
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
Nov 16 '05 #10
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

"Nicole Schenk" <ns*****@attglobal.net> wrote in message
news:GO*************@twister.socal.rr.com...
Zeng wrote:
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

Nov 16 '05 #11
Try the MS ScriptHost.

See the 'Evaluate an Expression With ScriptHost' article at:

http://getdotnetco.web101.discountas...tore/Free.aspx
--
Mike

Mike McIntyre MVP
www.getdotnetcode.com
"Zeng" <Ze******@hotmail.com> wrote in message
news:u2*************@TK2MSFTNGP09.phx.gbl...
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

"Nicole Schenk" <ns*****@attglobal.net> wrote in message
news:GO*************@twister.socal.rr.com...
Zeng wrote:
> 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


Nov 16 '05 #12
>
( ( 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.

Nov 16 '05 #13
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" <je**@lindholm.org> wrote in message
news:CJ******************@newssvr19.news.prodigy.c om...

( ( 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.

Nov 16 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by EDom | last post: by
8 posts views Thread by No Such Luck | last post: by
4 posts views Thread by netnet | last post: by
4 posts views Thread by Wilson | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.