代码之家  ›  专栏  ›  技术社区  ›  Programmer

系统格式正确的FormatException C#

c#
  •  -1
  • Programmer  · 技术社区  · 6 年前

    我制作了一个dll来使用c#中缺少的一些函数,比如python的eval函数(这是一个非常好的函数)。下面是我的eval类的代码

    public class EvalMath
        {
            public double result { get; private set; }
    
            private delegate double Operation(double x, double y);
    
    
    
    
            public EvalMath()
            {
    
    
            }
            private static string Reverse(string s)
            {
                char[] charArray = s.ToCharArray();
                Array.Reverse(charArray);
                return new string(charArray);
            }
    
            public double Eval(string operation)
            {
                operation = Reverse(operation);
    
                Operation op = null;
                if (operation == null) throw new EvalArgumentException();
                string integer1_r = "";
                foreach (char chr in operation)
                {
                    if(chr == '+' || chr == '-' || chr == '*' || chr == '/')
                    {
                        string integer = operation.Substring(0, operation.IndexOf(chr));
    
                        string integer1 = operation.Substring(operation.IndexOf(chr) + 1);
                        if(integer1.Contains("+") || integer1.Contains("-") || integer1.Contains("*") || integer1.Contains("/"))  integer1_r = Eval(integer.ToString()).ToString();
                        switch (chr)
                        {
                            case '+':
                                op = (x, y) => x + y;
                                break;
                            case '-':
                                op = (x, y) => x - y;
                                break;
                            case '*':
                                op = (x, y) => x * y;
                                break;
                            case '/':
                                op = (x, y) => x / y;
                                break;
                        }
                        System.Windows.Forms.MessageBox.Show(integer + " " + integer1_r); // for debug
                        result = op(Convert.ToDouble(integer), Convert.ToDouble(integer1_r));
    
                        return result;
                    }
    
    
                }
                return 0.0;
            }
        }
    

    我正在尝试这个代码来测试它

    EvalMath e = new EvalMath();
    double res = e.Eval("3+3");
    
    Console.ReadLine();
    

    但每当我尝试运行代码时,它只会给我一个系统。这行有例外吗

      double res = e.Eval("3+3");
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Karan    6 年前

    在代码中添加了一行。如果integer1不包含任何运算符,您忘记设置integer1_r值。代码中添加了以下行。

    else integer1_r = integer1;
    

    下面是完整的代码。

    public class EvalMath
    {
        public double result { get; private set; }
    
        private delegate double Operation(double x, double y);
    
        public EvalMath()
        {
    
        }
        private static string Reverse(string s)
        {
            char[] charArray = s.ToCharArray();
            Array.Reverse(charArray);
            return new string(charArray);
        }
    
        public double Eval(string operation)
        {
            operation = Reverse(operation);
    
            Operation op = null;
            if (operation == null) throw new EvalArgumentException();
            string integer1_r = "";
            foreach (char chr in operation)
            {
                if(chr == '+' || chr == '-' || chr == '*' || chr == '/')
                {
                    string integer = operation.Substring(0, operation.IndexOf(chr));
    
                    string integer1 = operation.Substring(operation.IndexOf(chr) + 1);
                    if(integer1.Contains("+") || integer1.Contains("-") || integer1.Contains("*") || integer1.Contains("/"))  integer1_r = Eval(integer.ToString()).ToString();
                    else integer1_r = integer1;
    
                    switch (chr)
                    {
                        case '+':
                            op = (x, y) => x + y;
                            break;
                        case '-':
                            op = (x, y) => x - y;
                            break;
                        case '*':
                            op = (x, y) => x * y;
                            break;
                        case '/':
                            op = (x, y) => x / y;
                            break;
                    }
                    System.Windows.Forms.MessageBox.Show(integer + " " + integer1_r); // for debug
                    result = op(Convert.ToDouble(integer), Convert.ToDouble(integer1_r));
    
                    return result;
                }
    
    
            }
            return 0.0;
        }
    }