public int PerformCalculation(int firstNumber, int secondNumber, string operatorSymbol)
        {
            BinaryOperations execute = new BinaryOperations();

            if (operatorSymbol == OperatorConstants.AddSymbol.ToString())
            {
                secondNumber = execute.Addition(firstNumber, secondNumber);

                return(secondNumber);
            }
            else if (operatorSymbol == OperatorConstants.SubtractSymbol.ToString())
            {
                secondNumber = execute.Subtraction(firstNumber, secondNumber);

                return(secondNumber);
            }
            else if (operatorSymbol == OperatorConstants.DivideSymbol.ToString())
            {
                secondNumber = execute.Division(firstNumber, secondNumber);

                return(secondNumber);
            }
            else if (operatorSymbol == OperatorConstants.MultiplySymbol.ToString())
            {
                secondNumber = execute.Multiplication(firstNumber, secondNumber);

                return(secondNumber);
            }
            else
            {
                return(0);
            }
        }
Beispiel #2
0
        public void MultiplicationInputArgumentReturnedValue(int leftValue, int rightValue, int expectedValue)
        {
            var leftNumber = new Number()
            {
                Value = leftValue
            };
            var rightNumber = new Number()
            {
                Value = rightValue
            };

            var result = BinaryOperations.Multiplication(leftNumber, rightNumber);

            Assert.AreEqual(expectedValue, result.Value);
        }
        /// <summary>
        /// The calculation of the converted expression.
        /// </summary>
        /// <param name="inputExpression"> Input the converted expression. </param>
        /// <param name="firstNumber"> The first number. </param>
        /// <param name="secondNumber"> The second number. </param>
        /// <returns> Result of calculating the expression. </returns>
        public static Number Counting(string inputExpression, Number firstNumber, Number secondNumber)
        {
            if (string.IsNullOrWhiteSpace(inputExpression))
            {
                throw new ArgumentNullException("The input expression cannot be empty.", nameof(inputExpression));
            }

            double result = 0;
            var    temp   = new Stack <double>();

            for (int i = 0; i < inputExpression.Length; i++)
            {
                if (char.IsDigit(inputExpression[i]))
                {
                    string a = string.Empty;

                    while (!IsDelimeter(inputExpression[i]) && !IsOperator(inputExpression[i]))
                    {
                        a += inputExpression[i];
                        i++;
                        if (i == inputExpression.Length)
                        {
                            break;
                        }
                    }

                    temp.Push(double.Parse(a, CultureInfo.InvariantCulture));
                    i--;
                }
                else if (IsOperator(inputExpression[i]))
                {
                    if (inputExpression[i] == '!')
                    {
                        firstNumber = new Number {
                            Value = temp.Pop()
                        }
                    }
                    ;
                    else
                    {
                        firstNumber = new Number {
                            Value = temp.Pop()
                        };
                        secondNumber = new Number {
                            Value = temp.Pop()
                        };
                    }

                    switch (inputExpression[i])
                    {
                    case '+': result = BinaryOperations.Addition(secondNumber, firstNumber).Value; break;

                    case '-': result = BinaryOperations.Subsctraction(secondNumber, firstNumber).Value; break;

                    case '*': result = BinaryOperations.Multiplication(secondNumber, firstNumber).Value; break;

                    case '/': result = BinaryOperations.Division(secondNumber, firstNumber).Value; break;

                    case '%': result = BinaryOperations.DivisionReaminder(secondNumber, firstNumber).Value; break;

                    case '^': result = UnaryOperations.Exponentiation(secondNumber, firstNumber).Value; break;

                    case '!': result = UnaryOperations.Factorial(firstNumber).Value; break;
                    }

                    temp.Push(result);
                }
            }

            return(new Number {
                Value = temp.Peek()
            });
        }