[英]C#, How to compute an operation string
i have a string like that 我有这样的字符串
str = "4975 + 10 * (LOG(250.6)) - 321.2"
i want to compute the result of this operation. 我想计算此操作的结果。 Is there any sort way to do that?
有什么办法吗?
// my operation just includes some of operators (,), +, -, *, / , ., 0-9, LOG
// '.' is used for double number
I believe this is what you are looking for. 我相信这就是您要寻找的。
Use this library will help you to perform math operations in string format 使用此库将帮助您以字符串格式执行数学运算
Add this package 添加这个包
Install-Package DynamicExpresso.Core 安装包DynamicExpresso.Core
code example 代码示例
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();
}
result=> 4709.03858042462 结果=> 4709.03858042462
link for lib https://github.com/davideicardi/DynamicExpresso lib的链接https://github.com/davideicardi/DynamicExpresso
Also answered here: 在这里也回答:
https://stackoverflow.com/a/2859130/1043824
https://stackoverflow.com/a/2859130/1043824
Try DataTable.Compute
试试
DataTable.Compute
I do not have a .net box lying around so I cannot confirm whether it can do log
or not, but I have used it for long algebric expressions. 我周围没有.net框,因此无法确认它是否可以
log
,但是我已将其用于长代数表达式。
Basically this is how it goes: 基本上是这样的:
DataTable dt = new DataTable();
var ans = dt.compute("5 + (7 - 9) / 3");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.