MathEvaluator解析器

时间:2013-02-28 19:25:08

标签: c# .net wpf

怎么样?但是我需要一个解析器,它只解析一次字符串并且有一个像这样的接口

Func<double,double> MathEvaluator(string input)

或者像这样

LambdaExpression MathEvaluator(string input)

它存在吗?

3 个答案:

答案 0 :(得分:2)

我想引用this帖子,引用结果,这些结果由RickOliverMax的答案提供。

您需要深思熟虑,最符合您的要求。例如。 Math Parser .NET支持^用于功能而其他功能不支持。

答案 1 :(得分:1)

所以我做了一个lib,如果你想,你可以使用它,我认为这很好。有一些正则表达式,将结构替换为^ b到Math.Pow(a,b)等等。它可能会更好,但对我来说这很难,但我正在研究它。

using System;
using System.CodeDom.Compiler;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using Microsoft.CSharp;

namespace MathEvalNS
{
public class MathEvaluator
{
    private readonly Delegate parsedFunction;
    private readonly string normalized;
    public double Invoke(double x)
    {
        if (parsedFunction == null)
            throw new NullReferenceException("No function to invoke");
        return (double)parsedFunction.DynamicInvoke(x);
    }
    private const string Begin =
        @"using System;
namespace MyNamespace
{
    public static class LambdaCreator 
    {
        public static Func<double,double> Create()
        {
            return (x)=>";
        private const string End = @";
        }
    }
}";

    public MathEvaluator(string input)
    {
        normalized = Normalize(input);
        var provider = new CSharpCodeProvider();
        var parameters = new CompilerParameters { GenerateInMemory = true };
        parameters.ReferencedAssemblies.Add("System.dll");
        CompilerResults results = provider.CompileAssemblyFromSource(parameters, Begin + normalized + End);
        try
        {
            var cls = results.CompiledAssembly.GetType("MyNamespace.LambdaCreator");
            var method = cls.GetMethod("Create", BindingFlags.Static | BindingFlags.Public);
            parsedFunction = (method.Invoke(null, null) as Delegate);
        }
        catch (FileNotFoundException)
        {
            throw new ArgumentException();
        }
    }

    private string Normalize(string input)
    {
        return input.ReplaceMath().ReplacePow().ReplaceMultipling().ReplaceToDoubles();
    }
}

public static class StringHelper
{
    public static string ReplaceMultipling(this string input)
    {
        return Regex.Replace(input, @"(\d+)(x)", @"$1*$2");
    }

    public static string ReplacePow(this string input)
    {
        var result = input.ReplacePow(@"(\d*x)\^(\d+\.?\d*)");
        return result.ReplacePow(@"\(([^\^]+)\)\^(\d+\.?\d*)");
    }

    private static string ReplacePow(this string input, string toReplace)
    {
        return Regex.Replace(input, toReplace, "Math.Pow($1,$2)");
    }

    public static string ReplaceToDoubles(this string input)
    {
        return Regex.Replace(input, @"(\d+)(?:[^\.]\d+)", "$1.0");
    }

    public static string ReplaceMath(this string input)
    {
        return
            input.ReplaceMath("sin", @"Math.Sin")
                 .ReplaceMath("cos", @"Math.Cos")
                 .ReplaceMath("ctg", @"1.0/Math.Tan")
                 .ReplaceMath("tg", @"Math.Tan");
    }

    private static string ReplaceMath(this string input, string name, string dotNetName)
    {
        return Regex.Replace(input, name, dotNetName, RegexOptions.IgnoreCase);
    }
}
}

答案 2 :(得分:0)

我有一个MathParser:http://github.com/MathewSachin/MathParser

我希望你想要的就是这样:

Router.route('/lti', { where: 'server' }).post(function() {
    provider.valid_request(request, function(error, valid) {
        if (valid) {
            this.response.writeHead(302, { Location: '/' });
        } else {
            this.response.writeHead(403);
        }
    });
    return this.response.end();
});

我唯一担心的是垃圾收集。