Hello!

Is there a way to calculate a string expression with mathematical functions using C # ( without using third-party solutions and libraries )?

An example of counting a string expression 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); 

PS I’m doing an iOS app on the Unity engine, so when I’m running on iOS, the code should work.

  • probably parse the string and use the standard math library? - Sleeeper

1 answer 1

Need parsing string. The answer turned out to be somewhat more than I expected, rather clumsy, but for example, I hope you will fit:

 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("") && !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("") && !item.Equals(" ")) { listNumber.Add(item); } } operation = listNumber.ToArray(); for(int i = 0; i < operation.Length; i++) { if(!operation[i].Equals("+") && !operation[i].Equals("-") && !operation[i].Equals("*") && !operation[i].Equals("/")) { operation[i] = operation[i].Remove(0, 3); } } for (int i = 0; i < 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) && operation[i - 1].Equals("+")) { result += (float)Math.Sin(Convert.ToSingle(number[i])); } if ((i != 0) && operation[i - 1].Equals("-")) { result -= (float)Math.Sin(Convert.ToSingle(number[i])); } if ((i != 0) && operation[i - 1].Equals("*")) { result *= (float)Math.Sin(Convert.ToSingle(number[i])); } if ((i != 0) && 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) && operation[i - 1].Equals("+")) { result += (float)Math.Cos(Convert.ToSingle(number[i])); } if ((i != 0) && operation[i - 1].Equals("-")) { result -= (float)Math.Cos(Convert.ToSingle(number[i])); } if ((i != 0) && operation[i - 1].Equals("*")) { result *= (float)Math.Cos(Convert.ToSingle(number[i])); } if ((i != 0) && 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) && operation[i - 1].Equals("+")) { result += (float)Math.Tan(Convert.ToSingle(number[i])); } if ((i != 0) && operation[i - 1].Equals("-")) { result -= (float)Math.Tan(Convert.ToSingle(number[i])); } if ((i != 0) && operation[i - 1].Equals("*")) { result *= (float)Math.Tan(Convert.ToSingle(number[i])); } if ((i != 0) && 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(); } } }