Beispiel #1
0
        /// <summary>
        /// Searches closing bracket for given open bracket in equation.
        /// </summary>
        /// <param name="Opening_Bracket"></param>
        /// <param name="Root_Bracket_Pair"></param>
        /// <returns></returns>
        private Bracket_Pair Search_for_Close_Bracket(Open_Bracket Opening_Bracket, Bracket_Pair Root_Bracket_Pair)
        {
            int bracket_Index = Element_Colection.IndexOf(Opening_Bracket);
            int bracket_Level = 0;

            for (int i = bracket_Index; i < Element_Colection.Count; i++)
            {
                if (Element_Colection[i] is Open_Bracket)
                {
                    bracket_Level++;
                }

                if (Element_Colection[i] is Close_Bracket CB)
                {
                    if (--bracket_Level == 0)
                    {
                        return(new Bracket_Pair(Opening_Bracket, CB, Root_Bracket_Pair));
                    }
                }
            }

            while (--bracket_Level > 0)
            {
                Element_Colection.Add(new Close_Bracket());
            }

            return(Add_Close_Bracket(Opening_Bracket, Root_Bracket_Pair));
        }
Beispiel #2
0
        /// <summary>
        /// Adds missing copening bracket at the beging of equation if missing.
        /// </summary>
        /// <param name="Closing_Bracket"></param>
        /// <param name="Root_Bracket_Pair"></param>
        private void Add_Open_Bracket(Close_Bracket Closing_Bracket, Bracket_Pair Root_Bracket_Pair)
        {
            Open_Bracket new_OB = new Open_Bracket();

            Element_Colection.Insert(0, new_OB);

            Bracket_Pair bracket_Pair = new Bracket_Pair(new_OB, Closing_Bracket, Root_Bracket_Pair);
        }
Beispiel #3
0
        /// <summary>
        /// Adds missing closing bracket at the end of equation if missing.
        /// </summary>
        /// <param name="_Open_Bracket"></param>
        /// <param name="Root_Bracket_Pair"></param>
        /// <returns></returns>
        private Bracket_Pair Add_Close_Bracket(Open_Bracket _Open_Bracket, Bracket_Pair Root_Bracket_Pair)
        {
            Close_Bracket new_CB = new Close_Bracket();

            Element_Colection.Add(new_CB);

            return(new Bracket_Pair(_Open_Bracket, new_CB, Root_Bracket_Pair));
        }
        public Bracket_Pair(Open_Bracket OB, Close_Bracket CB, Bracket_Pair RBP)
        {
            Open_Bracket  = OB;
            Close_Bracket = CB;

            Open_Bracket.Pair  = this;
            Close_Bracket.Pair = this;

            Root_Bracket_Pair = RBP;

            Inner_Brackets = new List <Bracket_Pair>();
            if (Root_Bracket_Pair != null)
            {
                Root_Bracket_Pair.Inner_Brackets.Add(this);
            }
        }
        /// <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);
        }
Beispiel #6
0
        /// <summary>
        /// Pairs brackets in equation, adds open brackets at the begining or close brackets at the end if there are missing.
        /// </summary>
        private void Bracket_Pairer()
        {
            Bracket_Pair Current_Externall_Bracket = Bracket_Map_Root;

            for (int i = 0; i < Element_Colection.Count; i++)
            {
                if (Element_Colection[i] is Open_Bracket OB && OB.Pair == null)
                {
                    Current_Externall_Bracket = Search_for_Close_Bracket(OB, Current_Externall_Bracket);
                }

                if (Element_Colection[i] is Close_Bracket CB)
                {
                    if (CB.Pair == null)
                    {
                        Add_Open_Bracket(CB, Current_Externall_Bracket);
                    }
                    else
                    {
                        Current_Externall_Bracket = CB.Pair.Root_Bracket_Pair;
                    }
                }
            }
        }
 public Bracket_Pair()         // Only for root of bracket map
 {
     Inner_Brackets    = new List <Bracket_Pair>();
     Root_Bracket_Pair = null;
 }