Click here to Skip to main content
15,906,094 members
Home / Discussions / C#
   

C#

 
GeneralRe: About password Pin
Heath Stewart25-Jun-04 3:38
protectorHeath Stewart25-Jun-04 3:38 
Generalboolean expression evaluation Pin
narada10824-Jun-04 5:12
narada10824-Jun-04 5:12 
GeneralRe: boolean expression evaluation Pin
Dave Kreskowiak24-Jun-04 5:47
mveDave Kreskowiak24-Jun-04 5:47 
GeneralRe: boolean expression evaluation Pin
narada10824-Jun-04 5:52
narada10824-Jun-04 5:52 
GeneralRe: boolean expression evaluation Pin
Heath Stewart24-Jun-04 6:02
protectorHeath Stewart24-Jun-04 6:02 
GeneralRe: boolean expression evaluation Pin
Heath Stewart24-Jun-04 6:12
protectorHeath Stewart24-Jun-04 6:12 
GeneralRe: boolean expression evaluation Pin
Dave Kreskowiak24-Jun-04 8:03
mveDave Kreskowiak24-Jun-04 8:03 
GeneralRe: boolean expression evaluation Pin
Karl 200027-Jun-04 11:51
Karl 200027-Jun-04 11:51 
narada108,

There is no class in .net to do what you would like. However, depending on the format of your Boolean expression this may not be a difficult task. This task can be broken down into two easier problems. Convert the expression to a postfix expression, and then evaluate the postfix expression.

Typically a Boolean expression looks like this "1 and 1". This is called infix notation since the operator is between the operands. The postfix equivalent to this expression would be "1 1 and". In postfix notation the operator comes after the operands. A more complicated example follows:

infix: "1 and 1 and not 0 or 0"
postfix: "1 1 0 not and and 0 or"

It looks difficult but you get used to it really fast. The main advantage of writing the expressions this way is that they are easy to evaluate. How to evaluate a postfix expression is covered in many introductory computer science classes. The following code will evaluate an expression in this format.

<br />
		private bool EvaluatePostfixExpression(string expression)<br />
		{<br />
			string[] pieces = expression.Split(new char[]{' '},100);<br />
<br />
			System.Collections.Stack stack = new Stack();<br />
			<br />
			foreach(string cur in pieces)<br />
			{<br />
				if (cur == "0")<br />
					stack.Push(false);<br />
				else if (cur == "1")<br />
					stack.Push(true);<br />
				else if (cur == "and")<br />
				{<br />
					bool second = (bool)stack.Pop();<br />
					bool first = (bool)stack.Pop();<br />
					stack.Push(first && second);<br />
				}<br />
				else if (cur == "or")<br />
				{<br />
					bool second = (bool)stack.Pop();<br />
					bool first = (bool)stack.Pop();<br />
					stack.Push(first || second);<br />
				}<br />
				else if (cur == "not")<br />
				{<br />
					stack.Push(!(bool)stack.Pop());<br />
				}<br />
					<br />
			}<br />
<br />
			return (bool)stack.Pop();<br />
<br />
		}<br />


For example EvaluatePostfixExpression("1 1 0 not and and 0 or") will return true;

I would recommend trying to get your Boolean expressions in postfix notation. If you can't then another simple stack based algorithm can be used to convert infix notation to postfix notation. I don't feel like writing that algorithm at the moment, so let me know if you go this way and need help with it. You can probably find these algorithms in a book on data structures.

Karl Baum
CEO of KGB Technologies
Specializing in custom software development.
GeneralRe: boolean expression evaluation Pin
Anonymous27-Jun-04 20:49
Anonymous27-Jun-04 20:49 
GeneralC# Open Dialog Box Pin
HyVong24-Jun-04 3:21
HyVong24-Jun-04 3:21 
GeneralRe: C# Open Dialog Box Pin
Heath Stewart24-Jun-04 5:09
protectorHeath Stewart24-Jun-04 5:09 
GeneralRe: C# Open Dialog Box Pin
HyVong24-Jun-04 5:18
HyVong24-Jun-04 5:18 
GeneralRe: C# Open Dialog Box Pin
Heath Stewart24-Jun-04 5:45
protectorHeath Stewart24-Jun-04 5:45 
GeneralRe: C# Open Dialog Box Pin
HyVong24-Jun-04 5:49
HyVong24-Jun-04 5:49 
GeneralRe: C# Open Dialog Box Pin
HyVong25-Jun-04 3:25
HyVong25-Jun-04 3:25 
Generalquery builer Pin
Member 114126624-Jun-04 2:35
Member 114126624-Jun-04 2:35 
GeneralMS Project 2003 Pin
frankvgb24-Jun-04 1:41
frankvgb24-Jun-04 1:41 
GeneralRe: MS Project 2003 Pin
Heath Stewart24-Jun-04 6:23
protectorHeath Stewart24-Jun-04 6:23 
Questionserver name? Pin
quocbao24-Jun-04 1:19
quocbao24-Jun-04 1:19 
AnswerRe: server name? Pin
Heath Stewart24-Jun-04 6:37
protectorHeath Stewart24-Jun-04 6:37 
Questionserver name? Pin
quocbao24-Jun-04 1:18
quocbao24-Jun-04 1:18 
QuestionHow to write a Async UDP socket code? Pin
fu023-Jun-04 20:53
fu023-Jun-04 20:53 
AnswerRe: How to write a Async UDP socket code? Pin
Heath Stewart24-Jun-04 6:41
protectorHeath Stewart24-Jun-04 6:41 
GeneralRe: How to write a Async UDP socket code? Pin
fu024-Jun-04 22:15
fu024-Jun-04 22:15 
GeneralRe: How to write a Async UDP socket code? Pin
Heath Stewart25-Jun-04 3:41
protectorHeath Stewart25-Jun-04 3:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.