none
splitting operand and operator RRS feed

  • Question

  • hi I have a question, actually I want to get a Boolean expression as a input and I want to split it by two variable

    like temp1 will have all the operands, and temp2 will have all operators, and I want to do the process based on the operators count. here I attached my small piece of code

    string str = "A*B+C+D";string tmp1=string.Empty;string tmp2=string.Empty;
                for (int i = 0; i < str.Length; i++)
                {
                    if (!isOperators(str[i]))
                    {

                        tmp1[i]=str[i];

                    }
                    else
                        tmp2[i]=str[i];
                }

    when I tried to store string[I] in tmp1 its showing error.

    could anyone help me to cure this.

    whereas isOperators is a function which identify the operands.

    static bool isOperators(char c)
            {
                return (!(c >= 'a' && c <= 'z') &&
                        !(c >= '0' && c <= '9') &&
                        !(c >= 'A' && c <= 'Z'));
            }

    even I tried this using queue concept.


    Thursday, March 14, 2019 11:51 AM

Answers

  • Hello,

    Perhaps this will work for you, the results of sbx can be seen as a string via sbx.ToString()

    public void Demo()
    {
        string str = "A*B+C+D";
    
        var sb1 = new StringBuilder();
        var sb2 = new StringBuilder();
    
        for (int i = 0; i < str.Length; i++)
        {
            if (!isOperators(str[i]))
            {
                sb1.Append(str[i]) ;
            }
            else
                sb2.Append(str[i]);
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, March 14, 2019 1:29 PM
    Moderator