简体   繁体   English

C# 从字符串计算

[英]C# calculate from string

I trying to calculate my string but I don't have any idea how to do that.我试图计算我的字符串,但我不知道该怎么做。

I want to do something like this:我想做这样的事情:

string input = "2+2+2+2*5^2/2";
Console.WriteLine(Calculate(input));

I tried to use DataTable().Compute() but its dose not support many of operators.我尝试使用DataTable().Compute()但它不支持许多运算符。

You can use DataTable.Compute for that.您可以为此使用DataTable.Compute this method computes the given expression on the current rows that pass the filter criteria.此方法计算通过过滤条件的当前行的给定表达式。

You can read more about it here: https://docs.microsoft.com/en-us/dotnet/api/system.data.datatable.compute?view=net-5.0您可以在此处阅读更多相关信息: https://docs.microsoft.com/en-us/dotnet/api/system.data.datatable.compute?view=net-5.0

string input = "2+2+2+2*5^2/2";
string res = new DataTable().Compute(input, null).ToString();

Console.WriteLine(res);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM