tags:

views:

96

answers:

4

I have a string, "-(1-cos(R*T))/R", that I need to be evaluated in both C and C#. Is there a library, or a quick way to do this? Or do I need to write my own parser?

I'm also assuming that R and T are known and are local variables.

A: 

Regular Expression is what you are looking for.

Marcus Johansson
I doubt it. With regular expressions you can't even check if the number of parentheses is balanced.
dtb
On the other hand, if he's not really trying to parse an arbitrary expression, but a fixed expression with some changing constants, regular expressions might indeed be a good solution.
dtb
+2  A: 

There's one on CodeProject that's certainly worth a look. There's also a blog post from 2007 that has a list (and benchmarks) of a number, including a half dozen or so that are free.

Jerry Coffin
A: 

No, there is no eval() in C.

pmg
+2  A: 

This is in C#

 private void MyMethod1()
        {
            string s = "-(1-cos(R*T))/R";
            float R = 1;
            float T = 1;
            double doutput =-(1-Math.Cos(R*T))/(R);
        }
AsifQadri