473,218 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 software developers and data experts.

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 33737
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Stefan Mueller | last post by:
I'd like to set a variable called 'FocusIsOn' if a button got the focus. Because my button is dynamically created I do it like xelement = document.createElement("input") xelement.type = "button"...
3
by: Jason luo | last post by:
Hi all, In c99-standard page 52,there is a sentence about void,as below: If an expression of any other type is evaluated as a void expression, its value or designator is discarded. I don't...
2
by: EDom | last post by:
Hi, I have expressions in a string like "100/24*5" and so on. How can I evaluate this expression to get the result. Regards, Vineet
2
by: Ray Martin | last post by:
I have created a custom datagridcolumn, and would like to color the cell based on criteria passed as a parm when the column is created. I would like this to be generic so I want to do something...
8
by: No Such Luck | last post by:
Is there anyway to literally evaluate the contents of a string in an if statement? For example: int i = 0; char * str = "i == 0"; if(str) /* I know this doesn't do what I want */ {
4
by: netnet | last post by:
Is it possible to evaluate a string as a method name? Something like this: Session = "loadFormFormACompany"; This is what I would like to do.... eval(Session+"()") Anyone have any ideas?
4
by: Wilson | last post by:
Hello, How can i eval a string expression like String abc = ??"dt.Rows.Columns.ToString() + dt.Rows.Columns.ToString()" Thanks Wilson
2
by: parul.prasad | last post by:
How can we evaluate a string expression in if statement Linux C/C++
2
by: yarborg | last post by:
This is kind of a weird one and hard to find answers online because of the format of the question. Essentially I want to be able to have a string that looks like this "True AND True AND True" and...
1
by: Monusonu | last post by:
Hi Expert, I am trying to get the value from excel formula cell using POI. My code works fine for less complex formula cells, but fails or returns error code for complex formula cells. Following...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.