自动计算数学字符串在C#中表达 [英] automatically calculate string mathematical expressing in C#

查看:122
本文介绍了自动计算数学字符串在C#中表达的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#字符串,它是一个数学表达式的表示,这样的事情:

I have a C# string which is representation of a mathematical expression, something like this:

A = (B*0.25)+(D*0.25)+(1-W)

我怎么能计算的价值?我应该手动解析所有的字符,然后执行我的计算?虽然这是可能的,但我认为这将是耗时写这样的代码,有没有什么办法,C#能明白这串并计算(价值当然我会宣布A,B,D&放大器; W--为变量

how can I calculate value of A? should I manually parse all characters, then perform my calculation? Although it is possible but I think it would be time-consuming to write such a code, is there any way that C# can understand this string and calculate value of A (of course I would declare A, B, D & W as variables

感谢

推荐答案

有没有这样的事情。网本身,但如果你只有主要业务可以解析输入字符串,并找到相关的值,这个你可以使用调车场算法或逆波​​兰式,还可以使用的 NCalc 更复杂的场景。

There isn't such a thing in .net itself, but if you have just primary operations you can parse input string and find related values, for this you can use shunting yard algorithm or reverse polish notation, but also you can use NCalc for more complicated scenarios.

这篇关于自动计算数学字符串在C#中表达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆