Exemple #1
0
        /// <summary>
        /// Method for calculating the math expretion, returns the result
        /// </summary>
        /// <param name="operation"></param>
        private string calculate_operation(Calc_operation operation)
        {
            double left  = 0;
            double right = 0;

            // validating the inputted data

            if (string.IsNullOrEmpty(operation.left_side_operation) || !double.TryParse(operation.left_side_operation, out left))
            {
                throw new InvalidOperationException($"No number located on the left side of the equation. {operation.left_side_operation}");
            }


            if (string.IsNullOrEmpty(operation.right_side_operation) || !double.TryParse(operation.right_side_operation, out right))
            {
                throw new InvalidOperationException($"No number located on the left side of the equation. {operation.right_side_operation}");
            }



            try
            {
                switch (operation.operators)
                {
                case Operators.add:
                    return((left + right).ToString());

                    break;


                case Operators.substract:
                    return((left - right).ToString());

                    break;


                case Operators.multiply:
                    return((left * right).ToString());

                    break;


                case Operators.divide:
                    return((left / right).ToString());

                    break;


                case Operators.percentage:
                    return((left % right).ToString());


                default:
                    throw new InvalidOperationException($"Invalid math operation. {operation.operators}");

                    break;
                }
            }catch (Exception x)
            {
                throw new InvalidOperationException($"Invalid calculation. {operation.left_side_operation} " +
                                                    $"{operation.operators} {operation.right_side_operation}");
            }


            // returning empty string
            return(string.Empty);
        }
Exemple #2
0
        /// <summary>
        /// Method to calculate the result and to convert it into string
        /// </summary>
        /// <returns></returns>
        private string resOperation()
        {
            try
            {
                // take the user input
                var user = this.UserInput.Text;

                // removing the spaces
                user = user.Replace(" ", "");

                var operation = new Calc_operation();

                // setting a boolean for the left side of the operation to true
                var left_side = true;


                // looping trough each character from the left
                for (int i = 0; i < user.Length; ++i)
                {
                    // string containing the valid numeric values
                    var strNumbers = "0123456789.";

                    // string containing the valid math operations
                    var strMathOperations = "+-/*%";


                    // validating the input
                    if (strNumbers.Any(num => user[i] == num))
                    {
                        if (left_side)
                        {
                            operation.left_side_operation = AddPartNum(operation.left_side_operation, user[i]);
                        }
                        else
                        {
                            operation.right_side_operation = AddPartNum(operation.right_side_operation, user[i]);
                        }
                    }
                    else if (strMathOperations.Any(num => user[i] == num))
                    {
                        if (!left_side)
                        {
                            var typeOperation = getTypeOperation(user[i]);


                            if (operation.right_side_operation.Length == 0)
                            {
                                if (typeOperation != Operators.substract)
                                {
                                    throw new InvalidOperationException($"Invalid operator specified");
                                }

                                operation.right_side_operation += user[i];
                            }
                            else
                            {
                                operation.left_side_operation = calculate_operation(operation);
                                operation.operators           = typeOperation;


                                operation.right_side_operation = string.Empty;
                            }
                        }
                        else
                        {
                            var typeOperation = getTypeOperation(user[i]);

                            if (operation.left_side_operation.Length == 0)
                            {
                                if (typeOperation != Operators.substract)
                                {
                                    throw new InvalidOperationException($"Invalid operator specified");
                                }

                                operation.left_side_operation += user[i];
                            }
                            else
                            {
                                operation.operators = typeOperation;

                                left_side = false;
                            }
                        }
                    }
                }


                // returning calculate_operation method
                return(calculate_operation(operation));


                throw new ArgumentException("Invalid argument");
            }
            catch (Exception x)
            {
                return($"Invalid input. \n{x.Message}");
            }
        }