Example #1
0
        public void EqualsButtonClick()
        {
            /* Method invoked when the "Equals" button is pressed. */

            // If the calculator is in an error state, then don't do anything.
            if (errorState == ErrorState.None)
            {
                // Variable to store the number currently being displayed.
                double number;

                // Parse and store the number currently being displayed.
                if (double.TryParse(DisplayString, out number))
                {
                    // If the current number is the first number in the current computation (ie. the selected operator is undefined), then store this number in "result".
                    if (selectedOperator == Operator.Undefined)
                    {
                        result = number;
                    }
                    // Otherwise, perform the selected operation and store the result in the "result" member.
                    else
                    {
                        switch (selectedOperator)
                        {
                        case Operator.Addition:
                            result = BasicMath.Add(result, number);
                            break;

                        case Operator.Subtraction:
                            result = BasicMath.Subtract(result, number);
                            break;

                        case Operator.Multiplication:
                            result = BasicMath.Multiply(result, number);
                            break;

                        case Operator.Division:
                            // If dividing by 0, set the calculator to error state
                            if (number == 0)
                            {
                                errorState = ErrorState.DivisionByZero;
                            }
                            else
                            {
                                result = BasicMath.Divide(result, number);
                            }
                            break;
                        }

                        // Set the display accordingly
                        if (errorState != ErrorState.None)
                        {
                            DisplayString = "Error";
                        }
                        else
                        {
                            DisplayString = $"{result}";
                        }
                    }
                }

                // Reset the selected operator to "Undefined" and update last button pressed and last dependency button pressed.
                selectedOperator      = Operator.Undefined;
                lastDepButtonPressed  = ButtonType.Equals;
                lastDepButtonPressed2 = ButtonType.Equals;
            }
        }