private void HandleOperator(char op) { _display.Print(_display.ParseDisplay()); if (UnaryOperatorProvider.IsUnaryOperator(op)) { var fun = UnaryOperatorProvider.GetOperator(op); _display.Print(fun(_display.ParseDisplay())); _lastPressed = ButtonCategory.UnaryOperator; } else if (BinaryOperatorProvider.IsBinaryOperator(op)) { if (double.IsNaN(_lastResult)) { _lastResult = _display.ParseDisplay(); } if (_operator != EMPTY_OPERATOR && _lastPressed != ButtonCategory.BinaryOperator) { double number = _display.ParseDisplay(); var fun = BinaryOperatorProvider.GetOperator(_operator); _lastResult = fun(_lastResult, number); _display.Print(_lastResult); } _operator = op; _lastPressed = ButtonCategory.BinaryOperator; } else { throw new ArgumentException($"Unknown operator '{op}'"); } }
private void EqualsSign() { if (_operator != EMPTY_OPERATOR) { double operand = _lastResult; if (!string.IsNullOrEmpty(_display.CurrentState)) { operand = _display.ParseDisplay(); } if (double.IsNaN(_lastResult)) { _lastResult = _display.ParseDisplay(); } var op = BinaryOperatorProvider.GetOperator(_operator); _display.Print(op(_lastResult, operand)); _operator = EMPTY_OPERATOR; _lastResult = double.NaN; } else { _display.Print(_display.ParseDisplay()); } }