public IEvaluator CreateEvaluator(Expression expression) { // 转换为后缀表达式集合 List<ExpressionItem> postfixExpressions = PostfixTranslator.Translate(expression.Items); Evaluator eval = new Evaluator(postfixExpressions, this.Accuracy); return eval; }
private void button2_Click(object sender, EventArgs e) { try { ExpressionContext context = new ExpressionContext(); var expression = new Expression(this.txtExpression.Text); IEvaluator eval = context.CreateEvaluator(expression); int count = Convert.ToInt32(this.textBox2.Text); double result = 0; for (int i = 0; i < count; i++) { result = eval.Evaluate(); } MessageBox.Show(result.ToString(CultureInfo.InvariantCulture)); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
public double Evaluate(string expression, ExpressionParam[] parameters = null) { var exp = new Expression(expression); IEvaluator eval = CreateEvaluator(exp); return eval.Evaluate(parameters); }