Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. boolean expression evaluation

boolean expression evaluation

Scheduled Pinned Locked Moved C#
csharpquestion
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    narada108
    wrote on last edited by
    #1

    Is there any .net class I can use to obtain a boolean value given a string that represents an expression (a boolean expression)?????

    D H K 4 Replies Last reply
    0
    • N narada108

      Is there any .net class I can use to obtain a boolean value given a string that represents an expression (a boolean expression)?????

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Your looking to get an expression evaluated, contained in a String? There is nothing in the .NET BCL that does this. You'll have to come up with either a 3rd party component to do this for you, or you'll have to write an expression evaluation library yourself. There is an example of this on CodeProject, but since there is a link problem going to CodeProject's site, I can't search for it in a reasonable amount of time. Just search the articles for "expression parse". RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      N 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Your looking to get an expression evaluated, contained in a String? There is nothing in the .NET BCL that does this. You'll have to come up with either a 3rd party component to do this for you, or you'll have to write an expression evaluation library yourself. There is an example of this on CodeProject, but since there is a link problem going to CodeProject's site, I can't search for it in a reasonable amount of time. Just search the articles for "expression parse". RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        N Offline
        N Offline
        narada108
        wrote on last edited by
        #3

        Thanks a lot. I was hoping there could be a class:(

        1 Reply Last reply
        0
        • N narada108

          Is there any .net class I can use to obtain a boolean value given a string that represents an expression (a boolean expression)?????

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          Like all primitives, Boolean has a Parse method for that very reason. You can also use Convert.ToBoolean (which uses Parse, but provides a little more abstract access to passing any primitive type (and a few other value types) to ToBoolean (though, in that case, few actually can be parsed into a bool). [EDIT] Oops, I didn't read that quite right. Nevermind; though this is still handy information if you didn't know it already. [/EDIT]

          Microsoft MVP, Visual C# My Articles

          1 Reply Last reply
          0
          • N narada108

            Is there any .net class I can use to obtain a boolean value given a string that represents an expression (a boolean expression)?????

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            There are a couple ways you could do this, including one way in the BCL (though not easy). The first is to reference the Microsoft.JScript.dll assembly and use the Eval class. In JScript .NET, this would be equivalent of the eval method (also in Javascript and JScript, though those aren't compiled languages nor do they target the CLR). I believe I saw an article on this some time back on MSDN. I could be wrong, but you could try searching for it or just give it a try. The other way is to use Reflection Emit or the CodeDOM. Basically, you create a class that contains those variables and contains a method which evaluates those variables and returns a bool. Read Emitting Dynamic Assemblies[^] in the .NET Framework SDK for more information about Reflection Emit and Generating and Compiling Source Code Dynamically in Multiple Languages[^] for more information on CodeDOM.

            Microsoft MVP, Visual C# My Articles

            D 1 Reply Last reply
            0
            • H Heath Stewart

              There are a couple ways you could do this, including one way in the BCL (though not easy). The first is to reference the Microsoft.JScript.dll assembly and use the Eval class. In JScript .NET, this would be equivalent of the eval method (also in Javascript and JScript, though those aren't compiled languages nor do they target the CLR). I believe I saw an article on this some time back on MSDN. I could be wrong, but you could try searching for it or just give it a try. The other way is to use Reflection Emit or the CodeDOM. Basically, you create a class that contains those variables and contains a method which evaluates those variables and returns a bool. Read Emitting Dynamic Assemblies[^] in the .NET Framework SDK for more information about Reflection Emit and Generating and Compiling Source Code Dynamically in Multiple Languages[^] for more information on CodeDOM.

              Microsoft MVP, Visual C# My Articles

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              Heath Stewart wrote: The other way is to use Reflection Emit or the CodeDOM. Basically, you create a class that contains those variables and contains a method which evaluates those variables and returns a bool. :eek: All that for "?1+1=2". Seems kind of like having a nuclear power plant to power your USB notebook fan. I'd rather smash my head into a board of nails than go through that! RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

              1 Reply Last reply
              0
              • N narada108

                Is there any .net class I can use to obtain a boolean value given a string that represents an expression (a boolean expression)?????

                K Offline
                K Offline
                Karl 2000
                wrote on last edited by
                #7

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

                A 1 Reply Last reply
                0
                • K Karl 2000

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

                  A Offline
                  A Offline
                  Anonymous
                  wrote on last edited by
                  #8

                  Thanks so much

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups