本文整理汇总了C#中NCalc.Expression类的典型用法代码示例。如果您正苦于以下问题:C# Expression类的具体用法?C# Expression怎么用?C# Expression使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Expression类属于NCalc命名空间,在下文中一共展示了Expression类的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: 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);
}
示例3: 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;
}
示例4: 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);
}
示例5: 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());
}
示例6: 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);
}
示例7: 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");
}
}
示例8: Evaluate
public override Value Evaluate(FSharpList<Value> args)
{
var e = new Expression(Formula);
var functionLookup = new Dictionary<string, Value>();
foreach (var arg in args.Select((arg, i) => new { Value = arg, Index = i }))
{
var parameter = InPortData[arg.Index].NickName;
if (arg.Value.IsFunction)
functionLookup[parameter] = arg.Value;
else
e.Parameters[parameter] = ((Value.Number)arg.Value).Item;
}
e.EvaluateFunction += delegate(string name, FunctionArgs fArgs)
{
if (functionLookup.ContainsKey(name))
{
var func = ((Value.Function)functionLookup[name]).Item;
fArgs.Result = ((Value.Number)func.Invoke(
Utils.SequenceToFSharpList(
fArgs.Parameters.Select<Expression, Value>(
p => Value.NewNumber(Convert.ToDouble(p.Evaluate())))))).Item;
}
else
{
fArgs.HasResult = false;
}
};
return Value.NewNumber(Convert.ToDouble(e.Evaluate()));
}
示例9: 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;
}
示例10: Evaluate
public void Evaluate(Expression exp, Expression derExp, double a, double b, double tolerance)
{
addToLog("Starting Newton Method");
double expX, derExpX, result;
double current = a;
double previous = 0;
while (true)
{
iteration++;
// Functions
expX = evaluate(exp, current);
derExpX = evaluate(derExp, current);
result = current - (expX / derExpX);
// log
addToLog(newLine + "Iteration: " + iteration);
addToLog("F(x)= " + RoundDigit(expX, 4) + tab
+ "F'(x)= " + RoundDigit(derExpX, 4) + tab
+ "x= " + RoundDigit(result, 4));
// swaps
previous = current;
current = result;
bool withinTolerance = inTolerance(tolerance, previous, current);
if (withinTolerance)
break;
}
addToLog("ROOT FOUND: " + result);
}
示例11: 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());
}
示例12: textBox_TextChanged
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
var textBox = sender as TextBox;
if (textBox != null)
{
if (textBox.Text != "")
{
expression = new Expression(textBox.Text);
var paras = GetParametersInExpression(textBox.Text).Distinct().ToList();
if (paras.Any())
{
RemoveAllInputPortsFromNode(paras);
var filteredParas = paras.Where(parameter => InputPorts.All(p => p.Name != parameter)).ToList();
foreach (var parameter in filteredParas)
AddInputPortToNode(parameter, typeof (object));
}
}
else
{
expression = null;
RemoveAllInputPortsFromNode();
}
}
Calculate();
}
示例13: 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);
}
示例14: 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
}
示例15: 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());
}