469,612 Members | 2,079 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

evaluate boolean string?

Hi!
Does c# has any class method that will be able to evaluate
a given string e.g ((True && False) || (True && True)) and
return the result in boolean for this case is true

Thanks!
Nov 15 '05 #1
3 7679
Coco wrote:
Hi!
Does c# has any class method that will be able to evaluate
a given string e.g ((True && False) || (True && True)) and
return the result in boolean for this case is true

Thanks!


No. But, you can implement a very simple "interpreter" pattern with C#
and get the behavior. Search web for Dotnet and patterns.. you might see
someone already implemented this stuff.

--
Girish Bharadwaj

Nov 15 '05 #2
Thankf for the help, but i am not sure so sure how can it
be done, can u tell me more?
-----Original Message-----
Coco wrote:
Hi!
Does c# has any class method that will be able to evaluate a given string e.g ((True && False) || (True && True)) and return the result in boolean for this case is true

Thanks!
No. But, you can implement a very simple "interpreter"

pattern with C#and get the behavior. Search web for Dotnet and patterns.. you might seesomeone already implemented this stuff.

--
Girish Bharadwaj

.

Nov 15 '05 #3
"Coco" <an*******@discussions.microsoft.com> wrote in message
news:05****************************@phx.gbl...
Hi!
Does c# has any class method that will be able to evaluate
a given string e.g ((True && False) || (True && True)) and
return the result in boolean for this case is true

Thanks!


Here's a simple class that can do it. It's not very efficient--for better
speed and memory use you'd want to use something else. Look up "infix
evaluation" for some more ideas. But this one is short and sweet and it's
fun to watch the expression cave in on itself each time through the loop.
You can easily extend it to include ==, != and other operators.

class BooleanEvaluator
{
public static bool Evaluate(string expression)
{
expression =
expression.ToLower(System.Globalization.CultureInf o.InvariantCulture);
expression = expression.Replace("false", "0");
expression = expression.Replace("true", "1");
expression = expression.Replace(" ", "");
string temp;
do
{
temp = expression;
expression = expression.Replace("(0)", "0");
expression = expression.Replace("(1)", "1");
expression = expression.Replace("0&&0", "0");
expression = expression.Replace("0&&1", "0");
expression = expression.Replace("1&&0", "0");
expression = expression.Replace("1&&1", "1");
expression = expression.Replace("0||0", "0");
expression = expression.Replace("0||1", "1");
expression = expression.Replace("1||0", "1");
expression = expression.Replace("1||1", "1");
}
while (temp != expression);
if (expression == "0")
return false;
if (expression == "1")
return true;
throw new ArgumentException("expression");
}
}

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Bagger Vance | last post: by
6 posts views Thread by Michael G | last post: by
22 posts views Thread by Paminu | last post: by
13 posts views Thread by TJS | last post: by
reply views Thread by KenRoy | last post: by
3 posts views Thread by willempie | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.