Click here to Skip to main content
15,861,172 members
Articles / Programming Languages / C#
Tip/Trick

Validate an InFix expression using C#

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
10 Mar 2010CPOL 14.3K   2   1
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

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

    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
Working on .NET technologies(windows, web, database, reporting, intergration services etc).

Comments and Discussions

 
GeneralThere is one mistake you made in this program which is givin... Pin
Suvy241-Dec-11 18:29
Suvy241-Dec-11 18:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.