evaluate boolean string?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Coco

    #1

    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!
  • Girish Bharadwaj

    #2
    Re: evaluate boolean string?

    Coco wrote:
    [color=blue]
    > 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![/color]

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

    --
    Girish Bharadwaj

    Comment

    • Coco

      #3
      Re: evaluate boolean string?

      Thankf for the help, but i am not sure so sure how can it
      be done, can u tell me more?[color=blue]
      >-----Original Message-----
      >Coco wrote:
      >[color=green]
      >> Hi!
      >> Does c# has any class method that will be able to[/color][/color]
      evaluate[color=blue][color=green]
      >> a given string e.g ((True && False) || (True && True))[/color][/color]
      and[color=blue][color=green]
      >> return the result in boolean for this case is true
      >>
      >> Thanks![/color]
      >
      >No. But, you can implement a very simple "interprete r"[/color]
      pattern with C#[color=blue]
      >and get the behavior. Search web for Dotnet and[/color]
      patterns.. you might see[color=blue]
      >someone already implemented this stuff.
      >
      >--
      >Girish Bharadwaj
      >
      >.
      >[/color]

      Comment

      • Bret Mulvey [MS]

        #4
        Re: evaluate boolean string?

        "Coco" <anonymous@disc ussions.microso ft.com> wrote in message
        news:05b501c398 3f$5651f840$a40 1280a@phx.gbl.. .[color=blue]
        > 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![/color]

        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 BooleanEvaluato r
        {
        public static bool Evaluate(string expression)
        {
        expression =
        expression.ToLo wer(System.Glob alization.Cultu reInfo.Invarian tCulture);
        expression = expression.Repl ace("false", "0");
        expression = expression.Repl ace("true", "1");
        expression = expression.Repl ace(" ", "");
        string temp;
        do
        {
        temp = expression;
        expression = expression.Repl ace("(0)", "0");
        expression = expression.Repl ace("(1)", "1");
        expression = expression.Repl ace("0&&0", "0");
        expression = expression.Repl ace("0&&1", "0");
        expression = expression.Repl ace("1&&0", "0");
        expression = expression.Repl ace("1&&1", "1");
        expression = expression.Repl ace("0||0", "0");
        expression = expression.Repl ace("0||1", "1");
        expression = expression.Repl ace("1||0", "1");
        expression = expression.Repl ace("1||1", "1");
        }
        while (temp != expression);
        if (expression == "0")
        return false;
        if (expression == "1")
        return true;
        throw new ArgumentExcepti on("expression" );
        }
        }



        Comment

        Working...