我需要创建一个用户界面,用户将建立一个公式。即:
对于一个项目公式是:
成本 * 项目/100
对于另一个项目:
项目* 5/100
我希望用户能够通过网络用户界面生成公式。
然后当用户输入我想要计算结果的变量时。
是否有任何软件包或插件可以做到这一点?
谢谢。
最佳答案
达斯维德!
这里有几个选项,这取决于您的需求以及您是需要非常复杂的东西还是简单易懂和扩展的东西(可能出于学术目的)。
1) 让我们从简单、容易和可定制开始。我已经创建了一个满足您在帖子中指定的需求的类,但是它非常原始,不应该在没有进一步测试和修改的情况下用于商业项目......如果您可以轻松地选择它并增加它想要...它显示了一种实现所需内容的简单方法。该代码运行良好,但未考虑数学优先级(例如括号或 * 优于 +)。它需要进行调整才能这样做...... 代码在下面,它被注释并希望能够 self 解释:
public class DynamicFormula
{
/// <summary>
/// This simply stores a variable name and its value so when this key is found in a expression it gets the value accordingly.
/// </summary>
public Dictionary<string, double> Variables { get; private set; }
/// <summary>
/// The expression itself, each value and operation must be separated with SPACES. The expression does not support PARENTHESES at this point.
/// </summary>
public string Expression { get; set; }
public DynamicFormula()
{
this.Variables = new Dictionary<string, double>();
}
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<double> aux = new List<double>();
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.");
}
/// <summary>
/// Add variables to the dynamic math formula. The variable should be properly declared.
/// </summary>
/// <param name="variableDeclaration">Should be declared as "VariableName=VALUE" without spaces</param>
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=Examples&referringTitle=Home
这是一个专门为此制作的库,它支持多种公式! 可能需要一些时间才能看到一些示例并理解它是如何工作的,但它应该可以在不做太多工作的情况下完成工作。
希望对你有帮助,
路易斯·恩里克·戈尔。
关于c# - 创建动态公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34138314/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
如何使用RSpec::Core::RakeTask初始化RSpecRake任务?require'rspec/core/rake_task'RSpec::Core::RakeTask.newdo|t|#whatdoIputinhere?endInitialize函数记录在http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#initialize-instance_method没有很好的记录;它只是说:-(RakeTask)initialize(*args,&task_block)AnewinstanceofRake
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
我正在阅读SandiMetz的POODR,并且遇到了一个我不太了解的编码原则。这是代码:classBicycleattr_reader:size,:chain,:tire_sizedefinitialize(args={})@size=args[:size]||1@chain=args[:chain]||2@tire_size=args[:tire_size]||3post_initialize(args)endendclassMountainBike此代码将为其各自的属性输出1,2,3,4,5。我不明白的是查找方法。当一辆山地自行车被实例化时,因为它没有自己的initialize方法
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha
我正在尝试按0-9和a-z的顺序创建数字和字母列表。我有一组值value_array=['0','1','2','3','4','5','6','7','8','9','a','b','光盘','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','','u','v','w','x','y','z']和一个组合列表的数组,按顺序,这些数字可以产生x个字符,比方说三个list_array=[]和一个当前字母和数字组合的数组(在将它插入列表数组之前我会把它变成一个字符串,]current_combo['0','0','0']