System.FormatException以正确的格式C#

时间:2018-06-23 10:13:00

标签: c#

我正在制作一个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();

但是每当我尝试运行代码时,它只会在这一行中将System.FormatException绑住

  double res = e.Eval("3+3");

1 个答案:

答案 0 :(得分:0)

在代码中添加了一行。如果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;
    }
}