示例#1
0
        private void buttonEqual_Click(object sender, EventArgs e)
        {
            String calcResult = getCalculationResult(); //string to hold the results of the calculation

            //if the user has selected to use the refactored calculator
            if (checkRefactoredCalculator.Checked == true)
            {
                //if the ressult returned by the calculation is not the same as the result stored by the calculator
                if (calcResult == refactoredCalculator.getResult())
                {
                    calculatorDisplayBox.Text = calcResult;
                    buttonUndo.Enabled        = true;  //set the undo button to be enabled
                    buttonRedo.Enabled        = false; //set the redo button to be disabled
                    index = 0;                         //set the history index to 0
                }
                else
                {
                    calculatorDisplayBox.Text = calcResult;
                }
            }
            //if the user has selected to use the simple calculator
            else
            {
                //if the ressult returned by the calculation is not the same as the result stored by the calculator
                if (simpleCalculator != null && calcResult == simpleCalculator.getResult())
                {
                    calculatorDisplayBox.Text = calcResult;
                    buttonUndo.Enabled        = true;  //set the undo button to be enabled
                    buttonRedo.Enabled        = false; //set the redo button to be disabled
                    index = 0;                         //set the history index to 0
                }
                else
                {
                    calculatorDisplayBox.Text = calcResult;
                }
            }

            //focus on the calculatorDisplayBox and set the cursor to the end
            calculatorDisplayBox.Focus();
            calculatorDisplayBox.SelectionStart  = calculatorDisplayBox.TextLength;
            calculatorDisplayBox.SelectionLength = 0;
        }
示例#2
0
        //private method to get the calculation results from the calculator
        private String getCalculationResult()
        {
            List <String> input = parseInput();

            //if the input is not empty
            if (input.Count != 0)
            {
                //if the user has specified they are using the simple calculator
                if (checkRefactoredCalculator.Checked == false)
                {
                    //if the input contains all 3 parts: (number operator number)
                    if (input.Count == 3)
                    {
                        //if the simpleCalculator has not been initiated yet
                        if (simpleCalculator == null)
                        {
                            simpleCalculator = new Calculator(input[0], input[2], input[1]); //call the constructor
                            simpleCalculator.calculate();                                    //calculate the result
                            return(simpleCalculator.getResult());                            //return the result
                        }
                        else
                        {
                            simpleCalculator.setLeft(input[0]);     //set the left operand
                            simpleCalculator.setOperator(input[1]); //set the operator
                            simpleCalculator.setRight(input[2]);    //set the right operand
                            simpleCalculator.calculate();           //calculate the result
                            return(simpleCalculator.getResult());   //return the result
                        }
                    }
                    //if the input only contains 2 parts: (number operator) or (operator number)
                    else if (input.Count == 2)
                    {
                        //if the simpleCalculator has not been initiated yet
                        if (simpleCalculator == null)
                        {
                            simpleCalculator = new Calculator(input[0], "", input[1]); //call the constructor
                            simpleCalculator.calculate();                              //calculate the result
                            return(simpleCalculator.getResult());                      //return the result
                        }
                        else
                        {
                            simpleCalculator.setLeft(input[0]);     //set the left operand
                            simpleCalculator.setOperator(input[1]); //set the operator
                            simpleCalculator.setRight("");          //set the right operand to be an empty string
                            simpleCalculator.calculate();           //calculate the result
                            return(simpleCalculator.getResult());   //return the result
                        }
                    }
                    //if the input only contains one part
                    else if (input.Count == 1)
                    {
                        /*
                         * //if the simpleCalculator has not been initiated yet
                         * if (simpleCalculator == null)
                         * {
                         *  simpleCalculator = new Calculator(input[0], "", "");    //call the constructor
                         *  simpleCalculator.calculate();   //calculate the result
                         *  return simpleCalculator.getResult();    //return the result
                         * }
                         * else
                         * {
                         *  simpleCalculator.setLeft(input[0]); //set the left operand
                         *  simpleCalculator.setOperator("");   //set the operator to be an empty string
                         *  simpleCalculator.setRight("");  //set the right operand to be an empty string
                         *  simpleCalculator.calculate();   //calculate the reuslt
                         *  return simpleCalculator.getResult();    //return the result
                         * }*/
                    }
                    //if the input is longer than the basic formula
                    else
                    {
                        //check for a failed scientific notation
                        foreach (String str in input)
                        {
                            if (str[str.Length - 1] == 'E')
                            {
                                for (int i = 0; i < str.Length - 1; i++)
                                {
                                    if (!Char.IsDigit(str[i]) && str[i] != '+' && str[i] != '-' && str[i] != '*' && str[i] != '/' && str[i] != '.')
                                    {
                                        return(String.Format("Error: \"{0}\" is not a number", str));
                                    }
                                }
                            }
                        }
                        return("Error: Too many arguments");
                    }
                }
                //if the user has specified they are using the refactored calculator
                else
                {
                    return(refactoredCalculator.calculate(input));   //return the result of the calculation
                }
            }

            return(calculatorDisplayBox.Text);
        }