示例#1
0
        // EqualsButton_Click: Checks for the state of the users input and if it's in a valid state, performs an equation,
        // saves the input for later use if desired and prints the result to the form.
        // Pre: -
        // Post: The result of the equation is shown in the TextBox inputTextBox. The numbers and numeric operator used in
        // the equation is shown in the TextBox previousInputTextBox and saved to be reused if the user continues to press the
        // Equals-button without making any other alterations to their input. In case of either a division by zero or an equation
        // leading to the number being represented by ∞, a warning is shown and the calculator is reset.
        private void EqualsButton_Click(object sender, EventArgs e)
        {
            if (LastInputWasOperator || inputTextBox.Text.Last() == ',' || (!OperatorTypeChosen && !PreviousCalulations))
            {
                return;
            }

            if (OperatorTypeChosen)
            {
                TermTwo                   = Convert.ToDouble(inputTextBox.Text.Split(OperatorType).Last());
                PreviousTermTwo           = TermTwo;
                previousInputTextBox.Text = inputTextBox.Text;
            }
            else if (!OperatorTypeChosen && PreviousCalulations)
            {
                TermOne = Answer;
                TermTwo = PreviousTermTwo;
                previousInputTextBox.Text = PreviousAnswer.ToString() + OperatorType + PreviousTermTwo;
            }

            CalculateEquation();
            if (DivisionByZero() || AnswerIsInfinity())
            {
                return;
            }

            inputTextBox.Text   = Answer.ToString();
            OperatorTypeChosen  = false;
            PreviousAnswer      = Answer;
            PreviousCalulations = true;
        }
示例#2
0
        private void ProcessOperatorCommand(string value)
        {
            try
            {
                switch (value)
                {
                case "Clear":
                    TopInput      = "";
                    OperatorInput = "";
                    BottomInput   = "";
                    Answer        = 0;
                    ErrorMessage  = "";
                    break;

                case "Calculate":
                    if (!string.IsNullOrEmpty(OperatorInput))
                    {
                        switch (OperatorInput)
                        {
                        case "+":
                            Answer = Math.Round(TopInputDecimal() + BottomInputDecimal(), 2);
                            break;

                        case "-":
                            Answer = Math.Round(TopInputDecimal() - BottomInputDecimal(), 2);
                            break;

                        case "*":
                            Answer = Math.Round(TopInputDecimal() * BottomInputDecimal(), 2);
                            break;

                        case "/":
                            Answer = Math.Round(TopInputDecimal() / BottomInputDecimal(), 2);
                            break;
                        }
                    }
                    else
                    {
                        Answer = TopInputDecimal();
                    }

                    TopInput = Answer.ToString();

                    break;

                case "+":
                case "-":
                case "*":
                case "/":
                    BottomInput   = "";
                    OperatorInput = value;
                    break;
                }
            }
            catch (ArithmeticException e)
            {
                ErrorMessage = e.Message;
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;
            }
        }