how to convert a string to a mathematical expression programmatically

后端 未结 5 1992
时光说笑
时光说笑 2020-12-20 23:32

I am a beginner at C#. I am facing a problem while converting a string to a mathematical expression. I have a UI where user can create formula using random formula field. An

5条回答
  •  感情败类
    2020-12-21 00:00

    string input= "(12 + 4 * 6) * ((2 + 3 * ( 4 + 2 ) ) ( 5 + 12 ))";       
        string str4 = "(" + input`enter code here`.Replace(" ", "") + ")";
                str4 = str4.Replace(")(", ")*(");
                while (str4.Contains('('))
                {
                    string sub1 = str4.Substring(str4.LastIndexOf("(") + 1);
                    string sub = sub1.Substring(0, sub1.IndexOf(")"));
                    string sub2 = sub;
                    string str21 = sub2.Replace("^", "~^~").Replace("/", "~/~").Replace("*", "~*~").Replace("+", "~+~").Replace("-", "~-~");
                    List str31 = str21.Split('~').ToList();
                    while (str31.Count > 1)
                    {
                        while (str31.Contains("*"))
                        {
                            for (int i = 0; i < str31.Count; i++)
                            {
                                if (str31[i] == "*")
                                {
                                    val = Convert.ToDouble(str31[i - 1]) * Convert.ToDouble(str31[i + 1]);
                                    str31.RemoveRange(i - 1, 3);
                                    str31.Insert(i - 1, val.ToString());
                                }
                            }
                        }
                        while (str31.Contains("/"))
                        {
                            for (int i = 0; i < str31.Count; i++)
                            {
                                if (str31[i] == "/")
                                {
                                    val = Convert.ToDouble(str31[i - 1]) / Convert.ToDouble(str31[i + 1]);
                                    str31.RemoveRange(i - 1, 3);
                                    str31.Insert(i - 1, val.ToString());
                                }
                            }
                        }
                        while (str31.Contains("+"))
                        {
                            for (int i = 0; i < str31.Count; i++)
                            {
                                if (str31[i] == "+")
                                {
                                    val = Convert.ToDouble(str31[i - 1]) + Convert.ToDouble(str31[i + 1]);
                                    str31.RemoveRange(i - 1, 3);
                                    str31.Insert(i - 1, val.ToString());
                                }
                            }
                        }
                        while (str31.Contains("-"))
                        {
                            for (int i = 0; i < str31.Count; i++)
                            {
                                if (str31[i] == "-")
                                {
                                    val = Convert.ToDouble(str31[i - 1]) - Convert.ToDouble(str31[i + 1]);
                                    str31.RemoveRange(i - 1, 3);
                                    str31.Insert(i - 1, val.ToString());
                                }
                            }
                        }
                    }
                    str4 = str4.Replace("(" + sub + ")", str31[0].ToString());
                }
    
                string sum = str4;
    

提交回复
热议问题