如何在 C# 中的 运行 时间将字符串数学公式转换为 int

How to convent string math formula to int at run time in c#

我需要在 运行 时间在 C# 中用条件解析数学公式。例如

string code = "1+ 1 + 1*1 * ((5+4==8)?2:0 )";
int value = int.parse(code);  // value should be 2

我能够在没有任何三元运算符的情况下解析数学公式。我在使用三元运算符时遇到问题:字符串代码 = "1+ 1 + 1*1 * ((5+4==8)?2:0 )";

如有任何帮助,我们将不胜感激

@Rand 随机,@Charlieface

首先,如果你遇到一个你不懂的语言的问题,我建议你跳过它。不要创建无效声明!

int.parse();习惯于 将字符串整数转换为整数值。

但显然在这种情况下这一行 =>

int value = int.parse(code);  // value should be 2

案例有误, (因为代码不是整数)

而且要完成我要求的操作,您不需要编译器。我实际上没有它就做到了!!

    using static System.Math;
    using Project = System.IO;
    using System.Linq;
    using System.Collections.Generic;
    using System.Text.RegularExpressions; 
    class YesICanWithoutACompiler
                {
                    static void Main(string[] args)
                    {
        
                        string code = "1+ 1 + 1*1 * (((((((3!=63))?(2==4?5:1):7))==8)?2:(4==6?7:3) ))";
        
                        string formula = getFormular(code);   // outputs: 1+ 1 + 1*1 * (3)
        
                        Console.WriteLine("yes I can!: " + new System.Data.DataTable().Compute(formula, ""));
                        Console.ReadLine();
        
                    }
                    static int IndexOfAny(string source, string[] items, int startat = 0)
                    {
                        List<int> indexes = new List<int>();
                        for (int f = 0; f < items.Length; f++)
                        {
                            int loo = source.IndexOf(items[f], startat);
                            if (loo != -1)
                            {
                                indexes.Add(loo);
                            }
                        }
        
                        if (indexes.Count > 0)
                        {
                            return indexes.Min();
                        }
                        else
                        {
                            return -1;
                        }
        
        
                    }
        
        
                    static bool conditoin(int x, string mark, int y)
                    {
                        switch (mark)
                        {
                            case "!=":
                                return (x != y);
                            case "==": return (x == y);
                            case ">=": return (x >= y);
                            case "<=": return (x <= y);
                            default: return false;
                        }
        
                    }
        
                    static string getFormular(string code)
                    {
                        int ind = IndexOfAny(code, new string[] { "==", ">=", "<=", "!=" });
        
                        while (ind != -1)
                        {
        
        
        
                            int condition1 = int.Parse(string.Join("", code.Substring(0, ind).Reverse().SkipWhile(c => !char.IsDigit(c)).TakeWhile(d => char.IsDigit(d))));
                            string conditioin = code.Substring(ind, 2);
        
                            int condition2 = int.Parse(string.Join("", code.Substring(ind + 2).TakeWhile(d => char.IsDigit(d))));
        
        
                            string arvo = "";
                            if (conditoin(condition1, conditioin, condition2))
                            {
                                arvo = code.Substring(1 + code.IndexOf("?", ind + 2)).Trim();
        
                                if (arvo.StartsWith("("))
                                {
        
        
                                    int ndent = 1;
                                    int h = 0;
                                    while (h < arvo.Length && ndent != 0)
                                    {
                                        h++;
        
        
                                        if (arvo[h] == ')') ndent--;
                                        if (arvo[h] == '(') ndent++;
                                    }
        
                                    arvo = arvo.Substring(0, h + 1);
        
        
                                }
                                else
                                {
                                    arvo = string.Join("", arvo.TakeWhile(s => s != ':'));
        
                                }
        
        
        
                            }
                            else
                            {
        
                                arvo = code.Substring(1 + code.IndexOf(":", ind + 2)).Trim();
        
                                if (arvo.StartsWith("("))
                                {
        
        
                                    int ndent = 1;
                                    int h = 0;
                                    while (h < arvo.Length && ndent != 0)
                                    {
                                        h++;
        
        
                                        if (arvo[h] == ')') ndent--;
                                        if (arvo[h] == '(') ndent++;
                                    }
        
                                    arvo = arvo.Substring(0, h + 1);
        
                                }
                                else
                                {
                                    arvo = string.Join("", arvo.TakeWhile(s => s != ')'));
        
                                }
        
        
        
        
        
        
        
        
                            }
        
                            int f = code.IndexOf("?", ind);
                            int indent = 0;
                            while (f > 0 && indent != -1)
                            {
                                f--;
                                if (code[f] == ')') indent++;
                                if (code[f] == '(') indent--;
                            }
        
                            int t = f;
                            indent = 1;
        
                            while (t < code.Length && indent != 0)
                            {
                                t++;
                                if (code[t] == ')') indent--;
                                if (code[t] == '(') indent++;
                            }
        
                            code = code.Remove(f, (t - f) + 1).Insert(f, arvo);
        
                            ind = IndexOfAny(code, new string[] { "==", ">=", "<=", "!=" });
        
                        }
                        return code;
                    }

 }