Esempio n. 1
0
        /// <summary>
        /// Solves given equation elements returns solved number as result.
        /// </summary>
        /// <param name="elements">Equation elements sorce list.</param>
        /// <exception cref="EquationIsEmptyException">Thrown when solving equation after been solved is left empty.</exception>
        /// <exception cref="EquationNotSolvedProperlyException">Thrown when solving equation after been solved is not composed of one number element.</exception>
        private Number No_Bracket_Solver(Equation_Elements elements)
        {
            foreach (IOperand[] operand_Level in Order_Of_Operations.Operands)
            {
                for (int operand_index = 0; operand_index < elements.Count; operand_index++)
                {
                    if (elements[operand_index] is IOperand Equation_Operand)
                    {
                        foreach (IOperand operand_of_Level in operand_Level)
                        {
                            if (operand_of_Level == Equation_Operand)
                            {
                                INumber First_Number   = (INumber)elements[operand_index - 1];
                                INumber Seccond_Number = (INumber)elements[operand_index + 1];

                                INumber Result_Number = _Calculator.Operand_Selector(Equation_Operand, First_Number, Seccond_Number);

                                Exchange_Solved_to_Simple_Calculated_Value(elements, operand_index, Result_Number);
                                operand_index--;
                            }
                        }
                    }
                }
            }

            if (elements.Count < 1)
            {
                throw new EquationIsEmptyException();
            }
            else if (elements.Count == 1 && elements[0] is Number Result)
            {
                return(Result);
            }
            else
            {
                throw new EquationNotSolvedProperlyException(elements.Recreate_Equation());
            }
        }
        /// <summary>
        /// Solves given bracket and returns solved number as result.
        /// </summary>
        /// <param name="elements">Equation elements sorce list.</param>
        /// <param name="Current_Bracket_Pair">Pairs of brackets that inner equation is to be solved.</param>
        /// <returns></returns>
        private Number Inner_Brackets_Solver(Equation_Elements elements, Bracket_Pair Current_Bracket_Pair)
        {
            foreach (Bracket_Pair Inner_Bracket_pair in Current_Bracket_Pair.Inner_Brackets)
            {
                int Index_Of_Open_Bracket     = elements.IndexOf(Inner_Bracket_pair.Open_Bracket);
                int Lenght_Of_Current_Bracket =
                    elements.IndexOf(Inner_Bracket_pair.Close_Bracket) - Index_Of_Open_Bracket + 1;

                Number Solved_Inner_Bracket = Inner_Brackets_Solver(
                    new Equation_Elements(elements.GetRange(Index_Of_Open_Bracket + 1, Lenght_Of_Current_Bracket - 2)),
                    Inner_Bracket_pair
                    );

                Exchange_Solved_Range_to_Calculated_Value(
                    elements, Index_Of_Open_Bracket, Lenght_Of_Current_Bracket, Solved_Inner_Bracket);
            }

            Number Result_Of_Current_Bracket;

            try
            {
                Result_Of_Current_Bracket = No_Bracket_Solver(elements);
            }
            catch (EquationIsEmptyException)
            {
                IMessage_Box Message_Box = Factory.Get_Message_Box;

                Message_Box.Pop_Up($"Something went wrong equation or part of eqaution: {elements.Recreate_Equation()} " +
                                   $"had been solved improperly so \"0\" been pushed as result of shown equation.{Environment.NewLine}" +
                                   "If there is an error in input eqution correct or try write write equation in other way");
                Result_Of_Current_Bracket = new Number(0);
            }
            catch (EquationNotSolvedProperlyException e)
            {
                IMessage_Box Message_Box = Factory.Get_Message_Box;

                Message_Box.Pop_Up($"Something went wrong equation or part of eqaution: {elements.Recreate_Equation()} " +
                                   $"had been solved as \"{e.Message}\" so \"0\" been pushed as result of shown equation.{Environment.NewLine}" +
                                   "If there is an error in input eqution correct or try write write equation in other way");
                Result_Of_Current_Bracket = new Number(0);;
            }

            return(Result_Of_Current_Bracket);
        }