Calculate the line phrase with mathematical functions using C# (Expression Evaluator)



  • Hello, everyone!

    Is there a way to calculate the mathematical expression using C#?free from external decisions and libraries?

    Example of a stroke-referencing account with mathematical functions:

    string test_s_expression = "5 + sin(0.1 * 8) - cos(0.5 - (3/4))";
    ExpressionEvaluator EE = new ExpressionEvaluator();
    float results = EE.Eval(test_s_expression);
    

    P.S. I'm making an iOS application on the movement of Unity, so the iOS code has to work.



  • I need a steaming line. The answer was slightly more than I expected, very concise, but for example, I hope you can:

    using System;
    using System.Collections.Generic;
    

    namespace ConsoleApplication1
    {
    class Program
    {
    public static float Eval(string expres)
    {
    string[] number = expres.Split("+-*/sincostg()".ToCharArray());
    var listNumber = new List<string>();

        foreach (var item in number)
        {
            if(!item.Equals("") &amp;&amp; !item.Equals(" "))
            {
                listNumber.Add(item);
            }
        }
        number = listNumber.ToArray();
    
        float result = Convert.ToSingle(number[0]);            
        string[] operation = expres.Split("0123456789()".ToCharArray());
    
        listNumber.Clear();
        foreach (var item in operation)
        {
            if (!item.Equals("") &amp;&amp; !item.Equals(" "))
            {
                listNumber.Add(item);
            }
        }
        operation = listNumber.ToArray();
    
        for(int i = 0; i &lt; operation.Length; i++)
        {
            if(!operation[i].Equals("+") &amp;&amp; !operation[i].Equals("-") 
                &amp;&amp; !operation[i].Equals("*") &amp;&amp; !operation[i].Equals("/"))
            {
                operation[i] =
                    operation[i].Remove(0, 3);
            }
        }
    
        for (int i = 0; i &lt; operation.Length; i++)
        {
            if (operation[i].Equals("+"))
            {
                result += Convert.ToSingle(number[i]);
            }
            if (operation[i].Equals("-"))
            {
                result -= Convert.ToSingle(number[i]);
            }
            if (operation[i].Equals("*"))
            {
                result *= Convert.ToSingle(number[i]);
            }
            if (operation[i].Equals("/"))
            {
                result /= Convert.ToSingle(number[i]);
            }
            if (operation[i].Equals("sin"))
            {
                if ((i != 0) &amp;&amp; operation[i - 1].Equals("+"))
                {
                    result += (float)Math.Sin(Convert.ToSingle(number[i]));
                }
                if ((i != 0) &amp;&amp; operation[i - 1].Equals("-"))
                {
                    result -= (float)Math.Sin(Convert.ToSingle(number[i]));
                }
                if ((i != 0) &amp;&amp; operation[i - 1].Equals("*"))
                {
                    result *= (float)Math.Sin(Convert.ToSingle(number[i]));
                }
                if ((i != 0) &amp;&amp; operation[i - 1].Equals("/"))
                {
                    result /= (float)Math.Sin(Convert.ToSingle(number[i]));
                }
                if(i == 0)
                {
                    result = (float)Math.Sin(Convert.ToSingle(number[i]));
                }
            }
            if (operation[i].Equals("cos"))
            {
                if ((i != 0) &amp;&amp; operation[i - 1].Equals("+"))
                {
                    result += (float)Math.Cos(Convert.ToSingle(number[i]));
                }
                if ((i != 0) &amp;&amp; operation[i - 1].Equals("-"))
                {
                    result -= (float)Math.Cos(Convert.ToSingle(number[i]));
                }
                if ((i != 0) &amp;&amp; operation[i - 1].Equals("*"))
                {
                    result *= (float)Math.Cos(Convert.ToSingle(number[i]));
                }
                if ((i != 0) &amp;&amp; operation[i - 1].Equals("/"))
                {
                    result /= (float)Math.Cos(Convert.ToSingle(number[i]));
                }
                if (i == 0)
                {
                    result = (float)Math.Cos(Convert.ToSingle(number[i]));
                }
            }
            if (operation[i].Equals("tg"))
            {
                if ((i != 0) &amp;&amp; operation[i - 1].Equals("+"))
                {
                    result += (float)Math.Tan(Convert.ToSingle(number[i]));
                }
                if ((i != 0) &amp;&amp; operation[i - 1].Equals("-"))
                {
                    result -= (float)Math.Tan(Convert.ToSingle(number[i]));
                }
                if ((i != 0) &amp;&amp; operation[i - 1].Equals("*"))
                {
                    result *= (float)Math.Tan(Convert.ToSingle(number[i]));
                }
                if ((i != 0) &amp;&amp; operation[i - 1].Equals("/"))
                {
                    result /= (float)Math.Tan(Convert.ToSingle(number[i]));
                }
                if (i == 0)
                {
                    result = (float)Math.Tan(Convert.ToSingle(number[i]));
                }
            }
        }
        return result;
    }
    
    static void Main(string[] args)
    {
        Console.WriteLine(Eval("5+5-3 * sin(11) - cos(55) / tg(20)"));
        Console.Read();
    }
    

    }
    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2