我需要创建一个用户将构建公式的UI.即:
对于一个项目公式是:
费用*项目/ 100
对于另一个项目:
项目*5/100
我希望用户能够通过web ui生成公式.
然后,当用户输入我想要计算结果的变量时.
有没有任何包或插件来做到这一点?
谢谢.
达斯·维达!
这里有几个选项,它取决于您的需求,是否需要一些非常复杂的东西或者只是简单易懂和扩展的东西(可能用于学术目的).
1)让我们从简单,容易和可定制的开始.我已经创建了一个满足您在帖子中指定的需求的类,但是它非常原始,不应该在没有进一步测试和修改的情况下用于商业项目...如果你可以轻松拿起并增加它想要...它显示了一种实现所需要的简单方法.代码很好地工作但没有考虑数学优先级(例如括号或*over +).它需要进行调整以便这样做......代码在下面,它被评论并希望自我解释:
public class DynamicFormula { ////// This simply stores a variable name and its value so when this key is found in a expression it gets the value accordingly. /// public DictionaryVariables { get; private set; } /// /// The expression itself, each value and operation must be separated with SPACES. The expression does not support PARENTHESES at this point. /// public string Expression { get; set; } public DynamicFormula() { this.Variables = new Dictionary(); } public double CalculateResult() { if (string.IsNullOrWhiteSpace(this.Expression)) throw new Exception("An expression must be defined in the Expression property."); double? result = null; string operation = string.Empty; //This will be necessary for priorities operations such as parentheses, etc... It is not being used at this point. List aux = new List (); foreach (var lexema in Expression.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)) { //If it is an operator if (lexema == "*" || lexema == "/" || lexema == "+" || lexema == "-") { operation = lexema; } else //It is a number or a variable { double value = double.MinValue; if (Variables.ContainsKey(lexema.ToLower())) //If it is a variable, let's get the variable value value = Variables[lexema.ToLower()]; else //It is just a number, let's just parse value = double.Parse(lexema); if (!result.HasValue) //No value has been assigned yet { result = value; } else { switch (operation) //Let's check the operation we should perform { case "*": result = result.Value * value; break; case "/": result = result.Value / value; break; case "+": result = result.Value + value; break; case "-": result = result.Value - value; break; default: throw new Exception("The expression is not properly formatted."); } } } } if (result.HasValue) return result.Value; else throw new Exception("The operation could not be completed, a result was not obtained."); } /// /// Add variables to the dynamic math formula. The variable should be properly declared. /// /// Should be declared as "VariableName=VALUE" without spaces public void AddVariable(string variableDeclaration) { if (!string.IsNullOrWhiteSpace(variableDeclaration)) { var variable = variableDeclaration.ToLower().Split('='); //Let's make sure the variable's name is LOWER case and then get its name/value string variableName = variable[0]; double variableValue = 0; if (double.TryParse(variable[1], out variableValue)) this.Variables.Add(variableName, variableValue); else throw new ArgumentException("Variable value is not a number"); } else { //Could throw an exception... or just ignore as it not important... } } }
以下是在WPF应用程序中使用上述类的示例(可以在任何C#应用程序中使用)
private void btCalculate_Click(object sender, RoutedEventArgs e) { string expression = tboxExpression.Text; //"cost * item / 100" (IT MUST BE SEPARATED WITH SPACES!) string variable1 = tboxVariable1.Text; //"item=10" string variable2 = tboxVariable2.Text; //"cost=2.5" DynamicFormula math = new DynamicFormula(); math.Expression = expression; //Let's define the expression math.AddVariable(variable1); //Let's add the first variable math.AddVariable(variable2); //Let's add the second variable try { double result = math.CalculateResult(); //In this scenario the result is 0,25... cost * item / 100 = (2.5 * 10 / 100) = 0,25 //Console.WriteLine("Success: " + result); tboxResult.Text = result.ToString(); } catch(Exception ex) { //Console.WriteLine(ex.Message); tboxResult.Text = ex.Message; } }
2)如果您需要更强大的东西以及大多数现实生活中的情况,您应该明确地查看FLEE:http://flee.codeplex.com/wikipage?title = Example &referringTitle = Home
这是专门为此制作的库,它支持多个公式!可能需要一些时间来查看一些示例并了解它是如何工作的,但它应该在没有太多工作的情况下完成工作.
希望能帮助到你,
LuísHenriqueGoll.