Logical processing of multiple (long) True/False strings

68 views Asked by At

Good day everyone.

I'm calling a function on strings which are up to 2000 char long and look like

cB = "-(1/0)+((1+1)/-(0+0+1)+1)"
cB = "0+(1/0/1)+(1/0/0/0/0/1)"
cB = "1/1/1/1/1/0/0/01/1/0/01/1/00/0/0/1/1/0/0/10"

with:

1: TRUE 0: FALSE +: logical AND /: logical OR -: logical NOT

As you can see, it is strictly mathematical with brackets first, then logical AND and lastly logical OR

The function is:

String cB = cdb.Value
for (int m = 0; m < 10; m++)
        {
            if (cB.Length == 1)
            {
                break;
            }
            else
            {
                cB = cB.Replace("01", "0").Replace("10", "0").Replace("11", "1").Replace("00", "0");
                cB = cB.Replace("-0", "1").Replace("-1", "0");
                cB = cB.Replace("(0)", "0").Replace("(1)", "1").Replace("(-0)", "1").Replace("(-1)", "0");
                for (int n = 0; n < 10; n++)
                {
                    if (!cB.Contains("+"))
                    {
                        break;
                    }
                    cB = cB.Replace("0+0", "0").Replace("1+1", "1").Replace("0+1", "0").Replace("1+0", "0");
                    cB = cB.Replace("(0)", "0").Replace("(1)", "1").Replace("(-0)", "1").Replace("(-1)", "0");
                }
                for (int p = 0; p < 10; p++)
                {
                    if (!cB.Contains("/"))
                    {
                        break;
                    }
                    cB = cB.Replace("0/0", "0").Replace("1/1", "1").Replace("0/1", "1").Replace("1/0", "1");
                    cB = cB.Replace("(0)", "0").Replace("(1)", "1").Replace("(-0)", "1").Replace("(-1)", "0");
                }
            }
        }
        if (cB == "1")
        {
            result = 1;
        }
        else if (cB == "0")
        {
            result = 0;
        }
        else
        {
            result = 9;
        }

The desired outcome of the three string given would be: 0 0 1

My problem is, that the function is really slow. So I'm asking you to help me please :)

I forgot to mention that I am an absolute beginner in C#

0

There are 0 answers