首页 > 解决方案 > System.FormatException 格式正确 C#

问题描述

我正在制作一个 dll 以使用 c# 中缺少的一些函数,例如 python 的 eval 函数(这绝对是 awsome )这是我的 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");

标签: c#

解决方案


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

推荐阅读