问题
Guys I have a calculation that is built by a user in a text box. In short, he defines the format he wants and the values are passed on later(after some database queries).
eg. (X + Y) * 55
or he may also bulid as
(X - 100) * 100
Is there any way of computing this with c# ?
NOTE : X and Y are retrieved from a database.
回答1:
have a look at http://NCalc.Codeplex.com
its an easy to use and extensibile calculation engine for C#
Example of how to use is :
Expression e = new Expression("2 + 3 * 5");
Debug.Assert(17 == e.Evaluate());
回答2:
Without doing it for you, you could either look here for information about using Lua in C#.
For another way you could create a system like this which creates an actual calculator. You could extend it by adding (
and )
buttons to compute those first.
来源:https://stackoverflow.com/questions/20210926/making-a-calculation-according-to-the-bodmas-in-c-sharp