public void ExpressionTest() { // (10 + 20) * 30 / 40 var e1 = new ValueExpression(10); var e2 = new ValueExpression(20); var e3 = new ValueExpression(30); var e4 = new ValueExpression(40); Expression expression = new DivisionBinaryExpression( new MultiplicationBinaryExpression( new AdditionBinaryExpression(e1, e2), e3), e4); var solution = expression.Solve(); Assert.That(solution, Is.EqualTo(22.5)); }
private static Expression Subtraction(IReadOnlyList<string> expression) { for (var index = 0; index < expression.Count; index++) { if (expression[index] == "-") { var value1 = decimal.Parse(expression[index - 1]); var value2 = decimal.Parse(expression[index + 1]); var expression1 = new ValueExpression(value1); var expression2 = new ValueExpression(value2); return new SubtractionBinaryExpression(expression1, expression2); } } return null; }