C#,如何计算操作字符串

Trư*_*Anh 0 c#

我有一个这样的字符串

str = "4975 + 10 * (LOG(250.6)) - 321.2"
Run Code Online (Sandbox Code Playgroud)

我想计算这个操作的结果.有没有办法做到这一点?

// my operation just includes some of operators (,), +, -, *, / , ., 0-9, LOG
// '.' is used for double number
Run Code Online (Sandbox Code Playgroud)

小智 7

我相信这就是你要找的东西.

使用此库将帮助您以字符串格式执行数学运算

添加此包

安装包DynamicEDpresso.Core

代码示例

public static void Main(string[] args)
        {
            var interpreter = new Interpreter();
            var result = interpreter.Eval("4975 + 10 * (LOG(250.6)) - 321.2".Replace("LOG", "Math.Log"));

            Console.WriteLine("result=> " + result);
            Console.ReadKey();
        }
Run Code Online (Sandbox Code Playgroud)

result => 4709.03858042462

lib的链接https://github.com/davideicardi/DynamicExpresso