public double Calculate(string expression) { var expressionUnitChecker = new ExpressionUnitChecker(); var helper = new PolyNotationReader(expressionUnitChecker, expression); var result = helper.GetPolNotation(); var stackExpression = new Stack <double>(); foreach (var unit in result) { switch (unit) { case NumberExpressionUnit n: stackExpression.Push(n.Number); break; case OperationExpressionUnit o: stackExpression.Push(Calck(stackExpression.Pop(), stackExpression.Pop(), o)); break; } } return(stackExpression.Peek()); }
public ExpressionReader(ExpressionUnitChecker expressionUnitChecker, string expression) { this.expressionUnitChecker = expressionUnitChecker; this.expression = expression.Replace(" ", ""); }
public PolyNotationReader(ExpressionUnitChecker expressionUnitChecker, string expression) { this.expressionUnitChecker = expressionUnitChecker; expressionReader = new ExpressionReader(expressionUnitChecker, expression.Replace(" ", "")); }