Beispiel #1
0
        /// <summary>
        /// Checks and corrects mistakes in equation.
        /// </summary>
        /// <exception cref="EquationIsEmptyException">
        /// The exception that is thrown when equation class has empty list of elements,
        /// because given text is empty or correction of incorrectly written equation text left no elements in the equation.
        /// </exception>
        private void Integrity_Check()
        {
            Elements_Enumerator Equation_Rator = new Elements_Enumerator(Element_Colection);

            Check_Integrity_Of_Equation_Begining(Equation_Rator);

            if (Equation_Rator.MoveNext() == false)
            {
                IElement Even_Preious_Element = null;

                do
                {
                    Check_Integrity_Of_Current_Element(Equation_Rator, Even_Preious_Element);

                    Even_Preious_Element = Equation_Rator.Previous_Element;
                } while (Equation_Rator.MoveNext() == false);
            }

            if (Element_Colection.Count < 1)
            {
                throw new EquationIsEmptyException();
            }

            while (Element_Colection[Element_Colection.Count - 1] is INot_At_The_End)
            {
                Element_Colection.RemoveAt(Element_Colection.Count - 1);
                if (Element_Colection.Count < 1)
                {
                    throw new EquationIsEmptyException();
                }
            }
        }
Beispiel #2
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 #3
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 #4
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));
        }
        /// <summary>
        /// Selects every character to organized as element list.
        /// </summary>
        private void Element_Selector()
        {
            if (the_Equation == default(string) || the_Equation == "")
            {
                Element_Colection.Add(new Number(0));

                return;
            }

            for (int i = 0; i < the_Equation.Length; i++)
            {
                i = Element_Segregator(i);
            }
        }
        /// <summary>
        /// Checks does given character is operand.
        /// </summary>
        /// <param name="sign">Character to check is operand.</param>
        private bool Is_Operand(char sign)
        {
            for (int i = 0; i < Order_Of_Operations.operand_Sings.Length; i++)
            {
                for (int k = 0; k < Order_Of_Operations.operand_Sings[i].Length; k++)
                {
                    if (Order_Of_Operations.operand_Sings[i][k] == sign)
                    {
                        Element_Colection.Add(Order_Of_Operations.Operands[i][k]);

                        return(true);
                    }
                }
            }

            return(false);
        }
        /// <summary>
        /// Checks does given character is bracket.
        /// </summary>
        /// <param name="sign">Character to check is bracket.</param>
        private bool Is_Bracket(char sign)
        {
            if (sign == '(')
            {
                Element_Colection.Add(new Open_Bracket());

                return(true);
            }

            if (sign == ')')
            {
                Element_Colection.Add(new Close_Bracket());

                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Returns end index of number whether given index is start of a number or do nothing if not.
        /// </summary>
        private int End_Number(int first_Sign_Position)
        {
            int sign_Position = first_Sign_Position;

            sign_Position = End_of_Number_Finder(sign_Position);

            if (first_Sign_Position == sign_Position)
            {
                return(sign_Position);
            }
            else
            {
                Number Parsed_Number = _Parser.Parse(the_Equation.Substring(
                                                         first_Sign_Position, sign_Position - first_Sign_Position));

                Element_Colection.Add(Parsed_Number);

                return(sign_Position);
            }
        }
Beispiel #9
0
        /// <summary>
        /// Removes wrong elements at the begining of the equation and returns number of removed elements.
        /// </summary>
        /// <param name="Equation_Rator"></param>
        /// <exception cref="EquationIsEmptyException">
        private int Check_Integrity_Of_Equation_Begining(Elements_Enumerator Equation_Rator)
        {
            int removed_Elements = 0;

            if (Equation_Rator.MoveNext() == true)
            {
                throw new EquationIsEmptyException();
            }

            // If equation begings by operand[s] - removes it[them]
            while (Equation_Rator.Current_Element is INot_At_the_Begining)
            {
                if (Element_Colection.RemoveAt(Equation_Rator.Current_Index) == false)
                {
                    throw new EquationIsEmptyException();
                }
                removed_Elements++;
            }
            return(removed_Elements);
        }
 /// <summary>
 /// Recreats string of equation after correcting mistakes.
 /// </summary>
 public string Recreate_Equation()
 {
     return(Element_Colection.Recreate_Equation());
 }
        /// <summary>
        /// Finds mistakes of given elements and correct them in custom way.
        /// </summary>
        /// <param name="Equation_Rator">Enumerator of current list.</param>
        /// <param name="Even_Preious_Element">elemnt </param>
        private void Check_Integrity_Of_Current_Element(Elements_Enumerator Equation_Rator, IElement Even_Preious_Element)
        {
            IElement Current_Element  = Equation_Rator.Current_Element;
            IElement Previous_Element = Equation_Rator.Previous_Element;

            if (Even_Preious_Element is Bracket && Equation_Rator.Previous_Element is IOperand && Equation_Rator.Current_Element is Bracket)
            {
                if (Even_Preious_Element is Open_Bracket || Equation_Rator.Current_Element is Close_Bracket)
                {
                    Element_Colection.RemoveAt(Equation_Rator.Current_Index);

                    Equation_Rator--;

                    Element_Colection.RemoveAt(Equation_Rator.Current_Index);

                    int removed_Elements = Check_Integrity_Of_Equation_Begining(
                        new Elements_Enumerator(Element_Colection));

                    for (int i = 0; i <= removed_Elements; i++)
                    {
                        Equation_Rator--;
                    }
                }
            }
            else if (Current_Element is Open_Bracket && Previous_Element is INot_Outside_The_Open_Bracket)
            {
                Element_Colection.Insert(Equation_Rator.Current_Index, new Multiplication());

                Equation_Rator--;
            }
            else if (Previous_Element is Operand && Current_Element is Operand)
            {
                Element_Colection.RemoveAt(Equation_Rator.Current_Index);

                Equation_Rator--;
            }
            else if (Previous_Element is Open_Bracket && Current_Element is Close_Bracket)
            {
                Element_Colection.RemoveAt(Equation_Rator.Current_Index);

                Equation_Rator--;

                Element_Colection.RemoveAt(Equation_Rator.Current_Index);

                Equation_Rator--;
            }
            else if (Previous_Element is Close_Bracket && Current_Element is INot_Outside_The_Close_Bracket)
            {
                Element_Colection.Insert(Equation_Rator.Current_Index, new Multiplication());

                Equation_Rator--;
            }
            else if (Previous_Element is Open_Bracket && Current_Element is INot_Intside_Bracket)
            {
                Element_Colection.RemoveAt(Equation_Rator.Current_Index);

                Equation_Rator--;
            }
            else if (Current_Element is Close_Bracket && Previous_Element is INot_Intside_Bracket)
            {
                Element_Colection.RemoveAt(Equation_Rator.Current_Index - 1);

                Equation_Rator--;
            }
        }