/// <summary>
        /// Calculates the number.
        /// </summary>
        /// <param name="value1">The value1.</param>
        /// <param name="typeEnum">The type enum.</param>
        /// <param name="value2">The value2.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException">Unknown operation symbol</exception>
        public static int CalculateNumber(int value1, ArithmeticTypeEnum typeEnum, int value2)
        {
            switch (typeEnum)
            {
            case ArithmeticTypeEnum.Add:
                return(value1 + value2);

            case ArithmeticTypeEnum.Subtract:
                return(value1 - value2);

            case ArithmeticTypeEnum.Multiply:
                return(value1 * value2);

            case ArithmeticTypeEnum.Divide:
                return(value1 / value2);

            default:
                throw new ArgumentException("Unknown operation symbol");
            }
        }
        /// <summary>
        /// Calculates the number.
        /// </summary>
        /// <param name="value1">The value1.</param>
        /// <param name="typeEnum">The type enum.</param>
        /// <param name="value2">The value2.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException">Unknown operation symbol</exception>
        public static int CalculateNumber(int value1, ArithmeticTypeEnum typeEnum, int value2)
        {
            switch (typeEnum)
            {
                case ArithmeticTypeEnum.Add:
                    return value1 + value2;

                case ArithmeticTypeEnum.Subtract:
                    return value1 - value2;

                case ArithmeticTypeEnum.Multiply:
                    return value1 * value2;

                case ArithmeticTypeEnum.Divide:
                    return value1 / value2;

                default:
                    throw new ArgumentException("Unknown operation symbol");
            }
        }
Example #3
0
        /// <summary>
        /// Calculates the expression.
        /// </summary>
        /// <param name="cell">The cell.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        public static string Calculate(ICell cell)
        {
            if (cell == null)
            {
                throw new ArgumentNullException(nameof(cell));
            }

            int[]  numbers;
            char[] operations;
            SplitExpression(cell, out numbers, out operations);

            const int differentLength = 1;

            var totalValue = Convert.ToInt32(numbers[0]);

            for (var i = 1; i <= operations.Length; i++)
            {
                ArithmeticTypeEnum arithmeticTypeEnum = Converter.ToArithmeticType(operations[i - differentLength]);
                var nextNumber = Convert.ToInt32(numbers[i]);
                totalValue = CellArithmeticCalculation.CalculateNumber(totalValue, arithmeticTypeEnum, nextNumber);
            }
            return(totalValue.ToString());
        }