Exemple #1
0
        /// <summary>
        /// To add the current number and the selected operation to the current expression
        /// </summary>
        public void SetBasicMathOperation(BasicMathOperations pressedOperation)
        {
            //If the expression was calculated and any basic math operation was pressed, except for "="
            if (buttonsState.EqualBtnPressed && (pressedOperation != BasicMathOperations.Equal))
            {
                currentData.CurrentExpression = clearData.ClearExpression(currentData.CurrentExpression);
                buttonsState.NumberPadBtnPressed_Change(true);
                buttonsState.AdditionalOperationBtnPressed_Change(false);
                buttonsState.EqualBtnPressed_Change(false);
            }

            currentData.CurrentExpression = NewCurrentExpression(pressedOperation);
            currentData.CurrentNumber     = pressedOperation == BasicMathOperations.Equal ? calculate.Calc(currentData.CurrentExpression).ToString() : clearData.ClearNumber(currentData.CurrentNumber);

            if (double.IsNaN(double.Parse(currentData.CurrentNumber)) || double.IsPositiveInfinity(double.Parse(currentData.CurrentNumber)) ||
                double.IsNegativeInfinity(double.Parse(currentData.CurrentNumber)))
            {
                currentData.CurrentNumber = "Calculation error";
            }

            if (pressedOperation == BasicMathOperations.Equal)
            {
                buttonsState.EqualBtnPressed_Change(true);
            }

            buttonsState.NumberPadBtnPressed_Change(false);
            buttonsState.AdditionalOperationBtnPressed_Change(false);
        }
Exemple #2
0
        /// <summary>
        /// To add the current number and the selected operation to the current expression
        /// </summary>
        public void SetBasicMathOperation(BasicMathOperations pressedOperation)
        {
            //If the expression was calculated and any basic math operation was pressed, except for "="
            if (buttonsState.EqualBtnPressed && (pressedOperation != BasicMathOperations.Equal))
            {
                currentData.CurrentExpression = clearData.ClearExpression(currentData.CurrentExpression);
                buttonsState.NumberPadBtnPressed_Change(true);
                buttonsState.AdditionalOperationBtnPressed_Change(false);
                buttonsState.EqualBtnPressed_Change(false);
            }

            currentData.CurrentExpression = NewCurrentExpression(pressedOperation);
            try
            {
                currentData.CurrentNumber = pressedOperation == BasicMathOperations.Equal
                ? calculate.Calc(currentData.CurrentExpression).ToString()
                : clearData.ClearNumber(currentData.CurrentNumber);
            }
            catch (DivideByZeroException) { currentData.CurrentNumber = "Division by zero"; }
            catch (OverflowException) { currentData.CurrentNumber = "NaN"; }


            if (pressedOperation == BasicMathOperations.Equal)
            {
                buttonsState.EqualBtnPressed_Change(true);
            }

            buttonsState.NumberPadBtnPressed_Change(false);
            buttonsState.AdditionalOperationBtnPressed_Change(false);
        }
 /// <summary>
 /// To change the current expression depending on the pressed operation and the state of the buttons
 /// </summary>
 private string NewCurrentExpression(BasicMathOperations pressedOperation)
 {
     //If the expression was calculated and "=" was pressed again
     if (buttonsState.EqualBtnPressed && (pressedOperation == BasicMathOperations.Equal) && Common.MathSignCheck(currentData.CurrentExpression))
     {
         return(NumberStandardization.Standardization(currentData.CurrentNumber) + CopyLastOperation(currentData.CurrentExpression.Remove(currentData.CurrentExpression.Length - 3, 3)) + SetSelectedBasicMathOperation(pressedOperation));
     }
     else if (pressedOperation == BasicMathOperations.Sqr)
     {
         return(SetSelectedBasicMathOperation(pressedOperation) + NumberStandardization.StandardizationSqr(currentData.CurrentNumber));
     }
     else if (pressedOperation == BasicMathOperations.Sqrt)
     {
         return(SetSelectedBasicMathOperation(pressedOperation) + NumberStandardization.StandardizationSqr(currentData.CurrentNumber));
     }
     else if (pressedOperation == BasicMathOperations.PartOfTheWhole)
     {
         return(SetSelectedBasicMathOperation(pressedOperation) + NumberStandardization.StandardizationSqr(currentData.CurrentNumber));
     }
     else if (buttonsState.NumberPadBtnPressed)
     {
         return(currentData.CurrentExpression + NumberStandardization.Standardization(currentData.CurrentNumber) + SetSelectedBasicMathOperation(pressedOperation));
     }
     //Сhanging the basic math operation sign
     else
     {
         return(ChangeOperation(currentData.CurrentExpression, SetSelectedBasicMathOperation(pressedOperation)));
     }
 }
        /// <summary>
        /// To set the symbolic representation of a basic math operation
        /// </summary>
        /// <returns>
        /// A string representing the selected operation
        /// </returns>
        private string SetSelectedBasicMathOperation(BasicMathOperations pressedOperation)
        {
            switch (pressedOperation)
            {
            case BasicMathOperations.Addition: return(" + ");

            case BasicMathOperations.Subtraction: return(" - ");

            case BasicMathOperations.Equal: return(" = ");

            case BasicMathOperations.Multiply: return(" × ");

            case BasicMathOperations.Division: return(" ÷ ");

            case BasicMathOperations.ModuleDivision: return(" % ");

            case BasicMathOperations.PartOfTheWhole: return(" ¹/ ");

            case BasicMathOperations.Sqr: return(" Sqr ");

            case BasicMathOperations.Sqrt: return(" √ ");
            }

            return("Operation not found");
        }
Exemple #5
0
        /// <summary>
        /// To set the symbolic representation of a basic math operation
        /// </summary>
        /// <returns>
        /// A string representing the selected operation
        /// </returns>
        private string SetSelectedBasicMathOperation(BasicMathOperations pressedOperation)
        {
            switch (pressedOperation)
            {
            case BasicMathOperations.Addition: return(" + ");

            case BasicMathOperations.Subtraction: return(" - ");

            case BasicMathOperations.Equal: return(" = ");
            }

            return("Operation not found");
        }
Exemple #6
0
        public void AdditionShouldReturnExpectedValue(
            double x,
            double y,
            double expected)
        {
            //Arrange
            BasicMathOperations bm = new BasicMathOperations();

            //Act
            double actual = bm.Addition(x, y);

            //Assert
            Assert.Equal(expected, actual);
        }
Exemple #7
0
        public void DivisionTest()
        {
            //Arrange
            BasicMathOperations add = new BasicMathOperations();
            double expected         = 2;
            double expected2        = 3;

            //Act
            double actual  = add.Division(4, 2);
            double actual2 = add.Division(6, 2);

            //Assert
            Assert.Equal(expected, actual);
            Assert.Equal(expected2, actual2);
        }
Exemple #8
0
        public void MultiplicationTest()
        {
            //Arrange
            BasicMathOperations add = new BasicMathOperations();
            double expected         = 2;
            double expected2        = 3;

            //Act
            double actual  = add.Multiplication(2, 1);
            double actual2 = add.Multiplication(1, 3);

            //Assert
            Assert.Equal(expected, actual);
            Assert.Equal(expected2, actual2);
        }
Exemple #9
0
        public void SubtractionTest()
        {
            //Arrange
            BasicMathOperations add = new BasicMathOperations();
            double expected         = 2;
            double expected2        = 3;

            //Act
            double actual  = add.Subtraction(3, 1);
            double actual2 = add.Subtraction(5, 2);

            //Assert
            Assert.Equal(expected, actual);
            Assert.Equal(expected2, actual2);
        }
Exemple #10
0
 static void Main(string[] args)
 {
     Console.WriteLine(BasicMathOperations.AddNumbers(10, 20));
     Console.WriteLine(BasicMathOperations.SubtractNumbers(10, 20));
     Console.WriteLine(BasicMathOperations.multiplyNumbers(3, 4));
     Console.WriteLine(BasicMathOperations.divideNumbers(1, 2));
     try
     {
         BasicMathOperations.divideNumbers(1, 0);
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
 }
Exemple #11
0
        /// <summary>
        /// To set the symbolic representation of a basic math operation
        /// </summary>
        /// <returns>
        /// A string representing the selected operation
        /// </returns>
        private string SetSelectedBasicMathOperation(BasicMathOperations pressedOperation)
        {
            switch (pressedOperation)
            {
            case BasicMathOperations.Addition: return(" + ");

            case BasicMathOperations.Subtraction: return(" - ");

            case BasicMathOperations.Equal: return(" = ");

            case BasicMathOperations.Multiplication: return(" * ");

            case BasicMathOperations.Division: return(" / ");

            case BasicMathOperations.ModuleDivision: return(" % ");
            }

            return("Operation not found");
        }
Exemple #12
0
 /// <summary>
 /// To change the current expression depending on the pressed operation and the state of the buttons
 /// </summary>
 private string NewCurrentExpression(BasicMathOperations pressedOperation)
 {
     //If the expression was calculated and "=" was pressed again
     if (buttonsState.EqualBtnPressed && (pressedOperation == BasicMathOperations.Equal) && Common.MathSignCheck(currentData.CurrentExpression))
     {
         return(NumberStandardization.Standardization(currentData.CurrentNumber) + CopyLastOperation(currentData.CurrentExpression.Remove(currentData.CurrentExpression.Length - 3, 3)) + SetSelectedBasicMathOperation(pressedOperation));
     }
     //If the number and additional operation were pressed
     else if (buttonsState.NumberPadBtnPressed && buttonsState.AdditionalOperationBtnPressed)
     {
         return(currentData.CurrentExpression + SetSelectedBasicMathOperation(pressedOperation));
     }
     //If a number was pressed, but not an additional operation
     else if (buttonsState.NumberPadBtnPressed && !buttonsState.AdditionalOperationBtnPressed)
     {
         return(currentData.CurrentExpression + NumberStandardization.Standardization(currentData.CurrentNumber) + SetSelectedBasicMathOperation(pressedOperation));
     }
     //Сhanging the basic math operation sign
     else
     {
         return(ChangeOperation(currentData.CurrentExpression, SetSelectedBasicMathOperation(pressedOperation)));
     }
 }