Beispiel #1
0
 public static void ClearLastSymbol(this DisplayNumber displayNumber)
 {
     if (displayNumber.HasComma)
     {
         if (!string.IsNullOrEmpty(displayNumber.FractionalPart))
         {
             displayNumber.FractionalPart = displayNumber.FractionalPart.Substring(0, displayNumber.FractionalPart.Length - 1);
         }
         else
         {
             displayNumber.HasComma = false;
         }
     }
     else
     {
         var integerPartLength = displayNumber.IntegerPart.Length;
         if (integerPartLength > 1)
         {
             displayNumber.IntegerPart = displayNumber.IntegerPart.Substring(0, integerPartLength - 1);
         }
         else
         {
             displayNumber.Reset();
         }
     }
 }
Beispiel #2
0
 internal Calculator(CalculatorState state)
     : this()
 {
     _lastOperand1  = state.PreviousValue;
     _displayNumber = state.DisplayNumber;
     _isLastActionAnBinaryOperation = state.IsLastActionAnBinaryOperation;
     DisplayValue = _displayNumber.ToString();
 }
Beispiel #3
0
        public void ApplyEquality()
        {
            try
            {
                if (_isLastActionAnEquation && _lastBinaryOperation == null)
                {
                    var newValue = _displayNumber.ToDouble();
                    DisplayValue = newValue.ToString(_cultureInfo);

                    _lastOperand1 = newValue;
                    return;
                }

                var binaryOperationInfo = GetBinaryOperationInfo();
                if (binaryOperationInfo == null)
                {
                    return;
                }

                var value1          = binaryOperationInfo.Value1;
                var value2          = binaryOperationInfo.Value2;
                var binaryOperation = binaryOperationInfo.BinaryOperation;

                var executableInfo = binaryOperation.GetExecutableInfo(value1, value2);
                if (executableInfo.CanBeExecuted)
                {
                    var result = binaryOperation.Execute(value1, value2);

                    var displayValue = result.ToString(_cultureInfo);
                    _displayNumber = DisplayNumberFactory.Create(displayValue);
                    DisplayValue   = displayValue;

                    _lastOperand1        = value1;
                    _lastOperand2        = value2;
                    _lastBinaryOperation = binaryOperation;
                }
                else
                {
                    DisplayValue = executableInfo.Message;
                }

                _selectedBinaryOperation = null;
            }
            catch
            {
                DisplayValue = ErrorMessages.OperationFailed;
            }
            finally
            {
                _lastActionType = ActionType.Equality;
                _isLastActionAnBinaryOperation = false;
                _isLastActionAnEquation        = true;
            }
        }
Beispiel #4
0
        private void ExecuteOperation(Func <ExecutableInfo> getExecutableInfo, Func <double> getResult)
        {
            var executableInfo = getExecutableInfo();

            if (!executableInfo.CanBeExecuted)
            {
                DisplayValue = executableInfo.Message;
                return;
            }

            var result       = getResult();
            var displayValue = result.ToString(_cultureInfo);

            _displayNumber = DisplayNumberFactory.Create(displayValue);
            DisplayValue   = displayValue;
        }