本文整理汇总了C#中NCalc.Expression.Evaluate方法的典型用法代码示例。如果您正苦于以下问题:C# Expression.Evaluate方法的具体用法?C# Expression.Evaluate怎么用?C# Expression.Evaluate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NCalc.Expression
的用法示例。
在下文中一共展示了Expression.Evaluate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UnaryEvaluate
public static IEnumerable<double> UnaryEvaluate(this string expression, string parameterName, Func<double[]> parameterFunc) {
expression.ShouldNotBeWhiteSpace("expression");
var expr = new NCalc.Expression(expression, EvaluateOptions.IgnoreCase | EvaluateOptions.IterateParameters);
expr.Parameters["x"] = parameterFunc();
return ((IList)expr.Evaluate()).Cast<object>().Select(result => result.AsDouble());
}
示例2: Test1
public void Test1()
{
var expr = new Expression("cos([t])", EvaluateOptions.IgnoreCase);
expr.Parameters["t"] = 0;
var result = expr.Evaluate();
Assert.AreEqual(1d, result);
expr.Parameters["t"] = Math.PI;
result = expr.Evaluate();
Assert.AreEqual(-1d, result);
}
示例3: DynamicParametersTest
public void DynamicParametersTest() {
var expr = new Expression("Round(Pow(Pi, 2) + Pow(Pi, 2) + x, 2)");
expr.Parameters["Pi2"] = new Expression("Pi * Pi");
expr.Parameters["x"] = 10;
expr.EvaluateParameter += delegate(string name, ParameterArgs args) {
if(name == "Pi")
args.Result = Math.PI;
};
Console.WriteLine("value = " + expr.Evaluate().AsDouble());
expr.Evaluate().AsDouble().Should().Be.GreaterThan(0);
}
示例4: HandleMessage
public void HandleMessage(string command, string args, object clientData, Action<string, AnswerBehaviourType> sendMessageFunc)
{
Expression expr = new Expression(args);
var exprAnswer = expr.Evaluate();
string messageAuthor = string.Empty;
string answer = string.Empty;
var skypeData = clientData as ISkypeData;
if (skypeData!=null)
{
messageAuthor = skypeData.FromName;
}
if (!string.IsNullOrEmpty(messageAuthor))
{
answer = string.Format("Мсье {0}, ответ равен : {1}", messageAuthor, exprAnswer);
}
else
{
answer = string.Format("Ответ равен : {0}", exprAnswer);
}
sendMessageFunc(answer, AnswerBehaviourType.Text);
}
示例5: Loading
/// <summary>
/// Setts up the Arcade.
/// </summary>
/// <param name='screen'>
/// Screen.
/// </param>
public static void Loading(int arcadeOrArendelle)
{
Console.Title = " ";
Console.Clear ();
if (arcadeOrArendelle == 0) {
string loadingText = "A R C A D E I D E " + InterfaceView.ArcadeVersion () + " ";
string show = "";
for (int i = 0; i < loadingText.Length; i++) {
Console.Clear ();
show = show + loadingText[i].ToString();
InterfaceView.CenterWriter (-1, show);
System.Threading.Thread.Sleep (50);
}
} else {
InterfaceView.CenterWriter (-1, "L O A D I N G ");
}
// running a very easy problem with NCalc
// so the runtime loads NCalc ( if we don't
// do the very first Spaces.calculate will
// take seconds!
Expression vari = new Expression ("2*3+1");
vari.Evaluate ();
if (arcadeOrArendelle == 0)
System.Threading.Thread.Sleep (1000);
}
示例6: buttonTest_Click
private void buttonTest_Click(object sender, EventArgs e)
{
float textValue;
float start;
float end;
try
{
textValue = float.Parse(textBoxTestInput.Text);
start = float.Parse(textBoxRangeStart.Text);
end = float.Parse(textBoxRangeEnd.Text);
}
catch (Exception)
{
MessageBox.Show("不正确的输入", "Error");
return;
}
if (textValue < start || textValue > end)
{
labelTestResult.Text = "not in range, did not cal";
return;
}
String realExp = textBoxExp.Text.Replace("t", textBoxTestInput.Text);
try
{
Expression ex = new Expression(realExp);
labelTestResult.Text = ex.Evaluate().ToString();
}
catch(Exception excep)
{
MessageBox.Show(excep.ToString(), "Error");
}
}
示例7: Eval
public static bool Eval(object parameterValue, string strExpression)
{
Expression expression = new Expression(strExpression);
expression.Parameters["p"] = parameterValue;
bool flag = true;
return flag.Equals(expression.Evaluate());
}
示例8: CalculateTimeSliceTable
public static EDataTable CalculateTimeSliceTable()
{
foreach (var simpleFormulaModel in _timeSliceFormulas)
{
_timeSliceTable.Columns.Add(simpleFormulaModel.Name, typeof(double));
foreach (DataRow row in _timeSliceTable.Rows)
{
_expression = new Expression(simpleFormulaModel.Formula);
foreach (var column in simpleFormulaModel.UsedColumns)
{
double rowValue;
if (double.TryParse(row[column].ToString(), out rowValue))
_expression.Parameters[column] = rowValue;
}
try
{
var result = _expression.Evaluate();
row[simpleFormulaModel.Name] = result;
}
catch (Exception)
{
row[simpleFormulaModel.Name] = -1;
}
}
}
return _timeSliceTable;
}
示例9: FunctionConverter
/// <summary>
/// Creates a new instance of the FunctionConverterClass
/// </summary>
/// <param name="function">The function as a string</param>
/// <param name="length"></param>
/// <param name="interval"></param>
/// <param name="type">The type of data the function represents</param>
public FunctionConverter(String function, double length, double interval, int type)
{
int size = Convert.ToInt32(length / interval) + 1;
//_time = new List<double>();
//_value = new List<double>();
_time = new double[size];
_value = new double[size];
for (int i = 0; i < size; i++)
{
_time[i] = (i * interval);
String TempString = function.Replace("x", System.Convert.ToString(_time[i]));
Expression e = new Expression(TempString);
try
{
object result = e.Evaluate();
_value[i] = (Convert.ToDouble(result));
}
catch(Exception a)
{
MessageBox.Show("Error caught: " + a.Message);
}
}
SetProperties((FunctionType)type);
}
示例10: btnCalculate_Click
private void btnCalculate_Click(object sender, EventArgs e)
{
#if !DEBUG
try
{
#endif
string format = txtFormat.Text;
IEnumerable<string> expressions = GetExpressions(format);
foreach (string expression in expressions)
{
Expression expr = new Expression(MakeParseable(expression)) { Parameters = GetParameters() };
string result = expr.Evaluate().ToString();
format = format.Replace(expression, result);
}
txtCalculated.Text = format;
#if !DEBUG
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
#endif
}
示例11: IterationTest
public void IterationTest() {
var expr = new Expression("Sin(x)", EvaluateOptions.IgnoreCase | EvaluateOptions.IterateParameters);
expr.Parameters["x"] = Enumerable.Range(0, 1000).Select(x => x / Math.PI).ToArray();
foreach(var result in (IList)expr.Evaluate())
Console.WriteLine(result.AsDouble());
}
示例12: PlusMinusTest
public void PlusMinusTest() {
var expr = new Expression("2 + 3 * 5");
expr.Evaluate().Should().Be(17);
expr = new Expression("48/(2*(9+3))");
expr.Evaluate().Should().Be(2d);
}
示例13: RulesToDouble
/// <summary>
/// 公式轉數值
/// </summary>
/// <param name="rules">公式</param>
/// <param name="array">陣列</param>
/// <returns>數值</returns>
public static double RulesToDouble(String rules, ref double[] array)
{
if (String.IsNullOrEmpty(rules))
return 0.0d;
String rule = rules
.Replace("@3", array[3].ToString())
.Replace("@4", array[4].ToString())
.Replace("@5", array[5].ToString())
.Replace("@6", array[6].ToString())
.Replace("@7", array[7].ToString())
.Replace("@8", array[8].ToString())
.Replace("@9", array[9].ToString())
.Replace("@10", array[10].ToString())
.Replace("@11", array[11].ToString())
.Replace("@12", array[12].ToString())
.Replace("@2", array[2].ToString())
.Replace("@1", array[1].ToString())
.Replace("@0", array[0].ToString());
try
{
Expression number = new Expression(rule);
return Double.Parse(number.Evaluate().ToString());
}
catch
{
}
return 0.0d;
}
示例14: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
string equation = getRandomEquation(5);
lblRandomEquation.Text = equation;
NCalc.Expression ex = new NCalc.Expression(equation);
lblResult.Text = (ex.Evaluate().ToString());
}
示例15: Execute
public async void Execute(Dictionary<string, object> context)
{
Update update = context["update"] as Update;
CommandEntity commandEntity = context["commandEntity"] as CommandEntity;
Expression mathExpression=new Expression(commandEntity.Parameter,EvaluateOptions.IgnoreCase);
var message = mathExpression.Evaluate().ToString();
await _bot.SendTextMessage(update.Message.Chat.Id,message);
}