Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6530197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:43:35+00:00 2026-05-25T09:43:35+00:00

Say I need to evaluate a expression as true or false starting from a

  • 0

Say I need to evaluate a expression as true or false starting from a sample input string like
True or False or ( False or True )
we suppose do not do any validation check.

Bellow we have a working sample that don’t use brackets, and I need to add the brackets support…

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = Console.ReadLine();

            // suppose the user introduces a valid input like:
            // True or False or ( False or True )
            var stringTokens = input.Split(" ".ToCharArray());

            List<Token> tokens = new List<Token>();

            foreach (string token in stringTokens)
            {
                tokens.Add(new Token(token));
            }

            bool result = Token.GetResult(tokens.ToArray());

            Console.WriteLine("The result is - {0}!", result);
            Console.ReadKey();
        }

        class Token
        {
            public readonly Linker Linker;
            public readonly bool? Value;

            public Token(string text)
            {
                switch (text.Trim().ToLower())
                {
                    case "true":
                        Value = true; break;
                    case "false":
                        Value = false; break;
                    case "(":
                        Linker = Program.Linker.LeftBracket; break;
                    case ")":
                        Linker = Program.Linker.RightBracket; break;
                    case "and":
                        Linker = Program.Linker.And; break;
                    case "or":
                        Linker = Program.Linker.Or; break;
                    default: break;
                }
            }

            public bool IsLinker
            { get { return !Value.HasValue; } }

            public static bool GetResult(params Token[] tokens)
            {
                bool result = true;

                Linker previousLinker = Linker.And;

                // this is some bull code...
                // please help
                foreach (var token in tokens)
                {
                    if (token.IsLinker)
                        previousLinker = token.Linker;
                    else
                    {
                        if (previousLinker == Linker.And)
                            result = result && token.Value.Value;
                        else if (previousLinker == Linker.And)
                            result = result || token.Value.Value;
                        else // brackets
                        {
                            previousLinker = Linker.And;
                            //result = result; // NO idea here...
                        }
                    }
                }
                return result;
            }
        }

        public enum Linker
        {
            None, And, Or,
            LeftBracket,
            RightBracket
        }
    }
}

I should mention that I can’t use the Dynamic Link Library, because I already use it… in fact, my question is strongly linked with this one: Using brackets in dynamic .NET expressions

I just want to understand how to group some booleans with brackets… Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:43:36+00:00Added an answer on May 25, 2026 at 9:43 am

    You should make GetResult function recursive.

    When your foreach loop meets Linker.LeftBracket it should call GetResult(nonProcessedTokents) without the processed tokens. And when your loop meets Linker.RightBracket it’d evaluate and return the result. This method’ll help to handle with nested brackets too. Something like that:

    GetResult(Tokens.Skip(parsedTokensCount).Take(Tokens.Length-parsedTokensCount)
    

    Or try this library: Flee.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say i have a string that i need to evaluate in javascript such as
Let's say I need to develop a plugin for a given technology not supported
Lets say we need to select two sets from a table: Things var GradeA
Let's say I need to access a web service from an iPhone app. This
Say i need to filter a generic list with a dynamic query ( List<string>
Say I need a signal to represent numbers from 0 to 5; obviously this
Say I need to populate 4 or 5 dropdowns w/ items from a database.
I need to evaluate a boolean expression. The purpose is to filter a set
Say I need some very special multiplication operator. It may be implemented in following
Say I need to call a javascript file in the <head> of an ERb

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.