How to evaluate in C# a string of expression - Javascript
This is a discussion on How to evaluate in C# a string of expression - Javascript ; 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 ...
-
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
-
Re: How to evaluate in C# a string of expression
Try this:
textBox1.Text = (3 + 5 / 2 > 4).ToString();
textBox2.Text = ((3 + 5) / 2 > 4).ToString();
"Zeng" <Zeng5000@hotmail.com> wrote in message
news:uGksp9q#EHA.2584@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
>
>
>
-
Re: How to evaluate in C# a string of expression
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).
-
Re: How to evaluate in C# a string of expression
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" <Zeng5000@hotmail.com> schreef in bericht
news:uGksp9q%23EHA.2584@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
>
>
>
-
Re: How to evaluate in C# a string of expression
"Zeng" <Zeng5000@hotmail.com> wrote in
news:uGksp9q%23EHA.2584@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
-
Re: How to evaluate in C# a string of expression
Hello,
Take a look to the Scripter.NET at http://www.qwhale.net/products/scripter/
It's a freeware product containing method EvaluateExpression.
Regards,
Dmitry
"Zeng" <Zeng5000@hotmail.com> wrote in message
news:uGksp9q#EHA.2584@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
>
>
>
-
RE: How to evaluate in C# a string of expression
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
>
>
>
>
-
Re: How to evaluate in C# a string of expression
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
-
Re: How to evaluate in C# a string of expression
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" <Zeng5000@hotmail.com> wrote in message
news:uGksp9q%23EHA.2584@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
>
>
>
-
Re: How to evaluate in C# a string of expression
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.
Similar Threads
-
By Application Development in forum C
Replies: 6
Last Post: 11-20-2007, 05:12 PM
-
By Application Development in forum XML SOAP
Replies: 2
Last Post: 03-02-2007, 05:40 PM
-
By Application Development in forum C
Replies: 11
Last Post: 12-05-2006, 02:53 PM
-
By Application Development in forum PROLOG
Replies: 2
Last Post: 11-10-2006, 12:33 AM