Exemple #1
0
        // Called when one of the rounding buttons is pressed.
        void RoundNumber(int decimalPlaces)
        {
            // Only round if there is a number to round
            if (double.TryParse(DisplayedNumber.Instance.Number, out double currentNumber))
            {
                double newNumber = Math.Round(currentNumber, decimalPlaces);

                string correctedDecimalNumber = newNumber.ToString();

                // TODO: add decimals if there aren't enough decimals

                //int decimalCount = 0;
                //bool startCount = false;
                //foreach (char character in correctedDecimalNumber)
                //{
                //    if (character == Constants.DecimalSeparator)
                //        startCount = true;
                //    else if (startCount)
                //        decimalCount++;
                //}
                //Console.WriteLine(decimalCount);
                //Console.WriteLine(decimalPlaces);
                //while (decimalCount < decimalPlaces)
                //{
                //    correctedDecimalNumber += Constants.Zero;
                //    decimalCount++;
                //}

                SetNumber(correctedDecimalNumber);

                // Let those interested know a number has been rounded
                NumberEvents.OnNumberRounded(newNumber);
            }
        }
Exemple #2
0
        // Invoked when an operation − + ÷ × is pressed.
        void AddOperation(Operation newOperation)
        {
            // If there is a previous operation that has to be calculated, first perform the calculation.
            // For example if the user inputs 8 + 3 x 2 without pressing =, we first need to calculate 8 + 3.
            // But only if a second operand has been entered, and not for example:
            // If the input is 8 + x, then we should correct 8 + to 8 x, not perform any calculation with +.
            if (Operation != null && newOperand == true)
            {
                Calculate();
            }

            // The operation and the first operand are saved, and we're waiting for a second operand.
            Operation  = newOperation;
            Operand1   = GetDisplayedNumber();
            newOperand = false;

            // Let those interested know that the first half of the equation is complete.
            NumberEvents.EquationStarted();
        }
Exemple #3
0
        // Invoked when = is pressed
        void Calculate()
        {
            // if the = is pressed but there's nothing to calculate, do nothing.
            if (Operation == null)
            {
                return;
            }

            Operand2 = GetDisplayedNumber();

            // Perform the operation.
            // The result could be not a valid number in the case of division by 0.
            (EquationAnswer answer, double number) = Operation.Calculate(Operand1.Value, Operand2.Value);

            // Let those interested know that a calculation has been completed.
            NumberEvents.EquationCalculated(answer, number);

            // Reset the equation. This should not be done before the event.
            ClearOperands();
        }
Exemple #4
0
        // Invoked every time a calculation or rounding is performed.
        void CopyToClipboard(EquationAnswer answer, double number)
        {
            if (answer == EquationAnswer.DivideBy0)
            {
                // If it's a division by 0, do not copy anything to clipboard but reset the textbox.
                NumberEvents.CopiedToClipboard(string.Empty);
                return;
            }


            string validAnswer = number.ToString();

            // Copy the number to the clipboard and show a message that a number has been copied.
            Clipboard.SetText(validAnswer);
            NumberEvents.CopiedToClipboard(validAnswer);

            //OutputEvents.UpdateClipboardMessage(String.Format(Constants.CopyToClipboard, result));

            //Need to save the copied value so it won't be pasted back into the calculator.
            lastPastedValue = validAnswer;
        }