473,406 Members | 2,369 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,406 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 33782
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.