0

How to find the result of the math equation given as a string in C#? Ex: "2+5*3+7" should return 3 and "(2+5)*(3+7)" should return 70... just any math equation. I tried splitting the string into Substring based on the position of the operator but failed.

int strStart = 0, strLength = 0, result =0, no=0;

List<char> ops = new List<char>();
        foreach (char c in inputString)
        {
            if (IsOperator(c))
            {
                strLength = inputString.IndexOf(c) - strStart;
                Console.WriteLine("String Length = " + strLength);
                if (ops.Count == 1)
                    result = Convert.ToInt32(inputString.Substring(strStart, strLength));
                else
                {                        
                    no = Convert.ToInt32(inputString.Substring(strStart, strLength));
                    result = Math(result, no, c);
                }
                strStart = strStart + strLength + 1;
            }                 

        }

no = Convert.ToInt32(inputString.Substring(strStart, strLength));
        result = Math(result, no, op);

Somehow i keep messing up on the strLength variable. Then I tried this

List<char> operators = new List<char>();
            foreach (char c in stringValue)
            {
                 try
                 {
                    int i = Convert.ToInt32(c.ToString());
                 }
                catch
                 {
                    operators.Add(c);
                 }
            }
            int totalOperators = operators.Count;
            string[] stringValues = new string[totalOperators + 1];
            int[] indexes = new int[totalOperators];
            for(int j=0;j<totalOperators;j++)
                indexes[j] = stringValue.IndexOf(operators.ElementAt(0));
            stringValues[0] = stringValue.Substring(0, indexes[0]) ;
            for(int k=1;k<totalOperators;k++)
            {
                stringValues[k] = stringValue.Substring(indexes[k - 1], indexes[k] - indexes[k - 1]);
            }
            stringValues[totalOperators] = stringValue.Substring(indexes[totalOperators - 1], stringValue.Length- indexes[totalOperators - 1]);

            int[] intValues = new int[totalOperators + 1];
            for (int j = 0; j <= totalOperators; j++)
                intValues[j] = Convert.ToInt32(stringValues[j]);
            int result = intValues[0];
            for (int k = 0; k < totalOperators; k++)
            {
                char c = operators.ElementAt(k);
                switch (c)
                {
                    case '+':
                        result += intValues[k + 1];
                        break;

                }

            }

it failed for addition, so i didn't even add the other cases, kindly help me out. Is there any class/library available to simplify the process?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
imthath
  • 1,353
  • 1
  • 13
  • 35
  • 1
    You could google one of those millions of Expression Evaluators in .NET/C#. – Uwe Keim Feb 07 '18 at 19:33
  • If you're writing such code like in second part of the post you need to think about how other developer will be understanding your creature and after delete it all. – cortisol Feb 07 '18 at 19:34

0 Answers0