65.9K
CodeProject is changing. Read more.
Home

Validate an InFix expression using C#

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (1 vote)

Mar 9, 2010

CPOL
viewsIcon

14543

I have written a small code to validate infix expression with basic arithmetic operations (+ ,- ,* ,/ ). As i was stuck validating the infix expression, so i thought it might be usefull for others as well.here is the codeusing System;using System.Collections.Generic;using...

I have written a small code to validate infix expression with basic arithmetic operations (+ ,- ,* ,/ ). As i was stuck validating the infix expression, so i thought it might be usefull for others as well. here is the code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ValidateInFixExpression
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList arr = new ArrayList();
            arr.Add("a+b-c");
            arr.Add("a+b-c+");
            arr.Add("a++b-c");
            arr.Add("a+(b-c)");
            arr.Add("a+(b-c)*(d-e)");
            arr.Add("a+((b-c)*(d-e))");
            arr.Add("a+((b-c)/(d*e)+(f/g))");
            arr.Add("a+((((b-c)*d)*s)*q)");
            arr.Add("a+(b-cc)");
           
            foreach (string s1 in arr)
            {
                string s = s1;
                s = "(" + s + ")";
                s = s.Replace(" ", string.Empty);
                Console.WriteLine("Exp : " + s);
                Console.WriteLine(validate(s) == true ? "true" : "false");
                Console.WriteLine();

            }
            Console.ReadLine();


            Console.ReadLine();
        }


        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;
        }

    }
}