none
Best way to check that arithmetic-expression is validate RRS feed

  • Question

  • i found a link https://sites.google.com/site/akshatsharma80/home/data-structures/validate-an-arithmetic-expression

    which has a function which can tell me expression is valid or not. just tell me below routine is perfect one or should i use other?

    .net has anything in-built ? tell me few best to achieve this. thanks

    static void Main(string[] args)
    
    {
    
    ArrayList arrexpressions = new ArrayList();
    
    arrexpressions.Add("a+b-c");
    
    arrexpressions.Add("a+b-c+");
    
    arrexpressions.Add("a++b-c");
    
    arrexpressions.Add("a+(b-c)");
    
    arrexpressions.Add("a+(b-c)*(d-e)");
    
    arrexpressions.Add("a+((b-c)*(d-e))");
    
    arrexpressions.Add("a+((b-c)/(d*e)+(f/g))");
    
    arrexpressions.Add("a+((((b-c)*d)*s)*q)");
    
    arrexpressions.Add("a+(b-cc)");
    
      foreach (string s1 in arrexpressions)
    
      {
    
         string s = s1;
    
         s = "(" + s + ")";
    
         s = s.Replace(" ", string.Empty);
    
         Console.WriteLine("Exp : " + s);
    
         Console.WriteLine(validate(s) == true ? "true" : "false");
    
         Console.WriteLine();
    
      }
    
    Console.Read();
    
    }
    
     
     
            public static bool validate(string expression)
    
            {
    
                int previous = 0;
    
                int previous1 = 0;
    
                string expEvaluated = string.Empty;
    
                int operatorOperand = 1;
    
                for (int i = 0; i < expression.Length; i++)
    
                {
    
                    char c = expression[i];
    
                    if (c == ')')
    
                    {
    
                    }
    
                    else if (c == '(')
    
                    {
    
                        int j = expression.IndexOf(')', i);
    
                        if (j == -1)
    
                            return false;
    
                        string substring = expression.Substring(i + 1, j - i - 1);
    
                        while (getcharactercount(substring, '(') != getcharactercount(substring, ')'))
    
                        {
    
                            if (j < expression.Length - 1)
    
                                j = expression.IndexOf(')', j + 1);
    
                            else
    
                                break;
    
                            substring = expression.Substring(i + 1, j - i - 1);
    
                        } i = j - 1; //Changing the counter i to point to the next character                   
    
                        //validating the sub expression                   
    
                        if (validate(substring) == true)
    
                        {
    
                            if (previous != 0 && previous1 != 0 && previous > previous1)
    
                            {
    
                                previous1 = operatorOperand;
    
                                operatorOperand++;
    
                                previous = 0;
    
                            }
    
                            else if (previous != 0 && previous1 != 0 && previous <= previous1)
    
                            {
    
                                return false;
    
                            }
    
                            else if (previous1 != 0)
    
                            {
    
                                return false;
    
                            }
    
                            else
    
                            {
    
                                previous1 = operatorOperand;
    
                                operatorOperand++;
    
                            }
    
                        }
    
                        else
    
                        {
    
                            return false;
    
                        }
    
                    }
    
                    else if (c == '+' || c == '-' || c == '*' || c == '/')
    
                    {
    
                        if (previous != 0)
    
                        {
    
                            return false;
    
                        }
    
                        previous = operatorOperand;
    
                        operatorOperand++;
    
                    }
    
                    else
    
                    {
    
                        if (previous != 0 && previous1 != 0 && previous > previous1)
    
                        {
    
                            previous1 = operatorOperand;
    
                            operatorOperand++;
    
                            previous = 0;
    
                        }
    
                        else if (previous != 0 && previous1 != 0 && previous <= previous1)
    
                        {
    
                            return false;
    
                        }
    
                        else if (previous1 != 0)
    
                        {
    
                            return false;
    
                        }
    
                        else
    
                        {
    
                            previous1 = operatorOperand;
    
                            operatorOperand++;
    
                        }
    
                    }
    
                }
    
                if (previous != 0)
    
                    return false;
    
                return true;
    
            }
    
     
     
    public static int getcharactercount(string exp, char _c)
    
    {
    
    int count = 0;
    
    foreach (char c in exp)
    
    {
    
    if (c == _c)
    
    count++;
    
    }
    
    return count;
    
    }

    Saturday, March 16, 2019 6:45 PM

All replies

  • .net has anything in-built ? tell me few best to achieve this. 

    You can use MSScriptControl.ScriptControl then Eval(string Expression)

    (add COM reference "Microsoft Script Control 1.0")

    I did a few tests with invalid expressions and it returns "syntax error" (with try/catch)


    • Edited by Castorix31 Saturday, March 16, 2019 8:28 PM
    Saturday, March 16, 2019 8:25 PM