473,396 Members | 1,789 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,396 software developers and data experts.

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

Similar topics

1
by: Bagger Vance | last post by:
The code below does not work -- but is there a way to do this? I want to both read a line and string sline; StreamReader m_streamReader = new StreamReader(fi); ...
6
by: Michael G | last post by:
Hi, $PHONETIC is an array of strings. How can !$PHONETIC, in the case below, evalutae to true. It is a string not a boolean. It would make more sense if it evalutated to null. Thanks Mike ...
22
by: Paminu | last post by:
As I remember if(1) evaluates to true and all other numbers including 0 evaluate to false. But where do I find out about this for sure?? I have looked through K&R, all the C for dummies books...
13
by: TJS | last post by:
How can I evaluate this string in vb.net " Dim submenu0 As New skmMenu.MenuItem('Home', '') " I have tried this with skmmenu as a reference in the compile command but assembly will not...
0
by: KenRoy | last post by:
I want to be able to evaluate a string value as either True or False. So if the string was "0 Or (1 or 0)" the value True would be returned. My ultimate goal would be to have a statement similar...
3
by: willempie | last post by:
hi all, in FoxPro we know a function EVALUATE(xxxx) . This function evaluates the contents of xxxx. for example EVALUATE("X>10") will give True if X is greater then 10. is there a such...
4
by: Jim Florence | last post by:
Hello, I've just started in ASP and I'm having a few teething problems. Initially I tried to write out dates from the database using <asp:Label runat="server" ID="Label6" Text='<%#...
2
kadghar
by: kadghar | last post by:
Many people asks if there is a way to write a mathematical expression, writen as a string in a text box, so they can do something like: sub something_click() textbox2.text=eval(textbox1.text)...
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...

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.