Click here to Skip to main content
Click here to Skip to main content

Tagged as

Validate an InFix expression using C#

, 10 Mar 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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;
        }
 
    }
}

License

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

Share

About the Author

Lokesh Lal
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... PinmemberSuvy241-Dec-11 18:29 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 10 Mar 2010
Article Copyright 2010 by Lokesh Lal
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid